In: Computer Science
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
#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