Wrong logic in do-while statement? - c

I must tell you that i' ve already searched thi site and other, but i couldn't solve my problem. I'm doing a program that works on some lists. The code copies the values form the second list to the first, with this condition: the current value of list 2 must be lesser than the value in list 1. If this condition is true, the value is copied. But that's not the point. I made a function called GetExitValue(), which at the end of main, gets the input from the user whether to continue or not. And this is the problem. When i call the function in main (inside of a while) , it continues to run the program even if i insert n (No). Can you tell me what am i doing wrong?? Thank you very much guys!!
And that's my code semplified.
int main(){
do{
.
.
.
.
printf("\nProgram finished. Do you wish to re-execute the program? \n\nPress y/Y (Yes) to continue. Press n/N (No) to exit. ");
}
while(GetExitValue() == 'y' || 'Y' ); // **Problem here!!**
return 0;
}
char GetExitValue(){
char exit_value;
scanf(" %c", &exit_value);
while(exit_value != 'y' && exit_value != 'n' && exit_value != 'Y' && exit_value != 'N'){
printf("Value incorrect. insert y or n!!\n");
scanf(" %c", &exit_value);
}
return exit_value;
}

This part:
while(GetExitValue() == 'y' || 'Y' ); // **Problem here!!**
which you've correctly spotted, is probably not doing what you meant. This always is "true" because 'Y' (which is not 0 in ASCII) is always "true".
You literally have this in your code: while (condition || 1) which can be useful, but not in this case.
You probably mean this:
.
.
// run GetExitValue() only once...
char gev = GetExitValue();
}
while(gev == 'y' || gev == 'Y' );
And I'll just point out that you can do something like:
.
.
}
while(GetExitValue()); // no need to store value!
if only you'd change GetExitValue() to return 0 when user does not want it to run again:
char GetExitValue(){
char exit_value;
scanf(" %c", &exit_value);
while(exit_value != 'y' && exit_value != 'n' && exit_value != 'Y' && exit_value != 'N'){
printf("Value incorrect. insert y or n!!\n");
scanf(" %c", &exit_value);
}
return exit_value == 'Y' || exit_value == 'y'; //evaluates to 1 if true
}
It will probably be cleaner, and you can change it's name to shouldRunAgain() which to my personal taste is more informative :)

The while expression is evaluated as:
while( ( GetExitValue() == 'y' ) || 'Y' );
So the expression is always true because anything or true == true.

Your problem is here-
while(GetExitValue() == 'y' || 'Y' );
The 'Y' part will always be true, because in C, anything that isn't a zero(false) is true.
the ASCII value of 'Y' isn't zero, therefore, the while condition will always return true.
What you should do is this-
`char x=GetExitValue();
while(x == 'y' || x=='Y' );`

Related

C - how to exclude other inputs in a y/n loop

So I am trying to get a simple y/n input to work. I have it working and it will break a while(1) loop when n or N is entered, however, any other alphabetical character will set the loop to go again. I only want it to loop when Y is entered.
I have tried:
if (try_another != 'n' || try_another != 'N' || try_another != 'y' || try_another != 'Y'), after the !isalpha line, and this did not work.
I have tried scanf (" %c", try_another); and then comparing the assigned characters.
this is my current code set up:
printf("Do you wish to try another problem [y/n]: ");
do{
try_another = getchar();
/*keeps scanning for input until its a letter*/
}while(!isalpha(try_another));
/*when input is n or N it will end the program*/
if (try_another == 'n' || try_another == 'N'){
break;
}
Why don't you do this?
do {
try_another = getchar();
} while(try_another != 'n' && try_another != 'N' && try_another != 'y' && try_another != 'Y');
You want to loop while the character isn't this and isn't that. You were close, but accidentally wrote "or" instead of "and".

Infinite looping,c logical thinking

I'm facing a problem with what I enter with any unknown during the first time to the program. it will show me an infinite loop problem program closing. The program won't read the else statement.
char cont;
printf("Do u want continue\n");
scanf("%c", &cont);
getchar();
do
{
if (cont == 'y' || cont == 'Y')
{
selection();
}
else if (cont != 'n' || cont != 'N')
{
printf("Program Closing \n");
}
else
{
printf("Invalid Please Re-enter");
getchar();
scanf("%c", &cont);
}
} while (cont != 'n'&& cont != 'N');
let's dissect your code line by line starting with
scanf("%c", &cont);
This line would get a char value from stdin and put it into cont, which is a char so that's fine
getchar();
All I have to say for this is, why? it doesn't do anything useful, remove it.
Entering the loop now we have this statement
if (cont == 'y' || cont == 'Y')
this line is correct, it checks if the character is equal to y or Y
else if (cont != 'n' || cont != 'N')
this line is the main issue, your statement checks if cont is a value NOT equal to n or N, i.e. as a comment mentioned above, if the user put in the value a this line would return true, and then end the program. To correctly check if the user wants to exist you can use the same if statement used for y
if (cont == 'n' || cont == 'N')
if you replace the original if statement with this your program should work as expected. Just remember in the future that the != means not equal to, i.e. if the value is anything besides n or N return true. The == operator checks for equality as you saw above, so the line cont == 'n' means return true if cont is the same value as 'n'
printf("Invalid Please Re-enter");
getchar();
scanf("%c", &cont);
also as an extra note, please explain why you keep throwing in useless getchar()'s, those lines literally do nothing and you should remove them.

unwanted result with loops in c

In the following code no matter what I do it loops forever and showing "syntax error" forever.
This code needs to check if the answer is neither 'y' nor 'n'. If it is ask again for answer and then if answer 'y' it will loop again all the func
and if answer is 'n' it should do nothing...
char answer = ' ';
int round = 0;
do
{
round = chooseLevel();
guess(round);
while ((answer != 'y') || (answer != 'n'))
{
printf("\nWould you like to play again? (y/n): ");
answer = getch();
putch(answer );
printf ("\nchar is %c\n", answer );
if ((answer != 'y') || (answer != 'n'))
{
printf("Syntax Error");
}
}
}
while (answer == 'y');
The condition should be with && and not || :
(answer != 'y') && (answer != 'n')
The answer cannot be both 'y' and 'n', so either answer != 'y' or answer != 'n' will be true. In case of || the whole statement will become true.
True || False => True
Switch || to &&. Currently, if answer is y, both if and while conditions will evaluate to ('y' != 'y') || ('y' != 'n') or (false) || (true) = true. The only way to get out of the while loop is to have answer somehow equal to both y and n at the same time.

How to use loops in terms of input (in C language)?

I've been trying to get this code to work but the loop does not seem to work? I am very new to C and I sort of get confused with the syntax of this language. However my loop is not functioning like how I want it to be. I want the if and else statement to work but no matter what input (right or wrong) it always outputs "thank you".
#include <stdio.h>
#include <stdlib.h>
int confirm()
{
char c;
printf("Confirm (y/n): ");
scanf("%c", &c);
while (scanf("%c", &c))
{
if (c = 'Y' && 'y' && 'N' && 'n')
{
printf("\nthank you");
break;
}
else
{
printf("\nInput not recognised, try again. \n");
printf("\nConfirm (y/n): ");
scanf("%c", &c);
}
}
}
int main(int argc, char* agrv[])
{
confirm();
return 0;
}
it won't ask to enter another output when the output is incorrect. It just keeps ending from the if statement, thus the loop is not running?
Please help.
There's nothing wrong with your loop - it's the if statement that's wrong.
This code compiles, but it does not do what you want it to do:
if (c = 'Y' && 'y' && 'N' && 'n')
= is an assignment; you need == to do a comparison
&& means "AND"; you need ||, which means an "OR"
You combine logical expressions, not constants with && or ||
The condition should be
if (c == 'Y' || c == 'y' || c == 'N' || c == 'n')
Also note that when you read single characters with %c, your program "sees" all characters, including whitespace. This is a problem, because the '\n' left over in the buffer will be passed to your program before Y or N. To fix this, add a space before %c to your format string:
scanf(" %c", &c)
// ^
// |
// Here
Your code also ignores the first character that it reads. I think this is not intentional, so remove the call of scanf before the loop. You should also remove the second scanf from the loop, leaving the only call to scanf in the loop header.
int confirm()
{
char c;
printf("Confirm (y/n): ");
//scanf("%c", &c);// <---------- needless
while (scanf("%c", &c)) //<----while loop will do `scanf("%c",&c)`, so previous line should be remove.
{
if (c == 'Y' || c == 'y' || c == 'N' || c == 'n')// <- &&(AND); ||(OR). Also, be careful that don't be lazy, [c == 'Y' || 'y' || 'N' || 'n'] can't to communicate with computer
{
printf("\nthank you");
break;
}
else
{
printf("\nInput not recognised, try again. \n");
printf("\nConfirm (y/n): ");
scanf("%c", &c);
}
}
}

Error Checking In C

I'm not as advance in C as yet so I'd need help with few problems. 1. Let's say I want to enter character (y or n) and I don't want anything else other than that character, so I'll use a while loop to until its entered. I can't get it working, here's my code. It compiles and run but it won't carry out what I want, if I enter y it continues to loop likewise n
printf("Enter code(y/n): \n");
scanf(" %c", &code);
while (code != 'y' || 'n' ){
printf("Try Again: \n");
scanf(" %c", &code);
}
REPLACE
while (code != 'y' || 'n' ){ <-- condition evaluates always to TRUE
WITH
while ((code != 'y' )&&(code != 'n')){
The line you have
while (code != 'y' || 'n' ){
is equivalent to:
while ((code != 'y') || 'n' ){
which evaluates to true all the time.
The logic you need is:
while (code != 'y' && code != 'n' ){

Resources