Question

In: Computer Science

Create a C++ program that follows the specifications below: *Define a struct with 4 or more...

Create a C++ program that follows the specifications below:

*Define a struct with 4 or more members.

*Application must have at least one user-defined function

*Declare an array of your struct using a size of 10 or more

*Load the date for each element in your array from a text file

*Display the data in your array in the terminal

*provide brief comments for each line of code

Solutions

Expert Solution

#include<iostream>
#include<fstream>

using namespace std;

struct Element
{
int id;
char name[20];
char date[10];
int age;
char course[30];
float fee;
  
};
// Here the structure has got 6 members of different datatypes.

int enterData(Element E[20]){
ifstream fin;
//file variable declared in read mode
fin.open("data.txt");
//textfile name is data.txt
int i=0;
while (!fin.fail()) {
fin >> E[i].id >> E[i].name >> E[i].date>> E[i].age>> E[i].course>> E[i].fee;
//reads the space seperated values in file to each fields
i++;
}
  
fin.close();
return --i;
//i-1 returns the count of data or the size of array.
}
void displayData(int size, Element E[] ){
for(int i=0;i<size;i++){
  
cout<<"\nDATA - "<<i+1<<"\nID\t"<<E[i].id<<"\nName\t"<<E[i].name<<"\nDate of Birth\t"<<E[i].date;
cout<<"\nAge\t"<<E[i].age<<"\nCourse\t"<<E[i].course<<"\nFee\t"<<E[i].fee<<endl;
//displays the structure elments one by one
}
}
int main()
{
Element E[20];
//declared a structure array of size 20
int n = enterData(E);
//returned size of array is caught in variable n.enterData() is used to assign value to structure variables
displayData(n,E);
//this function displays the stored data in structure array
return 0;
}

Here I have provided the error free working code. Do provide a thumbs up if its correct


Related Solutions

Design c++ program so that it correctly meets the program specifications given below.   Specifications: Create a...
Design c++ program so that it correctly meets the program specifications given below.   Specifications: Create a menu-driven program that finds and displays areas of 3 different objects. The menu should have the following 4 choices: 1 -- square 2 -- circle 3 -- right triangle 4 -- quit If the user selects choice 1, the program should find the area of a square. If the user selects choice 2, the program should find the area of a circle. If the...
write a Program in C++ Using a structure (struct) for a timeType, create a program to...
write a Program in C++ Using a structure (struct) for a timeType, create a program to read in 2 times into structures, and call the method addTime, in the format: t3 = addTime(t1, t2); Make sure to use add the code to reset and carry, when adding 2 times. Also, display the resultant time using a function: display(t3);
Write a program in C language 1- Define a struct for students with the aforementioned attributes,...
Write a program in C language 1- Define a struct for students with the aforementioned attributes, test it by populating one initialized struct variable with arbitrary input and take screenshots of the output. 2- For each student, struct add an array of number grades for a class the students are enrolled in such as S E 185. Then write functions which find the max, average, and minimum score for a specified assignment identified by a number, for example, Assignment 0...
Using C++ language, create a program that uses a struct with array variables that will loop...
Using C++ language, create a program that uses a struct with array variables that will loop at least 3 times and get the below information: First Name Last Name Job Title Employee Number Hours Worked Hourly Wage Number of Deductions Claimed Then, determine if the person is entitled to overtime and gross pay. Afterwards, determine the tax and net pay. Output everything to the screen. Use functions wherever possible. Bonus Points: Use an input file to read in an unknown...
Write in C++: create a Doubly Linked List class that holds a struct with an integer...
Write in C++: create a Doubly Linked List class that holds a struct with an integer and a string. It must have append, insert, remove, find, and clear.
HW_6d - Write a struct object to a binary file. Create a new C++ project and...
HW_6d - Write a struct object to a binary file. Create a new C++ project and name it as:   Cats Create a Source.cpp file. Declare a struct named Cat. (in the Source.cpp file, or in a separate header file) Each Cat object has a name and age. The name data member is a c_string. The age data member is an integer. Ask the user to enter 3 cats. Use a while loop to read the information about one cat entered...
Name your c++ file Word_LastNameFirstName.cpp. Create a struct that has a word and the length of...
Name your c++ file Word_LastNameFirstName.cpp. Create a struct that has a word and the length of the word. Next, use the struct to store the word read from a text file call “word.txt” and the length of the word. Print out the word x number of times based on the length of the word as shown below. Also, print a statement with the length and determine if the string length has an odd number or even number of characters. You...
Problems create a C++ program that will do the followings - define 3 double variables x,...
Problems create a C++ program that will do the followings - define 3 double variables x, y, z - calculate the value of y as the following formula: y = 2*x*x+4*x+5 and print x and y; - assign a new value to x: x=5.6 - calculate the value of z as the following formula z = (y*y)/4 + (x*x)/5 - print x, y,x
C++ tree program (do NOT use STRUCT, use classes)    Program 1 Implement a Binary tree...
C++ tree program (do NOT use STRUCT, use classes)    Program 1 Implement a Binary tree using an array    Program 2 Implement a tree using linked list - pointer Binary Tree    Program 3 - Convert program 1 to a template
(C++ ONLY) You will define your own data type. It will be a struct called Fraction....
(C++ ONLY) You will define your own data type. It will be a struct called Fraction. The struct will have 2 integer fields: numerator and denominator. You will write a function called reduce that takes a Fraction as a parameter and returns that Fraction in its reduced form. For example, if the fraction 2/4 is passed to the function, the fraction 1/2 will be returned. Consider any fraction with a denominator of 1 to be in reduced form. You will...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT