I split this into two questions about the same problem. This is part 1 of the pr
ID: 3622030 • Letter: I
Question
I split this into two questions about the same problem. This is part 1 of the problem.The first thing its suppose to do is read the students’ name (first and last name) followed by their test scores in a .txt file The student data will be stored in a struct variable of type StudentType which has three components: studentName of type string, testScore of type int (testScore is between 0 and 100 inclusive), and grade of type char.
Reads all the students data into the array. When reading the data, read the
student’s first name and last name as separate strings, then read the test score. If the score is in the proper range (0 to 100) add the student to the list. Ignore any students with scores outside the range. To store the student’s name, concatenate the last name and first name strings so that a comma and a space appears between them in the format mentioned in the text. (starting out with C++, Gaddis text) Update the number of students in the array, making sure that the number of students does not go beyond the maximum number allowed while reading the data.
Explanation / Answer
please rate - thanks
#include <iostream>
#include <fstream>
#include <iomanip>
#define STUDENTS 50
using namespace std;
struct StudentType {
string name;
int score;
char grade;
} student[STUDENTS];
bool OpenInputFile(ifstream& infile, string& infilename);
void Pause(); // same as in previous labs
void ReadStudentData(ifstream& infile, StudentType student[],int& numStudents);
void AssignGrades(StudentType student[], int numStudents);
int HighestScore(const StudentType student[], int numStudents);
void PrintNamesWithHighestScore(const StudentType student[], int numStudents);
void DisplayAllStudents(const StudentType student[], int numStudents);
void GetLowHighRangeValues(int& lowRange, int& highRange);
void DisplayStudentsInRange(const StudentType student[], int numStudents, int lowNum, int highNum);
void SortStudentsByName(StudentType student[], int numStudents);
void SortStudentsByScore(StudentType student[], int numStudents);
int main()
{ifstream infile;
string infilename;
int numStudents=0;
if(OpenInputFile(infile,infilename))
{cout<<"error opening input file ";
}
else
{ReadStudentData(infile,student,numStudents);
AssignGrades(student, numStudents);
DisplayAllStudents(student,numStudents);
PrintNamesWithHighestScore(student,numStudents);
}
Pause();
return 0;
}
bool OpenInputFile(ifstream& infile, string& infilename)
{cout<<"what is the name of the file you are using? ";
cin>>infilename;
infile.open(infilename.c_str()); //open file
if(infile.fail()) //is it ok?
return true;
return false;
}
void Pause()
{
cin.ignore(80, ' ');
cout << " Hit the enter key to continue... ";
cin.get();
cout << endl;
}
void ReadStudentData(ifstream& infile, StudentType student[],int& numStudents)
{string first,last;
infile>>first;
while(infile&&numStudents<STUDENTS)
{infile>>last>>student[numStudents].score;
if(student[numStudents].score>=0&&student[numStudents].score<=100)
{student[numStudents].name=last+", "+first;
numStudents++;
}
infile>>first;
}
}
void AssignGrades(StudentType student[], int numStudents)
{ int i;
for(i=0;i<numStudents;i++)
switch((int)student[i].score/10) // using integer arithmetic by dividing
{case 10: // score by 10 anything between 90 to 99 becomes 9
case 9: // anything between 80 to 89 becomes 8 etc.
student[i].grade='A';
break;
case 8:
student[i].grade='B';
break;
case 7:
student[i].grade='C';
break;
case 6:
student[i].grade='D';
break;
default:
student[i].grade='F';
}
}
int HighestScore(const StudentType student[], int numStudents)
{int i,max;
max=student[0].score;
for(i=1;i<numStudents;i++)
if(student[i].score>max)
max=student[i].score;
return max;
}
void PrintNamesWithHighestScore(const StudentType student[], int numStudents)
{int max=HighestScore(student,numStudents);
char c;
cout<<" The highest score was "<<max<<endl;
cout<<"received by: ";
for(int i=0;i<numStudents;i++)
if(student[i].score==max)
{c=student[i].grade;
cout<<student[i].name<<endl;
}
cout<<"these students received a grade of "<<c<<endl;
}
void DisplayAllStudents(const StudentType student[], int numStudents)
{int i;
cout<<"name score grade ";
for(i=0;i<numStudents;i++)
cout<<setw(20)<<left<<student[i].name<<" "<<student[i].score<<" "<<student[i].grade<<endl;
}
void GetLowHighRangeValues(int& lowRange, int& highRange);
//This function asks the user for the low and high values for a range of scores that he/she
//would like to view. The function checks that the values are in the order 0 ? lowRange ?
//highRange ? 100. If the values are out of order, an error message is printed and the user is
//asked to enter the range again. The updated parameters are returned by the function.
void DisplayStudentsInRange(const StudentType student[], int numStudents, int lowNum, int highNum);
//This function displays the students in the low to high range. The range boundaries are
//passed in as arguments to the function.
void SortStudentsByName(StudentType student[], int numStudents);
//This function sorts the student list by name.
void SortStudentsByScore(StudentType student[], int numStudents);
//void getname(char[]);
//void getgrades(char[],double[]);
//char calcgrade(double[],double&);
//void print(char[],double[],char,double);