[code]
Code:while(bread_type!='w && bread_type!='W') { printf("Please enter a valid character"); }
Basically what happens next is my program "crashes" and prints "Please enter a valid char" infinitely.
[code]
Code:while(bread_type!='w && bread_type!='W') { printf("Please enter a valid character"); }
Basically what happens next is my program "crashes" and prints "Please enter a valid char" infinitely.
Last edited by tmac619619; 10-22-2012 at 11:48 PM.
You're telling them to enter a valid character and then you're closing the program anyway?
You could use exit(1), beneath the printf() statement, but it sounds wrong to end a program right after requesting a char.
Oh right. So i should use a loop to keep prompting the user to enter the correctchar right?
Uhmm basically i want to keep prompting the user till they enter the correct character.Code:while(bread_type!='w && bread_type!='W') { printf("Please enter a valid character"); }
but my program crashes.. in an endless "Please enter a valid character" loop
Last edited by tmac619619; 10-22-2012 at 11:43 PM.
Take a long hard look at your if statement. It can never be false since everything is either not 'w' or not 'W'.
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"
A while() loop is a bit awkward for this. Consider a do while loop:
The difference is the naturalness of a test at the end of the loop, instead of at the beginning. This is when the loop will always be entered, at least once. A while loop may or may not be entered.Code:do { printf("Please enter a valid character [w,d,r]: "); scanf(" %c",&mychar); mychar=tolower(mychar); }while(mychar != 'w' && mychar != 'd' && mychar != 'r');