Okay so I'm trying to do one of the beginner programs which tells a user not to type a certain number. The user then types a number and if it isn't that certain number they have to try again. They do this until they actually type the number they were told not to type. Here is what I have so far and I face two problems: The very first input the user gives, for some reason isn't registering or something.. second, when the for loop is ran for the tenth time, the program terminates. This is fine but I also wanted it to print a message on screen before it terminated.. Please help and sorry that I'm such a newbie!
The reason your first cin isn't working is because you are asking the user to enter a number in for input on line 9, then you are asking them again immediately as the loop starts, which will erase whatever the first input value was without getting a chance to use it. The easiest solution to this is to erase the cin on line 9 and just count the ones in the loop.
I am not sure why you are using a for loop for an example like this. The problem wants you to run the program until the user enters in the correct value, in this case it will only run 10 times then just give up. I suggest you look into do/do-while loops for it unless you want it like this.
As for printing a line at the end of the program, you can just put a cout after the end of the for loop, and it will print it out once they have finished entering numbers.
1. You tell the user to input something on line 9, but then he has to again on line 13. That's why the first time won't do anything.
2.Just cout<< something after your for loop:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
for (int i = 1; i != 10; i++)
{
cin >> input;
if (input != 5)
{
cout << "Try Again.\n";
}
elseif (input == 5)
{
cout << "Disobedience is Frowned Upon.";
}
}
std::cout<<"Whatever";