The problem depends on your condition:
} while((temp!='x')||(temp!='X'));
This means: Repeat until it is equal to "x" OR "X". (This is always true because it can only be one or the other. Replace it as follows:
} while(temp != 'x' && temp != 'X');
Repeat until it is “x” or “X”. The same problem is in your if further (I seem to have missed this for the first time, my bad one).
Your code can be restored as follows:
int main(){ chu input; char temp; while(true) { printf("\nInput: "); temp=getche(); if(temp != 'x' && temp != 'X') printf("\nWrong input (only 'x' or 'X')"); else { input.c=temp; break; } } }
This code will be looped forever (as indicated by while(true) ), but as soon as you enter 'x' or 'X', it will set the variable to your structure and break free from your loop.
Some information about semantics:
Everything related to OR ( || ) ceases to be evaluated as soon as the first condition returns true . At this point, the whole construct will return true , no matter what.
On the contrary, everything that you associate with AND ( && ) stops evaluating as soon as the first condition returns false , since it makes the whole construct false anyway.
To get back to your example, this is what your compiler does, assuming we enter "X"
if(temp != 'x') { //Okay, it not 'x'. Let try the next one. if(temp != 'X') { //Oh wait, it IS X. I can't stop yet! //Stop looping } //This will be executed } //Loop me!
And this is what he does with &&
if(temp == 'x') { //Hmm...not 'x'. if(temp == 'X') { //Oh wait! It IS 'X'. //Stop looping //This gets executed. } } //Loop me!
You may have noticed that I changed != To == here. I can do this due to binary arithmetic. NOT (x OR X) equals (NOT x AND NOT X).