Double nested loop, first writes grades to a text file and second reads grades from the text and averages, I cant get the break right too finish the program I have tried for 2 days now. any ideas?
Code:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
int x, studentID, grade1, grade2, grade3, grade4;
int score, total;
char again = 'Y';
ofstream file;
file.open("grade.txt");
while ( total == 0)
{
for (x = 0; x < 1; x++)
{
//enter grades
cout << "Enter student ID#:" << endl;
cin >> studentID;
cout << "Enter grade: " << endl;
cin >> grade1;
if (grade1 <0 || grade1 > 100)
{
cout << "0 - 100 only! Enter again: " << endl;
cin >> grade1;
}
else if (grade1 <= 0 || grade1 >= 100)
{
cout << endl;
}
cout << "Enter grade: " << endl;
cin >> grade2;
if (grade2 <0 || grade2 > 100)
{
cout << "0 - 100 only! Enter again: " << endl;
cin >> grade2;
}
else if (grade2 <= 0 || grade2 >= 100)
{
cout << endl;
}
cout << "Enter grade: " << endl;
cin >> grade3;
if (grade3 <0 || grade3 > 100)
{
cout << "0 - 100 only! Enter again: " << endl;
cin >> grade3;
}
else if (grade3 <= 0 || grade3 >= 100)
{
cout << endl;
}
cout << "Enter grade: " << endl;
cin >> grade4;
if (grade4 <0 || grade4 > 100)
{
cout << "0 - 100 only! Enter again: " << endl;
cin >> grade4;
}
else if (grade4 <= 0 || grade4 >= 100)
{
cout << endl;
}
//file imput
file << studentID << " " << grade1 << " " << grade2 << " " << grade3 << " " << grade4 << endl;
}
//exit or not
cout << "Do you want to enter another?" << endl;
cin >> again;
if ( again == 'Y' || again == 'y' )
{
cout << "OK lets continue" << endl << endl;
}
else if ( again == 'N' || again == 'n' )
{
break;
}
}//while loop ends
total = 0;
while ( 400 < score)
{
for (x = 0; x < 0; x++)
{
score = grade1 + grade2 + grade3 + grade4;
ifstream file ("grade.txt");
file >> score;
cout << "Average is: " << score / 4 << endl;
}
}
return 0;
}