Do while loop question

im trying to write a do while loop for this program. its basically a report card. also i have to validate user input. i got it to work as a while loop, but the professor doesnt want that, she says there is spose to be two do while loops in this program. one at the top and one at the bottom.


my problem is it skips the switch statement, also feel like its a silly mistake that is skipping it.

any help is appreciated.


#include <iostream>
#include <iomanip>

using namespace std;

int main ()
{
char grade; // letter grade input
int gradePoints = 0; // grade point for gpa
int inputCounter = 0; // counts the number of vaild inputs
float gpa = 0; // grade point average


do
{
cout << "Enter letter grade (Enter 'X' to exit) ";
cin >> grade;
grade = toupper(grade);

if ((grade != 'A') && (grade != 'B') && (grade != 'C')
&& (grade != 'D')&& (grade != 'F') && (grade != 'X'))
{
cout << "\nInvalid letter grade, please try again\n\n";
}
}
while (grade != 'X');

inputCounter += 1;

switch (grade)
{
case 'A':
gradePoints += 4;
break;
case 'B':
gradePoints += 3;
break;
case 'C':
gradePoints += 2;
break;
case 'D':
gradePoints += 1;
break;
case 'F':
break;
default:
inputCounter -= 1;
break;

}


cout << setprecision(2) << fixed;

gpa = gradePoints / float(inputCounter);


cout << "Total Grade Points: " << gradePoints << endl;
cout << "GPA: " << gpa;


return 0;

}
your switch statement is outside your do-while loop

and please enclose your code with [code][/code] to be read easily on the forum
out of it as needs to tab over? or needs another set of brackets? ill recopy my code using

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
53
54
55
56
57
58
59
60
61
62
63
64
#include <iostream>
#include <iomanip>

using namespace std;

int main ()
{
	char grade;
	int gradePoints = 0;
	int inputCounter = 0;
	float gpa = 0;


	do
	{
		cout << "Enter letter grade (Enter 'X' to exit) ";
		cin >> grade;
		grade = toupper(grade);

		if ((grade != 'A') && (grade != 'B') && (grade != 'C')
				&& (grade != 'D')&& (grade != 'F') && (grade != 'X'))
		{
			cout << "\nInvalid letter grade, please try again\n\n";
		}
	}
		while (grade != 'X');

		inputCounter += 1;

		switch (grade)
		{
			case 'A':
				gradePoints +=  4;
				break;
			case 'B':
				gradePoints += 3;
				break;
			case 'C':
				gradePoints += 2;
				break;
			case 'D':
				gradePoints += 1;
				break;
			case 'F':
				break;
			default:
				inputCounter -= 1;
				break;

			}


	cout << setprecision(2) <<  fixed;

	gpa = gradePoints / float(inputCounter);


	cout << "Total Grade Points: " << gradePoints << endl;
	cout << "GPA: " << gpa;


	return 0;

}
You need to enclose your whole program into another do / while loop cause the way it is written now it will only work for one input and it will then exit.
what i mean is put your switch statement inside the do while loop

1
2
3
4
5
6
7
do {
    //user input here
    
    swith(input) {
        //logic
    }
}while (grade != 'X');
ya i had that, it worked but it worked the way the professor wanted...looks like i need a clearer explanation for the professor on what she wants.

Thanks for your help

she says there should be 2 do while loops in the programs so it confuses me becuase it works fine with just one.
i think im gunn try to make the whole program a while loop then the top, where it Error checks input implement a do-while loop, then again at the bottom. just dont know how to output nothing when 'X' and only x is enter. so if my first input is X then the whole thing terminates but if I put A then A then A then X it out puts gpa = 4.0 grade points 12
this is the prompt and my goal, maybe it will help make it clearer.

implement a program that obtains letter grades as input from a user, then coverts each letter grade into grade points. The program should total all the grade points and output the total grade points and the grade point average (note: you may have to track the total number of inputs to calculate the grade point average). The program should validate all user inputs (using do-while loops). Use a while loop for the main structure of the program that will stop accepting letter grades when the user inputs X.

im trying to make the if statement and Boolean variable so i can make the whole thing a while loop then have the error checking part a small do while that will exit that part when input is vaild then enter the switch statement. if i could use the switch statement to error check but i cant (professor doesn't want it.) talking to her she also says theres a do while loop at the bottom but what that one will do i dont know,

they also give me this tip,

The easiest way to approach this problem is to first implement the while loop. You will need to accept input to initialize the LCV prior to entering the while loop and then again accept input at the end of the while loop to update the LCV. Once you have completed the while loop and it running properly implement the do-while loops (in place of the inputs) to validate the input. Note, you will need to do this twice  once where you initialize the LCV and again where you change the LCV. DO NOT JUST CHECK FOR G  your program should be able to handle any character that is not valid.

maybe this will make it clearer.
closed account (z05DSL3A)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
    bool done = false;
    while(!done)
    {
        bool inputValid = false;
        do
        {
            //get input
            //if valid (A,B,C,D,F, OR X)
            //then inputValid = true

        }while(!inputValid);

        switch(grade)
        {
            //...
            case 'X':
                done = true;
                break;
            //...
        }
    }
    //display the results 
Last edited on
thanks grey wolf ill try that
now i get a infinite loop im sure its the trues and false's some where but idk which ones. and what loop is infinite. this output is what i get if i put invalid letters, also it didnt check the second one, but if i input normal inputs it infinite loops.


Enter letter grade (Enter 'X' to exit) a
Enter letter grade (Enter 'X' to exit) a
Enter letter grade (Enter 'X' to exit) j

Invalid letter grade, please try again

Enter letter grade (Enter 'X' to exit) j
Enter letter grade (Enter 'X' to exit) x
Enter letter grade (Enter 'X' to exit) x
Total Grade Points: 0
GPA: 0.00


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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <iostream>
#include <iomanip>

using namespace std;

int main ()
{
	char grade;
	int gradePoints = 0;
	int inputCounter = 0;
	float gpa = 0;
	bool invaildInput = false;
	bool done = false;

	while (!done)
	{
		do
		{

		cout << "Enter letter grade (Enter 'X' to exit) ";
		cin >> grade;
		grade = toupper(grade);

		if ((grade != 'A') && (grade != 'B') && (grade != 'C')
				&& (grade != 'D')&& (grade != 'F') && (grade != 'X'))
		{
			cout << "\nInvalid letter grade, please try again\n\n";
			invaildInput = true;
		}
		}
			while (!invaildInput);

		inputCounter += 1;

		switch (grade)
		{
			case 'A':
				gradePoints +=  4;
				break;
			case 'B':
				gradePoints += 3;
				break;
			case 'C':
				gradePoints += 2;
				break;
			case 'D':
				gradePoints += 1;
				break;
			case 'F':
				break;
			case'X':
				done = true;
				inputCounter -= 1;
				break;
			}

		cout << "Enter letter grade (Enter 'X' to exit) ";
				cin >> grade;
				grade = toupper(grade);

	}


	cout << setprecision(2) <<  fixed;

	gpa = gradePoints / float(inputCounter);


	cout << "Total Grade Points: " << gradePoints << endl;
	cout << "GPA: " << gpa;


	return 0;

}
I commented out my second input section, and that worked fine with vaild inputs, but once i put invaild it infinite loops, so im guessing its my top do-while loop.
i fixed it with a if else at top to convert it back to false. thanks everyone for all your help!
Topic archived. No new replies allowed.