#include <iostream>
usingnamespace 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.";
elseif (total / students >= 84.5 && total / students < 92.5)
cout << total / students << ": B.";
elseif (total / students >= 73.5 && total / students < 84.5)
cout << total / students << ": C.";
elseif (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?
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.