Why does this loop with scanf always quit? - c

#include <stdio.h>
int main(){
char quit = 'n';
do{
printf("Quit? (Y/N)");
scanf("%c", &quit);
}while(quit=='n' || quit=='N');
}
Why does my program quit after inputting anything?

The %c format specifier accepts any character, including newlines. So if you press N, then scanf reads that character first but the newline from pressing ENTER is still in the input buffer. On the next loop iteration the newline character is read. And because a newline is neither n or N the loop exits.
You need to add a space at the start of your format string. That will absorb any leading whitespace, including newlines.
scanf(" %c", &quit);

Just change your code to:
#include <stdio.h>
int main(){
char quit = 'n';
do{
printf("Quit? (Y/N)");
scanf(" %c", &quit);
}while(quit=='n' || quit=='N');
}
For more information read this link

Related

loop 2 taking input automatically

My code is behaving weirdly. In loop 1 it is working perfectly but in loop 2 it is automatically printing ASCII value 10. please help!.
#include<stdio.h>
int main(){
char c;
int loop =1;
do{
printf("\nLoop = %d\nWrite character of which you want to find acii values: ", loop);
scanf("%c", &c);
printf("\nASCII value of %c is %d.", c, c);
loop++;
}while(c != 'Z');
printf("\n******END*****");
return 0;
}
10 is ASCII for newline. You are reading whitespace, which you can avoid by using
scanf(" %c", &c);
Note the additional blank before the character specifier. It instructs scanf to ignore white space.

C program neglect in getting character after a string [duplicate]

This question already has answers here:
Scanf skips every other while loop in C
(10 answers)
Closed 6 years ago.
My problem is that the scanf for the character is skipped and it doesn't check scan the char to see if I want to repeat the program again or not so why this is happening?
#include <stdio.h>
#include <stdlib.h>
int main()
{
int number,check;
char rep;
printf("Program to check if number is even or odd");
while( (rep!='N') || (rep!='n') )
{
printf("\n\nPlease enter the number: ");
scanf("%d",&number);
check = number%2;
if(check != 0)
printf("\nNumber is odd.");
else
printf("\nNumber is even.");
printf("\n");
printf("Do you want to enter number again?\nY=yes\tN=no\n");
scanf("%c", &rep);
}
return 0;
}
Change scanf("%c", &rep); to scanf(" %c", &rep);.
This is because a '\n' is left in stdin the first time you input a number. When executing scanf("%c", &rep);, that '\n' is immediately consumed by scanf() and assigned to rep. Since '\n' is equal to neither 'N' nor 'n', that loop continues.
With the leading space in the format string, all whitespace characters are discarded before reading starts. In your case, the invisible '\n' will be ignored, so that you can input a character.
Also, you should write char rep = 0; instead, in case the original value of rep happens to be 'n' or 'N'.

Scanf Skip scanning character [duplicate]

This question already has answers here:
Scanf skips every other while loop in C
(10 answers)
Closed 6 years ago.
My problem is that the scanf for the character is skipped and it doesn't check scan the char to see if I want to repeat the program again or not so why this is happening?
#include <stdio.h>
#include <stdlib.h>
int main()
{
int number,check;
char rep;
printf("Program to check if number is even or odd");
while( (rep!='N') || (rep!='n') )
{
printf("\n\nPlease enter the number: ");
scanf("%d",&number);
check = number%2;
if(check != 0)
printf("\nNumber is odd.");
else
printf("\nNumber is even.");
printf("\n");
printf("Do you want to enter number again?\nY=yes\tN=no\n");
scanf("%c", &rep);
}
return 0;
}
Change scanf("%c", &rep); to scanf(" %c", &rep);.
This is because a '\n' is left in stdin the first time you input a number. When executing scanf("%c", &rep);, that '\n' is immediately consumed by scanf() and assigned to rep. Since '\n' is equal to neither 'N' nor 'n', that loop continues.
With the leading space in the format string, all whitespace characters are discarded before reading starts. In your case, the invisible '\n' will be ignored, so that you can input a character.
Also, you should write char rep = 0; instead, in case the original value of rep happens to be 'n' or 'N'.

input a letter, store in a variable print the letter to the command line

I am trying to make a very basic program to compare 2 numbers. after entering the 2 numbers the user is asked if they want to compare the 2 numbers. if y/n for yes or no. the problem I run into is that the program does not seem to ask for my input and immediately goes to the next print statement. Here is the code:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main(void){
int n1;
int n2;
char ch;
printf("compare 2 numbers, input a number\n");
scanf("%d", &n1);
printf("your first number is %d\n", n1);
printf("enter your second number to compare \n");
scanf("%d", &n2);
printf("your second number is %d\n", n2);
printf("do you want to compare these numbers? y/n \n");
//here is the problem. after entering y, the program closes.
//at this point I just want it to print the letter it was given by the user.
scanf("%c", &ch);
//the print statement that is supposed to print the letter the user inputs
printf("%c is a vowel.\n", ch);
getch();
return 0;
}
//I was using this code as a reference which runs correctly
#include <stdio.h>
int main()
{
char ch;
printf("Enter a character\n");
scanf("%c", &ch);
if (ch == 'a' || ch == 'A' || ch == 'e' || ch == 'E' || ch == 'i' || ch == 'I'
|| ch =='o' || ch=='O' || ch == 'u' || ch == 'U')
printf("%c is a vowel.\n", ch);
else
printf("%c is not a vowel.\n", ch);
return 0;
}
When you do
printf("enter your second number to compare \n");
scanf("%d", &n2);
you will enter the second number and press ENTER. This ENTER (\n) is still there in the buffer.
The scanf function removes whitespace automatically. scanf() leaves the new line char in buffer when you are using %c (%c are the exception they don't remove whitespace).
scanf("%c", &ch);
instead of this use
scanf("\n%c", &ch);
When you use %c,spaces and "escape character" as a valid character.So it will be stored in the ch.In the first two scanf,you use the %d can do no wrong.
In the second program,you call scanf("%c", &ch); at the first.This problem does not arise.If you call again, also can appear the same problem.
The reason for this problem is newline character \n leftover by the previous scanf after pressing Enter. This \n is left for the next call of scanf.
To avoid this problem you need to place a space before %c specifier in your scanf.
scanf(" %c", &C);
...
scanf(" %c", &B);
...
scanf(" %c", &X);
A space before %c is able to eat up any number of newline characters.
OR
You can use scanf to eat the single character without assigning it to anything like this::
scanf( "%[^\n]%*c", &C ) ;
%[^\n] tells the scanf to read every character that is not '\n'. That leaves the '\n' character in the input buffer, then the * (assignment suppression) will consume the a single character ('\n') but would not assign it to anything.

Do while loop with choice as char in C

In my code given below if I press 'y' for once it will reapeat, but then it is not asking for next tome to repeat (or press 'y').Can someone help why this code is terminated after one loop?
main()
{
char choice;
do
{
printf("Press y to continue the loop : ");
scanf("%c",&choice);
}while(choice=='y');
}
That will be because stdin is buffered. So you are probably entering the string of a y followed by a \n (newline character).
So the first iteration takes the y, but the next iteration doesn't need any input from you because the \n is next in the stdin buffer. But you can easily get around this by getting scanf to consume the trailing whitespace.
scanf("%c ",&choice);
NOTE: the space after the c in "%c "
But, your program can get stuck in an infinite loop if the input ends with a y. So you should also check the result of the scanf. e.g.
if( scanf("%c ",&choice) <= 0 )
choice = 'n';
You should read out the newline character after that scanf() call. Otherwise, that gets into choice the next time around and so the while loop comes out.
#include<stdio.h>
int main()
{
char choice;
do
{
printf("Press y to continue the loop : ");
choice = getchar();
getchar();
}
while(choice=='y');
return 0;
}
At the first character of the scanf format string, insert a space. This will clear out all white space characters from stdin before reading data.
#include <stdio.h>
int main (void)
{
char choice;
do
{
printf("Press y to continue the loop : ");
scanf(" %c",&choice); // note the space
}while(choice=='y');
return 0;
}

Resources