Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

While loop not breaking?? C++

I’m trying to construct a simple dice game in C++. I do not understand why it’s not breaking out of the while loop.

You are supposed to ONLY be able to bet 100, 300, or 500€.

Even if I enter my bet "100-300-500" which is supposed to be correct. It still loops and says it’s an invalid bet. Why doesn’t it progress to the if statement when I have if ((bet1 == 100) || (bet1 ==300) || (bet1==500))

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I’ve tried putting the if statement before the while loop and all sorts of things. I can’t get it to work.

cout << "Please place a bet. 100, 300, 500kr" << endl;
cin >> bet1;

while ((bet1 <= 99) || (bet1 >= 101) || (bet1 <= 299) || (bet1 >= 301) || (bet1 <= 499) || (bet1 >= 501)) {
    cout << "Please place a valid bet" << endl;
    cin >> bet1;
}
if ((bet1 == 100) || (bet1 ==300) || (bet1==500)) {
    cout << "You have deposited " << " " << bet1 << "" << "And you now have: "<< saldo - bet1 << " " << "Remaining on your account." << endl;
}

>Solution :

Any finite integer value is either >=101 or <=299. Oring more conditions does not change that the condition is always true.

If you want to loop until bet1 is either 100,300 or 500:

while ( bet1 != 100 && bet1 != 300 && bet1 != 500) {...
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading