My while loop with char doesnt work. Some help please - c

So i have this peace of code:
int choice_dig;
char choice_dup;
printf("Welcome to the mystery number game.\n");
printf("How many digits do you want to use (3 to 5)?");
scanf("%d", &choice_dig);
while (choice_dig<3 || choice_dig>5)
{
printf("\nPlease choose a number between 3 and 5.\t");
scanf("%d",&choice_dig);
}
printf("\nDo you want to allow duplicate digits (y or n)?");
scanf(" %c", &choice_dup);
while (choice_dup != 'y' || choice_dup != 'n')
{
printf("\nInvalid entry. Please choose y for yes and n for no.\t");
choice_dup = getchar();
getchar();
}
The choice_dup is already assinged as a char var at the start of the main. So when i run this its all good. But even when i press y or n it cant recognize it and the loop never ends. No matter what i type. Can someone help me and expain to me what wrong?

The loop will run forever because while (choice_dup != 'y' || choice_dup != 'n') will always evaluate to true.
You probably wanted: while (choice_dup != 'y' && choice_dup != 'n')

should be this:
printf("\nDo you want to allow duplicate digits (y or n)?");
scanf(" %c", &choice_dup);
while (choice_dup != 'y' && choice_dup != 'n')
{
printf("\nInvalid entry. Please choose y for yes and n for no.\t");
choice_dup = getchar();
getchar();
}

Just use
while (choice_dup != 'y' && choice_dup != 'n') instead

In the last while loop,i.e the one for choice_dup,change it to:
while(!(choice_dup=='y'| choice_dup=='n'))

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.

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' ){

Scanf() does not recognize space before %c

Observe this chunk of code:
#include <stdio.h>
int main(void)
{
char choice;
printf("\n\nDo you want to play again (Y/N)? ");
scanf(" %c", &choice);
if (choice != 'Y' || choice != 'y' || choice != 'N' || choice != 'n')
{
printf("\n\nYou didn\'t enter a decision.");
}
return 0;
}
I want the printf() to prompt the user to input either Y or N. The scanf() will fill the user's input in the variable choice. If choice is not equal to Y, y, N, or n, it will tell the user that he/she didn't enter a decision. Then, the program will end.
However, when I inputted Y or N, it printed "You didn't enter a decision." This should only happen if I don't enter Y or N (lowercase or uppercase).
I even put a space before the conversion character so the scanf() wouldn't read the newline character (\n).
Any help would be greatly appreciated!
Change
if (choice != 'Y' || choice != 'y' || choice != 'N' || choice != 'n')
to
if (choice != 'Y' && choice != 'y' && choice != 'N' && choice != 'n')
otherwise whether you enter any of Y, y, N, n or any other character (as pointed by Jonathan Leffler in the comment), the expression in if will be evaluated to true.
You Have to Just Include Else Like Following
#include <stdio.h>
int main(void)
{
char choice;
printf("\n\nDo you want to play again (Y/N)? ");
scanf(" %c", &choice);
if (choice != 'Y' || choice != 'y' || choice != 'N' || choice != 'n')
{
printf("\n\nYou didn\'t enter a decision.");
}
return 0;
}

Resources