is this possible?

closed account (G20RfSEw)
I wrote a swtich statement and am wondering if its possible to put a loop into this code and if i could how would i do it? what i want is, if the grade chosen is not a,b or c, than i want it to repeat until they put that char in.

#include <iostream>
#include <string>

using namespace std;
int main ( )
{
char grade, Keep_going;

cout << "enter your grade based on how you think you performed???\n";
cin >> grade;




do
{

switch (grade)
{
case 'A':
case 'a':
cout << "Wow. You probably did get an A\n";
cout << "great job this semester\n";

break;

case 'B':
case 'b':
cout << "that is pretty good. However you coulda done better\n";

break;

case 'c':
case 'C':
cout << "you failed\n";

break;

default:
cout << "that is not an option. please pick\n";
cout << " between a,b,c.";
}

cin >> Keep_going;




return 0;
closed account (j3Rz8vqX)
Something like this:
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
#include <iostream>
#include <string>

using namespace std;
int main ( )
{
    char grade, Keep_going;
    do
    {
        cout << "enter your grade based on how you think you performed???\n";
        cin >> grade;
        switch (grade)
        {
            case 'A':
            case 'a':
                cout << "Wow. You probably did get an A\n";
                cout << "great job this semester\n";
                break;
            case 'B':
            case 'b':
                cout << "that is pretty good. However you coulda done better\n";
                break;

            case 'c':
            case 'C':
                cout << "you failed\n";
                break;

            default:
                cout << "that is not an option. please pick\n";
                cout << " between a,b,c.";
        }
    }while(/*In Here!*/);//<<---Insert your condition here
    cin >> Keep_going;
    return 0;
}


It currently cannot compile. After you insert your condition, then maybe...

Have fun.
you can put the switch case in while loop like this:-
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
#include <iostream>
#include <string>

using namespace std;
int main ( )
{
char grade, Keep_going;

cout << "enter your grade based on how you think you performed???\n";
cin >> grade;



while(getch())
{

switch (grade)
{
case 'A':
case 'a':
cout << "Wow. You probably did get an A\n";
cout << "great job this semester\n";

break;

case 'B':
case 'b':
cout << "that is pretty good. However you coulda done better\n";

break;

case 'c':
case 'C':
cout << "you failed\n";

break;

default:
cout << "that is not an option. please pick\n";
cout << " between a,b,c.";
} 

cout << "enter your grade based on how you think you performed???\n";
cin >> grade;

}



return 0;
closed account (G20RfSEw)
thank you so much. i really appreciate the help from you.



And it did work. thanks:)
Last edited on
I think it won't work.
if(case 'c'), it will compare if it is 'C', which is false.
Topic archived. No new replies allowed.