infinite loop with do-while
Nov 9, 2010 at 9:51pm UTC
I have a project to do that takes students names and grades from a .txt file and display their names, avgs, and the highest avg. I must do this with a do-while loop. I have completed the program and it compiles fine but when I run it the program goes into an infinite loop. Any help would be awesome.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
ifstream inFile;
int chmp;
double test1, test2, test3, test4, test5, test6, test7;
double avg, avg1, avg2;
string firstName, lastName;
string firstName1, lastName1;
string firstName2, lastName2;
inFile.open("C:\\Users\\Cheeto\\Desktop\\PROG8\\prog8Data.txt" );
inFile >> firstName >> lastName >> test1 >> test2 >> firstName1 >> lastName1 >> test3 >> test4 >> test5
>> firstName2 >> firstName2 >> test6 >> test7;
//----------
cout << firstName << " " << lastName << " " << avg << endl;
cout << firstName1 << " " << lastName1 << " " << avg1 << endl;
cout << firstName2 << " " << lastName2 << " " << avg2 << endl;
//----------Loop for highest avg
do {
avg = (test1 + test2) / 2;
avg1 = (test3 + test4 + test5) / 3;
avg2 = (test6 + test7) / 2;
if (avg > avg1 && avg2){
cout << firstName << " " << lastName << " has the highest average: " ;
}
if (avg1 > avg && avg2){
cout << firstName1 << " " << lastName1 << " has the highest average: " ;
}
if (avg2 > avg && avg1){
cout << firstName2 << " " << lastName2 << " has the highest average: " ;
}
}
while (avg != avg1);{
cout << "Error" << endl;
}
inFile.close();
system("pause" );
return 0;
}
Nov 9, 2010 at 10:01pm UTC
The reason this will generate an infinite loop is because you haven't provided a condition in which avg
or avg1
can change in the case that avg != avg1
.
Topic archived. No new replies allowed.