While loop

How would I change this program so that if the user enters 'q', then the program ends?


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
#include <iostream>
#include <cctype>
/*
Input: Enter a grade, Q to exit
Output: Comments pertaining to the grade level
*/
using namespace std;
void main()
{
//variables
char letter, cont='Y';

//instructions
while (toupper(cont)=='Y')
{
cout<<"Please, enter grade:\t";
cin>>letter;

switch (toupper(letter))
{
case 'A':
cout<<"Excellent job!!!"<<endl;
break;
case 'B':
cout<<"Good job!"<<endl;
break;
case 'C':
cout<<"It's ok."<<endl;
break;
case 'D':
cout<<"You can do better."<<endl;
break;
case 'F':
cout<<"Please try again."<<endl;
break;
case 'Q':
cout<<"Bye."<<endl;
break;
default:
cout<<"Error in selection of grade!!!"<<endl;
break;
}

cout<<"Continue? (Y/N)\t";
cin>>cont;

}

}
Last edited on
Just stick a return 0; after the cout << "Bye." << endl; line.

Also, you should be using int main(), not void main() (which is non-standard).
In fact, you don't even need to put a return 0; in there, and int is one less character to type compared to void, so there's really no reason to use void main().
Thank you and I will most definitely start using int main.
What compiler are you using that allows void main? As pointed out by long double main, that is non-standard and any standard compliant compiler should have gave a compile error upon hitting void main. Though the other arguments of int being shorter and not needing to include return at the end of the main function are true, but not that big a deal.

So what operating system, compiler, and IDE (if any) are you using?
Topic archived. No new replies allowed.