+ 1

C++ Practice question

Hey everyone! Im working on this practice problem: Requires: variables, data types, and numerical operators basic input/output logic (if statements, switch statements) loops (for, while, do-while) Write a program that ccontinues to asks 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. ★★ Modify the program so that it asks the user to enter any number other than the number equal to the number of times they've been asked to enter a number. (i.e on the first iteration "Please enter any number other than 0" and on the second iteration "Please enter any number other than 1"m etc. etc. The program must behave accordingly exiting when the user enters the number they were asked not to.) So far I have everything operational until the two star. When I run the program it stops after 10 loops and the number not to enter increases by one each time but even if you enter the number it does not end. Here is my code not sure why it is not working. Thanks for help! #include <iostream> using namespace std; int main() { int x; int y = 0; int run = 1; while ((x != y) && (run <=10)) { cout << "Please enter a number other than " << y << endl; cin >> x; ++run; y++; } if (x == y) { cout << "You lose" << endl; } else { cout << "You win" << endl; } return 0; }

24th Jun 2017, 2:14 AM
Chris Karras
Chris Karras - avatar
6 Respostas
+ 1
@Rrestoring faith is on the right path, but I would get rid of the run variable all together (less code to change) and change your variables to: int x = 0; int y = 0; Then change the while conditional to: while ((x != y-1) && (y < 10)) If you change your prompt to: cout << "Please enter a number other than " << y << ": "; It reads a little better when output IMO. Then all that's left is to alter your if statement to handle the fact that y++ makes it 1 too big by changing it to: if (x == y-1) At this point I think you'll get your desired output. #include <iostream> using namespace std; int main() { int x = 0; int y = 0; while ((x != y-1) && (y < 10)) { cout << "Please enter a number other than " << y << ": "; cin >> x; y++; } if (x == y-1) { cout << "You lose" << endl; } else { cout << "You win" << endl; } return 0; }
24th Jun 2017, 3:28 AM
ChaoticDawg
ChaoticDawg - avatar
+ 3
at first, the value of y = 0 then it enter the while loop you input value of x, let's say 0. so at this point it should stop. but it wouldn't. why? because the value of y is changed, so the value of y is no longer 0, but 1 after that, the loop check it's condition (x != y), which is true because x = 0 and y = 1 my advice: 1. change the initial value of y to -1, then in cout print y + 1 2. instead of put (x!=y) in your while condition, use break
24th Jun 2017, 2:45 AM
M Rizky Widyayulianto
+ 3
I recommend using the run variable instead of y. y essentially does the same thing. (Aka: Removing y all together)
24th Jun 2017, 2:50 AM
Rrestoring faith
Rrestoring faith - avatar
+ 1
I realize that and when you pass 10 it outputs "you win" I am trying to get it to say you lose when you enter y. At the moment if you enter y + 1 it says you lose but not y
24th Jun 2017, 2:36 AM
Chris Karras
Chris Karras - avatar
+ 1
The given program outputs "C++". Change the code to output each character on a new line, resulting in: C + +
3rd Oct 2021, 9:01 PM
Adrian Czepułkowski
Adrian Czepułkowski - avatar
0
it's because your while condition (x!=y) && (run<=10) if value of run > 10, it's no longer fulfill the while condition
24th Jun 2017, 2:24 AM
M Rizky Widyayulianto