I just want to use a loop sort of like this, but if you entered a char it goes wild. How do I account for characters being entered as well?
Code:while(monster!=1||2||3||4){ cout<<"1-man\n"; cout<<"2-goblin\n"; cin>>monster; }
I just want to use a loop sort of like this, but if you entered a char it goes wild. How do I account for characters being entered as well?
Code:while(monster!=1||2||3||4){ cout<<"1-man\n"; cout<<"2-goblin\n"; cin>>monster; }
My computer is awesome.
Remember the FAQ
http://faq.cprogramming.com/cgi-bin/...&id=1043284392
I don't really see the logic behind this line of code. I've never seen a loop used like that.
Could you please explain this line? I know what it does, but it doesn't make sense.Code:while (cout << "Enter a number: " && !(cin >> input))
My computer is awesome.
What logic? The code indicates to output the question each time the user's input is invalid.
Kuphryn
What your code is really checking is:Originally Posted by cerin
Perhaps you should repahse that if statement...Code:while(monster!=(BOOL) TRUE){ cout<<"1-man\n"; cout<<"2-goblin\n"; cin>>monster; }
Actually it's:
However, you're correct that the truth test needs to be redone. (There is no if statement however, but I know what you meant. )Code:while( monster != 1 || (non-zero is true, so true) )
In case you, the origional poster, don't know what we're getting at: You cannot test one variable against multiple values with a single statement like that. You have to repeatedly compare the variable to each new value. Therefore, for your example to be correct, it would need to be:
Or, you could write it like so:Code:while( monster != 1 && monster != 2 && monster != 3 && monster != 4 ) { ...stuff... }
Quzah.Code:while( monster < 1 || monster > 4 ) { ...do stuff... }
Hope is the first step on the road to disappointment.
^ DeMorgans law, simply...boolean algebra.
after distributing, "or" becomes its opposite "and"Code:not (1 or 2 or 3 or 4)
using your example yeildsCode:(not 1) and (not 2) and (not 3) and (not 4)
Code:((monster != 1) && (monster != 2) && (monster != 3) && (monster != 4 ))
You can't use the >> operator, i.e cin>>monster. That requires that you know that the input is the same type as monster. If the user may enter a different type than monster, then you need to read in the input as a string with getline():How do I account for characters being entered as well?
Then, you can use atoi() in <cstdlib> to convert the string to a number(alpha to int). atoi() returns 0 if there isn't a leading integer in the string, otherwise it returns the integer. So, you can check for 0 which is a signal for bad input before continuing.Code:char input[30] cin.getline(input, 30);
I've never seen it before, but after doing some reading, I think I understand it.I don't really see the logic behind this line of code. I've never seen a loop used like that.
Code:while (cout << "Enter a number: " && !(cin >> input))
The <<operator returns a reference to the stream cout, a reference is a pointer which stores an address, and an address is a non-zero integer, so it evaluates to true. By the same logic, the >>operator returns a reference to the stream cin, which is an address, and an address is non-zero, so it evaluates to true. Since !true is false, the result is:
true && false
which evaluates to false. So, it seems like the while loop will never execute.
However, the !operator is overloaded for stream objects, and it is programmed to check the status of the stream. If an input operation did not read the characters that were expected, then an error flag is set for the stream, and any subsequent attempts to read from the stream will fail. The !operator checks for that error flag, and returns true if it finds it, meaning there is an error. But, as long as the input matches the type of the input variable, no error flag gets set, and the !operator returns false--signaling it couldn't find an error flag, which results in the conditional:
true && false
That evaluates to false, and the while loop with the error message doesn't execute. But, if the wrong type of input is entered, then an error flag gets set for the cin stream, and the !operator finds it and returns true, which results in the conditional:
true && true
That evaluates to true, which causes the while loop error statements to execute. Those statements display an error message, reset the error flag, and remove the bad input from the stream.
Last edited by 7stud; 04-02-2005 at 04:59 AM.