do while looping problems

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
#include <iostream>
using namespace std;
int main()
{
	double gpa = 0;
	int exam = 0;
	
	cout.setf (ios::fixed) ;
	cout.setf (ios::showpoint) ;
	cout.precision (1) ; // lines 14-16 fix decimal to 1 place
	
	do {
		cout << "Please enter your GPA (0.0 exits program) :" ; // ask for GPA
		cin >> gpa ; // get user input for GPA
		if (gpa == 0.0) {return 0;} // exits program if user enters 0.0 as GPA
	    if (gpa < 0.0 && gpa > 4.0) cout << "You've entered an invalid GPA or entrance score. " <<endl;   // ask for user input again if not valid
	 	else if (gpa > 0.0 && gpa <= 4.0) {cout << "Thank you" << endl; }// check that user input is in required range for GPA

		cout << "Please enter your entrance score :" ; // ask for entrance score
		cin >> exam ; // get user input for entrance score
		if (exam >=40 && exam <=44) cout << "Congratulations!  You are hereby admitted to ABC Medical University";
			else if (exam <1 && exam >44) cout <<"You've entered an invalid GPA or entrance score. "; continue ; // ask for user input again if not valid
		if (gpa >=3.7 && exam >=32) "Congratulations!  You are hereby admitted to ABC Medical University"; //check for admission requirements
			else if (gpa <3.7 && exam <32) cout << "We are sorry!  You are hereby denied admission to ABC Medical University.";
	}
	while (gpa != 0.0);
{
}
return 0;
}
Last edited on
I can get it to exit if I enter 0.0, but then it skips to asking for the entrance score without executing the other conditions. Any help would be greatly appreciated.
I spaced out the code and added some comments below.

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
int main()
{
    double gpa = 0;
    int exam = 0;
    
    cout.setf (ios::fixed) ;
    cout.setf (ios::showpoint) ;
    cout.precision (1) ; 
    
    do {
        cout << "Please enter your GPA (0.0 exits program) :" ; 
        cin >> gpa ; 

        if (gpa == 0.0) {
            return 0; 
        } 

        if (gpa < 0.0 && gpa > 4.0) // Check this: how can a number fit both conditions?
            cout << "You've entered an invalid GPA or entrance score. " <<endl;   
        else if (gpa > 0.0 && gpa <= 4.0) { 
            cout << "Thank you" << endl; 
        }

        cout << "Please enter your entrance score :" ; 
        cin >> exam ;

        if (exam >=40 && exam <=44) 
            cout << "Congratulations!  You are hereby admitted to ABC Medical University";
        else if (exam <1 && exam >44) // Check this: how can a number fit both conditions?
            cout <<"You've entered an invalid GPA or entrance score. "; 

        continue ; // This is sitting out on its own. Should it be part of else if above?

        if (gpa >=3.7 && exam >=32) 
            cout << "Congratulations!  You are hereby admitted to ABC Medical University"; //missing cout
        else if (gpa <3.7 && exam <32) 
            cout << "We are sorry!  You are hereby denied admission to ABC Medical University.";
    } while (gpa != 0.0);

    return 0;
}
Thanks for pointing out my error on line 18. I guess it should be
 
if (gpa < 0.0 || gpa > 4.0)


on line 32 I wanted the
 
continue;

statement to go back to the beginning of the do while loop to ask for input again. Is that correct or is there a better way to accomplish this? Thanks for your help again.
With the continue sitting out on its own - it's going to run in all cases, so the code below it will be skipped.

There are different ways to approach this. You could use a short while loop to keep prompting the user for valid data on the gpa and exam. Then, with valid data, decide whether they're admitted.

while (gpa < 0.0 || gpa > 4.0)
// display invalid gpa error
// reprompt entry

while (exam <1 || exam > 44)
// display invalid exam error
// reprompt entry

if // high exam score
// congratulations
else if // not as high exam score but high gpa
// congratulations
else
// sorry
Topic archived. No new replies allowed.