Just stuck on writing up a loop and also help reading from the file created during program execution and displaying the info on screen.
Here's the code (the parts I need help with are near the bottom (but before the functions) and are commented:
//A student gradebook program that gets information from the user
//and then creates a text file based on that information. After that
//the information is input from the text file and displayed on screen.
//
//November 29, 2006
//
////////////////////////////////////////////////
#include<iostream>
#include<fstream>
#include<string>
#include<iomanip>
using namespace std;
struct grades
{
string firstName;
string lastName;
string idNumber;
int programs[3];
int exams[3];
int finalExam;
char courseGrade;
};
void printLine();
void calcGrade (grades &student);
int main()
{
ifstream infile;
ofstream outfile;
int noOfStudents = 0;
grades student[100];
int count;
int counter = 0;
outfile.open("gradebook.txt");
cout << "Enter the class size: ";
cin >> noOfStudents;
printLine();
while(noOfStudents > counter)
{
cout << "Enter student's first and last name: ";
cin >> student[counter].firstName >> student[counter].lastName;
cout << "Enter student id number: ";
cin >> student[counter].idNumber;
cout << "Enter 3 programming grades (each seperated by a space)" << endl
<< "Maximum grade for each program is 100: ";
for(count = 0; count < 3; count++)
cin >> student[counter].programs[count];
cout << "Enter 3 exam grades (each seperated by a space)" << endl
<< "Maximum grade for each exam is 100: ";
for(count = 0; count < 3; count++)
cin >> student[counter].exams[count];
cout << "Enter final exam grade (max grade is 100): ";
cin >> student[counter].finalExam;
calcGrade(student[counter]);
//You need a loop here to output programs and exams
outfile << student[counter].firstName << " " << student[counter].lastName << " " << student[counter].idNumber
<< " " << student[counter].programs[count]
<< " " << student[counter].exams[count]
<< " " << student[counter].finalExam << " " << student[counter].courseGrade << endl;
counter++;
printLine();
}
outfile.close();
//After that I need to read the input from the file and display it on screen
infile.open("gradebook.txt");
if(!infile)
cout << "Could not open the file.";
while(infile) //I know this is completely wrong since it's supposed to read
{ //from the file, so I can't use the variables I have below.
cout << "Student name: " << student.firstName << student.lastName << endl
<< "ID Number: " << student.idNumber << endl
<< "Programs: " << student.programs[0] << student.programs[1] << student.programs[2] << endl
<< "Exams: " << student.exams[0] << student.exams[1] << student.exams[2] << endl
<< "Final Exam: " << student.finalExam << endl
<< "Course Grade: " << student.courseGrade << endl << endl << endl;
}
return 0;
}
void printLine()
{
cout << "----------------------------------------" << endl;
}
void calcGrade (grades &student)
{
double programAverage = 0, examAverage = 0;
double semesterAverage;
int grade;
for (int count = 0; count < 3; count++)
programAverage += student.programs[count];
for (int exam = 0; exam < 3; exam++)
examAverage += student.exams[exam];
semesterAverage = programAverage/3 * .30 + examAverage/3 * .45 + student.finalExam * .25;
grade = (int) semesterAverage/10;
switch (grade)
{
case 10:
case 9: student.courseGrade = 'A';
break;
case 8: student.courseGrade = 'B';
break;
case 7: student.courseGrade = 'C';
break;
case 6: student.courseGrade = 'D';
break;
default:student.courseGrade = 'F';
}
}
Here's a sample of how the output/screen should ultimately look like (in case it helps):
Enter the class size: 3 ---------------------------------------- Enter student's first and last name: richard winters Enter student id number: 2004384 Enter 3 programming grades (each separated by a space) Maximum grade for each program is 100: 100 100 80 Enter 3 exam grades (separated by a space) Maximum grade for each exam is 100: 96 95 93 Enter final exam grade Maximum grade for final exam is 100: 94 ---------------------------------------- Enter student's first and last name: sue smith Enter student id number: 244333 Enter 3 programming grades (each separated by a space) Maximum grade for each program is 100: 80 95 95 Enter 3 exam grades (separated by a space) Maximum grade for each exam is 100: 93 92 92 Enter final exam grade Maximum grade for final exam is 100: 90 ---------------------------------------- Enter student's first and last name: joe klein Enter student id number: 3004222 Enter 3 programming grades (each separated by a space) Maximum grade for each program is 100: 87 92 93 Enter 3 exam grades (separated by a space) Maximum grade for each exam is 100: 89 88 84 Enter final exam grade Maximum grade for final exam is 100: 90 ---------------------------------------- Student Name: richard winters ID Number: 2004384 Programs: 100 100 80 Exams: 96 95 93 Final Exam: 94 Course Grade: A Student Name: sue smith ID Number: 244333 Programs: 80 95 95 Exams: 93 92 92 Final Exam: 90 Course Grade: A Student Name: joe klein ID Number: 3004222 Programs: 87 92 93 Exams: 89 88 84 Final Exam: 90 Course Grade: B Press Enter Key to Continue
This post has been edited by richiewinters: 04 December 2006 - 06:53 PM

New Topic/Question
Reply




MultiQuote



|