| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Little bit of help please [problem with 'while' loop]
Hey, im bulding a program for an assigment which converts numbers to roman nurmerals, i have it all working except at the begining where the user inputs which mode he wants to run the program in.
Code:
void main(){
char answer;
printf("\nWelcome to Roman numeral convertor by Mike Pledge!\n\nWould you like to run this application in (B)atch or (I)nput mode??\n\n");
scanf("%c%*c", &answer);
while(answer != 'B' || answer != 'b' || answer != 'i' || answer != 'I')
{
printf("\nPlease make sure you enter either B for Batch or I for Input\n\n");
scanf("%c%*c", &answer); }
if(answer == 'I' || answer == 'i')
input();
if(answer == 'B' || answer == 'b')
batch(); }
the while loop doesn't seem to work, and will just keep running whatever you put in, however if i change it to : Code:
while(answer != 'B' ) it works fine (when i enter B) Any help will be great, Thanks |
|
#3
|
|||
|
|||
|
the problem with changing it to && is that it will look to satisfy all the requirments, i only need one of the requirments to be true for it to break the loop
|
|
#4
|
||||
|
||||
|
The loop will continue as long as the whole condition is true. Your condition is always true. Did you try using &&?
|
|
#5
|
|||
|
|||
|
you were right, can i ask somthing though, i was taught that using && with multiple conditions meant all of them had to be satisfied, but while using || meant only one of them had to be true?
but either way thank you soo much for your help ![]() |
|
#6
|
||||
|
||||
|
Yes that is right. In your case each condition is an inequality, so they are sateisfieds when the operands are not equal. You could also have rewritten the while like this:
Code:
while( ! (answer == 'B' || answer == 'b' || answer == 'i' || answer == 'I') ) ... The negation now applies to the whole expression. |
|
#7
|
|||
|
|||
|
ahhh lol i never thought of it like that, seems it can be a bit of a pit trap, i will be more aware for the future
again thank you for all your help ![]() |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Little bit of help please [problem with 'while' loop] |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|