This programrequires you to write a C++ program that will manipulate datastored
ID: 3609973 • Letter: T
Question
This programrequires you to write a C++ program that will manipulate datastored in an array of structs. This program will give you practiceinputting data from files, writing and calling functions and usingarrays. A textfilecontains information about the final exam scores for students intwo sections of an art history class. Store all of the student datain one array of structs. Each line in the input file begins with acommand (P, S, C, A, D, Q). Your program will be commanddriven.
P: Print the contents ofthe list
C: Change the test scorefor a particular student; input the ID and the newscore.
S: Print the data for onesection; input the section number
A: Add data for a newstudent to the end of the array; input the section, ID, andscore
D: Delete the datafor a particular student; input the ID to delete.
Q: Quit processing.Sort the array into ascending order by ID. Print a report. Thereport should contain a listing of the ids and scores by section, the number of studentsin each section, the average of the scores in eachsection
and the high score in eachsection. The average should be a decimal formatted with 1 decimalplace. Print the report to the screen.
Format of the input file: Each line begins with a command. The format of the rest of the line depends on thecommand.
P
C <id> <new test score>
S <section number>
A<section number><id> < test score>
D <id>
Q
Sample lines:
A 62 5555 86
P
A 61 6666 83
C 1220 75
S 61
D 5555
P
Q
Notes:
2 Prompt the user to inputthe name of the input file. Two sample files are fiveA.txt andfiveB.txt.
3. Print an error message if the input file is missing.
4. Do not use global variables.
5.You should use functions to accomplish each task.
6. Use function prototypes. Declare the main function before youdeclare additional functions.
7. You may assume that there are no more than 50students.
five.txt
A 61 4678 51
A 62 5555 86
A 61 1220 62
A 62 1121 70
A 62 2221 83
A 61 3324 90
P
A 61 6666 83
A 61 7777 73
A 61 8888 63
A 61 9999 81
A 62 1010 66
A 62 1111 76
A 61 1221 50
A 62 1331 92
A 61 1441 73
P
S 61
S 62
C 1220 75
C 8888 75
A 61 9876 100
A 61 9875 100
S 61
D 1010
D 6666
P
C 5454 100
D 3321
P
D 7777
D 1331
D 1220
D 3324
D 1221
C 1441 100
C 1111 100
Q
T trash
X your progrma should not be reading this!
fivea.text
A 61 4678 51
A 62 5555 86
A 61 1220 62
A 62 1121 70
A 62 2221 83
A 61 3324 90
P
A 61 6666 83
A 61 7777 73
A 61 8888 63
A 61 9999 81
A 62 1010 66
A 62 1111 76
A 61 1221 50
A 62 1331 92
A 61 1441 73
P
D 3321
S 61
S 62
C 1220 75
C 8888 75
A 61 9876 100
A 61 9875 100
S 61
D 1010
D 6666
P
C 5454 100
P
D 7777
D 1331
D 1220
D 3324
D 1221
C 1441 100
C 1111 100
Q
T trash
X your program should not be reading this!
fiveb.txt
A 61 4678 51
A 62 5555 86
A 61 1220 62
A 62 1121 70
A 62 2221 83
A 61 3324 90
P
S 61
S 62
C 1220 100
A 61 9876 100
A 61 6001 100
S 61
C 3324 100
C 4678 100
P
D 1121
D 5555
Q
T trash
X your program should not be reading this!
Explanation / Answer
please rate - thanks about 1/2 done, will finish tomorrow #include<iostream.h>#include <fstream.h>
#include <iomanip.h>
using namespace std;
#define Max_students 50
struct record
{int id;
int section;
int score;
}students[Max_students];
void printlist(record[],int);
void change(record[],int,ifstream&);
void add(record[],int &,ifstream&);
void sort(record[],int);
void deleteit(record[],int);
void printreport(record[],int);
void printsection(record[],int,ifstream&);
voidswap(int,int);
int main()
{char type;
char filename[30];
int count=0;
cout<<"what is the name of the file you are using? ";
cin>>filename;
ifstream in;
in.open(filename); //open file
if(in.fail()) //is it ok?
{ cout<<"file did not open please checkit ";
system("pause");
return 1;
}
in >> type;
while(in)
{switch(type)
{case 'P': printlist(students,count);
break;
case 'S': printsection(students,count,in);
break;
case 'C': change(students,count,in);
break;
case 'A': add(students,count,in);
break;
case 'D': deleteit(students,count);
break;
case 'Q': sort(students,count);
printreport(students,count);
system("pause");
return 0;
}
in >> type;
}
}
void change(record s[], int count,ifstream & in)
{int iid,grade;
in>>iid>>grade;
for(int i=0;i<count;i++)
if(s[i].id==iid)
s[i].score=grade;
return ;
}
void deleteit(record s[], int count)
{return ;
}
void sort(record s[], int count)
{return ;
}
void printreport(record s[], int count)
{return ;
}
void add(record s[], int& i,ifstream & in)
{
in>>s[i].section>>s[i].id>>s[i].score;
i++;
return;
}
void printlist(record s[], int count)
{cout<<" section id score ";
for(int i=0;i<count;i++)
cout<<setw(5)<<s[i].section<<setw(9)<<s[i].id<<setw(7)<<s[i].score<<endl;
return ;
}
void printsection(record s[], int count,ifstream& in)
{int sect;
in>>sect;
cout<<" section:"<<sect<<endl;
cout<<" id score ";
for(int i=0;i<count;i++)
if(s[i].section==sect)
cout<<setw(7)<<s[i].id<<setw(7)<<s[i].score<<endl;
return;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.