Name and Grade Structure - C++ Note: Must use structures, arrays, and functions.
ID: 3669823 • Letter: N
Question
Name and Grade Structure - C++
Note: Must use structures, arrays, and functions.
Input file: NameStruct.txt (found here: http://pastebin.com/2vQQHrHy )
You are to write a program that will read in up to 15 students with student information and grades. Your program will compute an average and print out certain reports.
Format: The information for each student in on separate lines of input. The first data will be the student’s ID number, next the students classification, next line is the students name and the last line are the 9 grades where the last grade is a final and is to be counted twice.
Read in the students, compute their averages and print out the following reports:
Print out each student ID and their average.
Print out all the Seniors in a list with their name and average.
Print out all the Juniors in a list with their name an average.
Sort the students on ID and print out ID and average.
Print out the class average, (average of the averages.)
Be sure to label each list and line up all output in straight columns. (No waves)
Example of inputs are given below.
Example input
12345
Junior
tom archer jones
89 90 95 90 95 100 85 90 95
51321
Senior
dewy luewey
90 95 100 100 95 95 95 100 95
Explanation / Answer
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <string.h>
#include <stdlib.h>
using namespace std;
struct Student{
string id;
string type;
string name;
int marks[10];
double average;
};
int main()
{
ifstream inFile;
inFile.open("student_info.txt");
int n;
cout<<"Enter number of student[1-15] : ";
cin>>n;
string marks;
Student students[n];
double class_total = 0;
for(int i=0; i<n; i++){
inFile>>students[i].id;
inFile>>students[i].type;
inFile.ignore();
getline(inFile, students[i].name);
//inFile.ignore();
getline(inFile, marks);
cout<<marks<<endl;
//spliting marks line
istringstream iss(marks);
int j=0;
int total = 0;
int m;
while (iss && j<9) {
string mark;
iss >> mark;
m = atoi( mark.c_str());
students[i].marks[j] = m;
total = total+m;
j++;
}
//counting last grade as twice
students[i].marks[j] = students[i].marks[j-1];
total = total+m;
students[i].average=total/10.0;
class_total = class_total + total/10.0;
}
cout<<"........................................"<<endl;
cout<<"Class Average: "<<class_total/n<<endl;
cout<<"........................................"<<endl;
cout<<"ID "<<"Average"<<endl;
for(int i=0; i<n;i++){
cout<<students[i].id<<" "<<students[i].average<<endl;
}
cout<<"........................................"<<endl;
cout<<"Seniors Info"<<endl;
cout<<"ID "<<"Average"<<endl;
string s="Senior";
for(int i=0; i<n;i++){
if (s.compare(students[i].type) == 0)
cout<<students[i].id<<" "<<students[i].average<<endl;
}
cout<<"........................................"<<endl;
cout<<"Junior Info"<<endl;
cout<<"ID "<<"Average"<<endl;
s="Junior";
for(int i=0; i<n;i++){
if (s.compare(students[i].type) == 0)
cout<<students[i].id<<" "<<students[i].average<<endl;
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.