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 a C program that creates a structure and displays its content. • Create a struct...
Write a C program that creates a structure and displays its content. • Create a struct that will be used to hold a student's name, age, and year in school (Freshman, Sophomore, Junior, or Senior) • Within function main, dynamically allocate space to hold the structure and assign a pointer to point to the memory space allocated • Read in (from the keyboard) the student's name, age, and year in school • Create a separate function with the prototype: void...
Create a C++ program that creates instances of a Struct using an Array (you can choose...
Create a C++ program that creates instances of a Struct using an Array (you can choose the number of instances to create). You can also use either user input or an initialization list to initialize 3 peoples names. Make sure that the values stored in each member are printed to the screen.
c++ Create a one-dimension array, y, of this struct data type with a length 4. Pass...
c++ Create a one-dimension array, y, of this struct data type with a length 4. Pass y into a function: PrintArrayElement( …). Finish the interface of this function, and loop over each array element inside this function
Maze in C++ Simple Maze Program – Project Specifications: 1. Create a simple maze game that...
Maze in C++ Simple Maze Program – Project Specifications: 1. Create a simple maze game that a player must traverse to win. 3. The maze must be text-based and adjustable from 5x5 to 20x20. • Player gets to choose size either as: 1) any size in the range from 5-20 by 5-20. 2) selects from 4 set size options [5x5,10x10,15x15, and 20x20] • the player can choose among different mazes. • You will need to figure out how to denote...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT