Can't terminate program correctly

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
#include <iostream>
using namespace std;

int main()
{
 float students, individual, total;
 int count = 1;
 cout << "How many students do you have? ";
 cin >> students;
 for(int i = 0; i < students; i++){
 cout << "Score number " << count << ": ";
 cin >> individual;
 total += individual;
 count++;
 if(individual == 'q' || individual == 'Q')
   break;
 } 
 cout << "Your class average is a ";
 if(total / students >= 92.5)
   cout << total / students << ": A.";
 else if (total / students >= 84.5 && total / students < 92.5)
   cout << total / students << ": B.";
 else if (total / students >= 73.5 && total / students < 84.5)
   cout << total / students << ": C.";
 else if (total / students >= 69.5 && total / students < 73.5)
   cout << total / students << ": D.";
 else
   cout << total / students << ": F.";
 cin.get();
 cout << "\nPress Enter to continue...";
 cin.get();
 return 0;   
}

For some reason, an infinite loop occurs when I try to use 'q' to quit. Any ideas?
Works fine for me.

GCC 3.4.5 (MingW)
Last edited on
Your variable individual is of type float, and you are entering a character value as your sentinel value. Try changing your code to making -1 be the code to quit. Otherwise, you need to do some fancy coding in the way you input something that can be either a char or a numerical value.
You'll also want to make an if statement to make sure that the number for individual isn't negative before you add it to total, because you are using it also as a sentinel.

~psault
Topic archived. No new replies allowed.