Reputation: 9
I'm doing c++ practice exercises. The one question that I'm on is as follows...
Write a program that continues to ask the user to enter any number other than 5 until the user enters the number 5. Then tell the user "Hey! you weren't supposed to enter 5!" and exit the program.
★ Modify the program so that after 10 iterations if the user still hasn't entered 5 will tell the user "Wow, you're more patient then I am, you win." and exit.
#include <iostream>
using namespace std;
int main()
{
int userNum;
int numTimes = 0;
do
{
cout << "Please enter a number: ";
cin >> userNum;
numTimes++;
}while ((numTimes != 10) || (userNum != 5));
{
if (numTimes == 10)
{
cout << "Wow, you're more patient than I am, you win.\n";
}
else
{
cout << "Hey! you weren't supposed to enter 5!\n";
}
}
}
I can't seem to get the or condition of the while loop to work.
Upvotes: 0
Views: 559
Reputation: 8126
You are doing this in a confusing way, I would aim for expressiveness:
#include <iostream>
int main() {
int userNum;
int numTimes = 0;
while (true) {
std::cout << "Please enter a number: ";
std::cin >> userNum;
if (userNum == 5) {
std::cout << "Hey! you weren't supposed to enter 5!\n";
break;
}
++numTimes;
if (numTimes == 10) {
std::cout << "Wow, you're more patient than I am, you win.\n";
break;
}
}
}
break
allows you to conveniently exit a loop. Please don't "abuse" this to overcomplicate or to accomplish weird tricks, though. No one likes spaghetti code.
Note how you shouldn't use namespace std;
. Also try to use ++var
over var++
as var++
unnecessarily copies the object.
Please enter a number: 5 Hey! you weren't supposed to enter 5!
Please enter a number: 12 Please enter a number: 45 Please enter a number: 12 Please enter a number: 0 Please enter a number: 9 Please enter a number: 1 Please enter a number: 2 Please enter a number: 3 Please enter a number: 4 Please enter a number: 1 Wow, you're more patient than I am, you win.
Upvotes: 1
Reputation: 16876
Change this here
while ((numTimes != 10) || (userNum != 5));
To this
while ((numTimes != 10) && (userNum != 5));
Because you want it to stop as soon as just one condition turns false
. The way you have it now with ||
, it only stops if both conditions are true at the same time, so if the 10th number is 5
. It won't stop if just a 5
is entered, or if it just reached 10. It wouldn't even stop if a 5
is entered after 10 numbers.
Remember, a || b
is true
even if just one of a
and b
are true. But since the conditional is confusing this way, why not split the logic like this?
int main()
{
int userNum;
for (int numTimes = 0; numTimes < 10; numTimes++){
std::cout << "Please enter a number: ";
std::cin >> userNum;
if (userNum == 5) {
std::cout << "Hey! you weren't supposed to enter 5!\n";
return 0;
}
}
std::cout << "Wow, you're more patient than I am, you win.\n";
}
Upvotes: 1
Reputation: 490
The problem is with your condition. Your loop continues until the condition isnt met anymore.
while ((numTimes != 10) || (userNum != 5));
Forces the loop to continue until both conditions are false. If your numTimes == 10, then the userNum will be still < 5
while ((numTimes < 10) and (userNum != 5));
Is the correct condition
Upvotes: 0