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...
1) Define a C struct that can be used to represent an ingredient in a recipe....
1) Define a C struct that can be used to represent an ingredient in a recipe. You must include the ingredient, the amount to use, and the unit of measurement. When allocated, the struct must be self-contained; do not rely on information being stored anywhere else in memory. 2) Define a C function that will print an array of ingredients to the standard output stream, one per line. You must use the struct definition from the first part. 3) Define...
This project requires the student to create a record (in C++ called a struct). The record...
This project requires the student to create a record (in C++ called a struct). The record should contain several fields of different data types and should allow the user to search the records to find the student with the highest grade. In this project the student should gain an understanding of the nature of records with multiple data types, how to save the records, search the records and retrieve them.  The special problems of maintaining records of multiple data types on...
Write a c program that creates a struct to be able to read a .img file...
Write a c program that creates a struct to be able to read a .img file and a .pat file.
Write a C program with a struct that contains a string of 12 characters and two...
Write a C program with a struct that contains a string of 12 characters and two integers: number1 and number2. Read all 3 values in from the keyboard using scanf. Print the string and the sum of the two integers using the fully qualified struct names.
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT