Alright, I got the code all set and it works perfectly. I just want to know if I did it the best way or if I'm missing some shortcuts. This is just a practice program learning how to use counters, loops and stuff. I'll leave the description of what I was trying to accomplish below
Program:
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.
int main()
{
int x = 0;
int timesAsked = 0;
// ask user to enter a number thats not correlated to the number of times they have been asked
do
{
cout << "Please enter a number other than " << (timesAsked + 1) << "." << endl;
cin >> x;
timesAsked++;
if (x == timesAsked)
{
cout << "You entered the number " << timesAsked << ". " << "Program will end" << endl;
return 0;
}
} while (x != timesAsked);
}
I'd move the contents of that inner if statement outside after the while loop. As it is, the if statement's condition and the while loop's condition are redundant with each other (the only time you will not loop, you will have entered the if statement and exited anyway).
Otherwise, I can't really think of anything except for the fact that your program will start at 1 instead of 0. ;)
ahhh thanks ! I have it start with 1 because of where the counter is. If I entered 0 on my first time it would say you entered the number 0 , program will end