-6

trying to a loop until the user enters 0.0 to terminate the loop. however i get an error. The user should enter the gpa and score until he is done to terminate. Any help?

#include <iostream>

using namespace std;

int main() {
    double gpa; // gets the double entered by the user
    int score; // gets to store the score
    bool done = false;

    // statements to print to the user

    while (!done) {
        cout << "Please enter your GPA(enter 0.0 to end): ";
        cin >> gpa;
        cout << "Please enter your entrance score: ";
        cin >> score;

        // the if statements
        if (gpa >= 3.7 && score >= 32) {
            cout << "Congratulations!. You are hereby admitted to ABC Medical University";
        }
        else if (gpa < 3.7) {
        cout << "you are denied";
        }
        else if (gpa == 0.0)
            done = true;
    }// end while loop
    return  0;
}
Pete Becker
  • 74,985
  • 8
  • 76
  • 165
  • 3
    Welcome to Stack Overflow! Please consider taking the short introductory [tour]. Please look at your post. Does it look like you intended it to? (I hope not.) To properly format your code, just select all of it and click the "`{}`" Code button (which will indent all of the code by 4 spaces, per Markdown rules). Also, when mentioning an error, *include what it says*. – Jongware Sep 04 '16 at 21:58
  • @PeteBecker: No worries (although you could have fixed more – OP might need to look at [Advice for non-native English speakers](http://meta.stackoverflow.com/questions/291362/advice-for-non-native-english-speakers) :). But mainly I'm hoping for a clarification on the "however i get an error" [sic] part. – Jongware Sep 04 '16 at 22:01

5 Answers5

1

The else if (gpa < 3.7) satisfies the condition so the next else if doesn't process.

Perhaps change the line to else if (gpa < 3.7 && gpa > 0.0)

Bert
  • 2,134
  • 19
  • 19
0
else if (gpa < 3.7) {
    cout << "you are denied";
}
else if (gpa == 0.0)
     done = true;

0.0 is smaler then 3.7. So reordering your if's:

else if (gpa == 0.0)
     done = true;
else if (gpa < 3.7) {
    cout << "you are denied";
}

will fix it.

Stefan
  • 17,448
  • 11
  • 60
  • 79
0

You can use a break statement to Exit the loop.

0

else
if (gpa == 0.0) break;

0
else if (gpa < 3.7) // this condition hides condition below because any value smaller than 3.7 even 0.0 will succeed this condition so any else block will be skipped
{
    cout << "you are denied";
}
else if (gpa == 0.0) // this condition will never be executed because 0.0 is smaller than 3.7 which it is covered by the above condition.
           done = true;

the correct form as your code:

swap the conditions:

else if (gpa == 0.0)
         done = true;
else if (gpa < 3.7) 
{
    cout << "you are denied";
}

I think a good thing to do give priority to this loop-exit condition by putting the first condition:

while (1) 
{
    cout << "Please enter your GPA(enter 0.0 to end): ";
    cin >> gpa;

    else if (gpa == 0.0) // high priority so if the user enters 0.0 no need to continue
        done = true;

    if(done)
        break; // exiting quickly and rightously

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

    // the if statements
    if (gpa >= 3.7 && score >= 32) {
        cout << "Congratulations!. You are hereby admitted to ABC Medical University";
    }
    else if (gpa < 3.7)
    {
            cout << "you are denied";
    }

}// end while loop
Raindrop7
  • 3,889
  • 3
  • 16
  • 27