I am trying to get my code below to display what high school the students are from from the .txt file

the text file below will work but i want to add school to txt file and confused on what to change in my code

Alphy Beta 85 83 77 91 76
Brant Baxter 80 90 95 93 48
Craig Cooper 78 81 11 90 73
Don Duck 92 83 30 69 87
Ellie Eggman 23 45 96 38 59
Frank Fritz 60 85 45 39 67
George Gomez 77 31 52 74 83
Harry Hacker 93 94 89 77 97
Hilda Hacker 79 85 28 93 82
Horatio Hacker 77 78 79 80 45
Izzie Ills 95 72 99 89 91

text file should be the follwing
"PS 321 Brooklyn, NY" is the school

PS 321 Brooklyn, NY
Alphy Beta 85 83 77 91 76
Brant Baxter 80 90 95 93 48
Craig Cooper 78 81 11 90 73
Don Duck 92 83 30 69 87
Ellie Eggman 23 45 96 38 59
Frank Fritz 60 85 45 39 67
George Gomez 77 31 52 74 83
Harry Hacker 93 94 89 77 97
Hilda Hacker 79 85 28 93 82
Horatio Hacker 77 78 79 80 45
Izzie Ills 95 72 99 89 91

So the first line of print results I want
"Test results from: PS 321 Brooklyn, NY"

and i have a problem with my numbers for students not lining up in even columns

#include <iostream> 
#include <fstream> 
#include <string> 
#include <iomanip>


using namespace std;

struct StudentData
{
string firstname;
string lastname;
int tests[5]; 
};

int gethighest(const StudentData list[], int listsize);
int getlowest(const StudentData list[], int listsize);
void CalcAverage(const StudentData list[], double Sum[], double Average[], int listsize);
void getGrade(const StudentData list[], double Average[], char Grade[], int listsize);
void printheading();
void PrintResults(const StudentData list[], double Average[], char Grade[], int listsize);



int main()
{
const int NoOfStudents = 200; 
StudentData item;
int students;
int listsize;
StudentData list[NoOfStudents];
char Grade[NoOfStudents];
double Sum[NoOfStudents];
double Average[NoOfStudents];


ifstream inFile;
string filename;
cout << "Programmer Name: Chris Cummings"
<< endl;
cout << "Lab CRN: 10945\n\n"
<< endl; 


cout << "Enter the file name of student data: \t";
cin >> filename;
cout << endl;

inFile.open(filename.c_str()); 

if (!inFile) 
{ 
cout << "Missing File \n\n";
system ("pause");
return 1;
}

int test;
students = 0;

inFile >> list[students].firstname >> list[students].lastname; 
while (inFile) 
{
for(test = 0; test < 5; test++)
{
inFile >> list[students].tests[test];
}
students++; 
inFile >> list[students].firstname >> list[students].lastname;
} 
students = students + 1; 
CalcAverage(list, Sum, Average, students); 
getGrade(list, Average, Grade, students);
printheading();
PrintResults(list, Average, Grade, students);



cout << "The program is complete. " << endl;
inFile.close();
system ("pause");
return 0;
}

void CalcAverage(const StudentData list[], double Sum[], double Average[], int listsize) 
{
int row, col, test; 
double lowest, highest;


highest = static_cast<double>(gethighest(list, listsize));
lowest = static_cast<double>(getlowest(list, listsize));

for (int row = 0; row < listsize; row++)
{
Sum[row] = 0.0;
for(test = 0; test < 5; test++)
{
Sum[row] = Sum[row] + static_cast<double>(list[row].tests[test]); 
if(list[row].tests[test] == lowest) 
Sum[row] = Sum[row] - lowest;
if(list[row].tests [test] == highest)
Sum[row] = Sum[row] + highest;
} 
Average[row] = (Sum[row] / 5);
}
}

int gethighest(const StudentData list[], int listsize)
{
int row, col;
int highest;

for (row = 0; row < listsize; row++)
{
highest = list[row].tests[0];
for (col = 1; col < 5; col++)
{
if(highest < list[row].tests[col])
highest = list[row].tests[col];
}
}
return highest;
}


int getlowest(const StudentData list[], int listsize)
{
int row, col;
int lowest;

for (row = 0; row < listsize; row++)
{
lowest = list[row].tests[0]; 
for (col = 0; col < 5; col++)
{
if(lowest > list[row].tests[col])
lowest = list[row].tests[col];
}
}
return lowest;
} 

void getGrade(const StudentData list[], double Average[], char Grade[], int listsize)
{

int row;

for (int row =0; row < listsize; row++)
{

if(Average[row] >= 90)

Grade[row] = 'A';

else if (Average[row] >= 80)

Grade[row] = 'B';

else if (Average[row] >= 70)

Grade[row] = 'C';

else if (Average[row] >= 60)

Grade[row] = 'D';
else

Grade[row] = 'F'; 
}
}


void printheading()
{
cout << "-*-*-*-*-*-*-*-*-*-*-*-*Classroom Results*-*-*-*"
<< "-*-*-*-*-*-*-*-*-*-*-*-*-*" << endl;
cout << "------------------------------------------------"
<< "---------------------------------" << endl;
cout << " Student Test1 Test2 Test3 Test4 Test5"
<< " Average Grade" << endl; 
cout << "------------------------------------------------"
<< "---------------------------------\n\n" << endl;
} 

void PrintResults(const StudentData list[], double Average[], char Grade[], int listsize)
{
int row, col;
double SumAverage = 0.0;
double ClassAverage;
int Excellent = 0,
Satisfactory = 0,
Failing = 0;
int looper ;


for ( looper = 0; looper < listsize - 1; looper++)
{
cout << list[looper].lastname << " , " << list[looper].firstname;
for(int test = 0; test < 5; test++)
{
cout << " " << list[looper].tests[test] << " ";
} 

cout << Average[looper] << " "
<< Grade[looper] << endl;
if (Grade[looper] == 'A')
Excellent++;
else if (Grade[looper] == 'B' || Grade[looper] == 'C')
Satisfactory++;
else if (Grade[looper] == 'D' || Grade[looper] == 'F')
Failing++; 
}

cout << "The number of students in the class: \t\t"
<< listsize - 1 << endl;

for(row = 0; row < listsize - 1; row++)
{
SumAverage = SumAverage + Average[row]; 
} 
ClassAverage = SumAverage / (listsize - 1);
cout << "Class Average = " << static_cast<int>(ClassAverage) << endl;
cout << "Number of Students in each Category:\n\n " 
<< "Excellent:\t\t" << Excellent << "\n"
<< "Satisfactory:\t\t" << Satisfactory << "\n"
<< "Failing:\t\t" << Failing << "\n\n\n";


}

Recommended Answers

All 7 Replies

You could always just test for the student with the longest name, and find the difference in spacing between that name and every other name. In doing so, you can print the correct amount of spaces after each person's name, followed by the list of grades.

Good luck.

P.S. I'm originally from Brooklyn lol

You have to read the desired line out of the file between the time you open the file and the time you start reading student information out of it, maybe around line 57:

string schoolInfo = inFile.getline();
cout << "Test results from: " << schoolInfo << endl;

Sorry, I'm not going to fight through all that unformatted code. It's difficult to follow. See this to learn how to format code for readability and easier coding.

You have to read the desired line out of the file between the time you open the file and the time you start reading student information out of it, maybe around line 57:

string schoolInfo = inFile.getline();
cout << "Test results from: " << schoolInfo << endl;

i tried putting it in but i get an error where it say .getline();

it says undeclared

Sorry, incorrect usage, my fault. This should work:

char _schoolInfo[100];
inFile.getline(_schoolInfo, 100);
if (inFile.fail()) {
    cerr << "school info was longer than 100 characters, sorry!" << endl;
    return 1;
}
// make a std::string from the char-array
string schoolInfo(_schoolInfo);
cout << "Test results from: " << schoolInfo << endl;

Sorry, incorrect usage, my fault. This should work:

char _schoolInfo[100];
inFile.getline(_schoolInfo, 100);
if (inFile.fail()) {
    cerr << "school info was longer than 100 characters, sorry!" << endl;
    return 1;
}
// make a std::string from the char-array
string schoolInfo(_schoolInfo);
cout << "Test results from: " << schoolInfo << endl;

Thank you, I got it working using that info.

Awesome! Please mark your thread as "solved". :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.