Can't exit a do/while loop - please help
Hello again,
I was wondering if anyone might be able to tell me why the program won't exit the loop when response equals 'q' or 'Q'. When I select a Q or a q, the program just loops contiuously. Any help would be appreciated.
Thanks
Vireyda
[CODE]
do{
printf("Please select a transaction:\n");
printf("Cheque - C or c\nDeposit - D or d\nPrint - P or p\nQuit - Q or q\n");
fflush(stdin);
scanf("%c", &response);
switch(response)
{case'c':case'C':
Cheque(balance);
break;
case 'd': case'D':
Deposit(balance);
break;
case 'p': case'P':
Print(balance);
break;
case 'q': case'Q':
break;
default:
printf("That is an invalid selection. Please try again.\n ");
}
}while(response!='q'||response!='Q');
[\CODE]
Look at the logic in your while condition
Code:
while(response!='q'||response!='Q');
if you look at the logic of the statment
(response!='q') is true(1) when any other letter is entered and false(0) when you enter 'q'
(response!='Q') is true(1) when any other letter is entered and false(0) when you enter 'Q'
one is always going to be true and the other false when you try to quit
when you OR 1 and 0 you get 1 which is what you are doing so it evaluates to while(1) which is an infinite l oop
when you AND 1 and 0 you get 0 which will break the loop and exit so just change the || to && and you should be alright.