Hello, i am currently working on a grade program that displays the student name, id #, and 2 grades. I have to read each of these categories off of an input file which should look like the following for at least 20 students.:
EX:
Jack Smith
450654333
95 89
The output should look like:
Student Name ID Eng101 Hist201
Jack Smith 450654333 95(A) 89(B)
Class Average: ---
The first problem I am having is reading the output from my teacher's desired input file (the spaces between the first and last name and grades were killing me). Therefore, I change the input file to this to try to make it easier, but might lose points for doing so:
Jack
Smith
45456650
95
89
The second problem is calculating the grades and class average, i do not understand how to do so from an input file.
Here is my code thus far:
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
const int MAX_STDS = 20;
struct StudentGrade {
string first;
string last;
int id;
float eng101;
float hist201;
};
void init_students(StudentGrade[]);
void read_data(StudentGrade[], int&);
void print_results(const StudentGrade[], int);
//void calc_grade (StudentGrade[]);
//int calc_avg (StudentGrade[]);
int main()
{
StudentGrade studList[MAX_STDS];
int std_num;
init_students(studList);
read_data(studList, std_num);
print_results(studList, std_num);
return 0;
}
void init_students(StudentGrade st[])
{
for (int j = 0; j < MAX_STDS; j++)
{
st[j].first = "";
st[j].last = "";
st[j].id = 0;
st[j].eng101 = 0.0;
st[j].hist201 = 0.0;
}
}
void read_data(StudentGrade st[], int& num)
{
ifstream inFile;
string fileName;
int i = 0;
cout << "Enter file name that contains data: ";
cin >> fileName;
inFile.open(fileName.data());
while (!inFile)
{
cout << "File does not exist!" << endl;
cout << "Enter file name: ";
cin >> fileName;
inFile.open(fileName.data());
}
for (int i = 0; i < MAX_STDS; i++)
{
inFile >> st[i].first >> st[i].last
>> st[i].id >> st[i].eng101
>> st[i].hist201;
}
num = i;
}
/*void calc_grade(StudentGrade st[])
{
char letter;
for (int i = 0; i < MAX_STDS; i++)
{
if ((st[i].eng101 < 60) || (st[i].hist201 < 60))
letter = 'F';
else if ((st[i].eng101 < 70) || (st[i].hist201 < 70))
letter = 'D';
else if ((st[i].eng101 < 80) || (st[i].hist201 < 80))
letter = 'C';
else if ((st[i].eng101 < 90) || (st[i].hist201 < 90))
letter = 'B';
else
letter = 'A';
}
}
*/
/*int calc_avg (StudentGrade st[])
{
int avg;
int sum;
int num;
for (int i = 0; i < MAX_STDS; i++)
{
sum = st[i].eng101 + st[i].hist201;
}
num = i;
avg = sum / num;
return (avg);
}
*/
void print_results(const StudentGrade st[], int num)
{
cout << "Student Name" << setw(15) << "Student ID" << setw(15)
<< "Eng101" << setw(10) << "Hist201" << endl;
for (int i = 0; i < MAX_STDS; i++)
{
cout << st[i].first << ' ' << st[i].last
<< setw(20) << st[i].id << setw(15) << st[i].eng101 <<
setw(10) << st[i].hist201 << endl;
}
}
Any suggestions?