This question already has answers here:
scanf() leaves the newline character in the buffer
(7 answers)
Closed 4 years ago.
I want to know the reason why this code not run properly
#include<stdio.h>
main()
{
char a;
int n;
do
{
printf("enter the number");
scanf("%d",&n);
printf("the squre is %d",n*n);
printf("want any more so Y for yes N for no");
scanf("%c%[^\n]",&a);
}while(a=='Y');
}
Reasons are
1. scanf("%c%[^\n]",&a); needs two parameters. Remove %[^\n].
2. \n character left behind by the previous scanf on pressing Enter key. Next scanf will read this \n character in the buffer. You need to consume this \n. Use a space before %c specifier to eat up this \n.
Try this:
scanf(" %c",&a);
↑ A space before %c specifier
A space before %c specifier is able to eat ant number of newline characters.
Your code after modification:
#include<stdio.h>
int main(void)
{
char a;
int n;
do
{
printf("enter the number\n");
scanf("%d",&n);
printf("the squre is %d\n",n*n);
printf("want any more so Y for yes N for no\n");
scanf(" %c",&a);
}while(a=='Y');
return 0;
}
Here is a solution to your problem.
#include<stdio.h>
main()
{
char a;
int n;
do
{
printf("enter the number");
scanf("%d",&n);
a = getchar(); // <-- Here is a change.
printf("the squre is %d",n*n);
printf("want any more so Y for yes N for no");
while(a == '\n') a = getchar();
}while(a=='Y');
}
What actually is making you problem is this line.
scanf("%d",&n);
Suppose, you entered 10 then pressed 'Enter', now scanf() takes 10 and leaves a newline behind in the buffer. It is making problem. By getchar() you are eating up that new line each time after taking a input with scanf. Yes there are other solutions too with scanf() tricks but it seems simpler to me. So I shared it.
just use scanf(" %c",&a); and could be done.
Wrong code: scanf("%c%[^\n]",&a);
Right code: scanf(" %c%*[^\n]",&a);
Related
This question already has answers here:
Using fflush(stdin)
(7 answers)
Parsing input with scanf in C
(5 answers)
scanf() leaves the newline character in the buffer
(7 answers)
Closed 5 years ago.
#include <stdio.h>
int main()
{
char another;
int num;
do
{
printf("enter the number");
scanf("%d", &num);
printf("square of%d is %d\n", num, num * num);
printf("want to check another number y/n");
fflush(stdin);
scanf("%c", &another);
} while (another == 'y');
return 0;
}
In the above code, the second scanf() is not getting executed and hence the console is not accepting input.
According to the standard, flushing stdin is undefined behaviour. See Using fflush(stdin) for more information.
When you enter a number for the first scanf, its always followed by a newline. %d only takes the integer value and the newline is still left in the input buffer. So the subsequent scanf ends up consuming that character and your loop terminates due to another=='y' being false. (another has '\n').
Following is one of the ways to solve the problem. Use a %c along with %d to capture newline and ignore it.
#include<stdio.h>
int main()
{
char another, nl;
int num;
do
{
printf("enter the number");
scanf("%d%c",&num,&nl);
printf("square of%d is %d\n",num,num*num);
printf("want to check another number y/n: ");
//fflush(stdin);
scanf("%c",&another);
printf("%c", another);
}while (another=='y');
return 0;
}
if you add the statement
fseek(stdin, 0, SEEK_END);
it will move the stdin pointer to the end of the file so any extra character will be omitted. then write the second scanf. I mean:
#include<stdio.h>
int main()
{
char another, nl;
int num;
do
{
printf("enter the number");
scanf("%d%c",&num,&nl);
fseek(stdin, 0, SEEK_END);
printf("square of%d is %d\n",num,num*num);
printf("want to check another number y/n: ");
//fflush(stdin);
scanf("%c",&another);
fseek(stdin, 0, SEEK_END);
printf("%c", another);
}while (another=='y');
return 0;
}
Cause of the char input before scanf dont work
remove the fflush and add a space to <<"%c">>
like this: scanf(" %c",&another);
#include<stdio.h>
int main(){
char another;
int num;
do
{
printf("enter the number ");
scanf("%d",&num);
printf("square of %d is %d\n",num,num*num);
printf("want to check another number y/n ");
scanf(" %c",&another);
}while (another=='y');
return 0;
}
This works great
First of all fflush; flushes the output buffer of a stream, so you should not be using it here. The reason why the second scanf is not working is because you are trying to read a 1-bit character which in this case always getting the value of \0 after the second printf statement. Hope this helped.
I am writing a simple quiz in C (using CodeBlocks 13.12)
It compiles, but doesn't work in second question. Whatever I will input, it always give answer 'that's sad'.
I can't understand what is wrong.
I came to this, where if I comment line 13 ( scanf("%d", &age); ) it's starting works ok for second question.
What the problem is?
#include <iostream>
#include <stdio.h>
#include <windows.h>
#include <clocale>
int main()
{
int age;
char S1;
printf("How old is your dog? \n");
scanf("%d", &age);
if (age <= 7)
{
printf(" very young. the end \n");
return 0;
}
else
{
printf("old dog. \n \n");
}
//question2
printf("Do you like dogs? y/n \n");
scanf("%c%c", &S1);
if (S1 == 'y')
{
printf("hey, that's nice \n");
}
else
{
printf(" that's sad :( . \n");
return 0;
}
return 0;
}
You cause undefined behavior by
scanf("%c%c", &S1);
scanf reads two chars, one stored in S1, one stored in some location on the stack because scanf expects a second char* to be supplied.
If your intention is to ignore the newline following the actual character, write
scanf("%c%*c", &S1);
Change the second scanf() to
scanf(" %c", &S1);
This would escape the left out newline character \n in the input buffer.
Plus, you are reading one char in this. So you need only one %c
scanf("%c", &S1);
is the correct way to input one character ,
My program below isn't taking input using getchar(). Instead, it ends after printing, "want to continue??(press y for yes and press n to reenter)". It doesn't take input after typing n or N.
void main(){
int i,arr[]={55,10,23,11,35,8,9,20},n;
char a;
printf("Given array is:\n");
for(i=0;i<8;i++)
printf("%d ",arr[i]);
do{
printf("\nEnter position where you want to insert element:");
scanf("%d",&n);
printf("You entered position %d \n",n);
printf("want to continue ??(press y for yes and press n to reenter)");
a=getchar();
} while(a=='n' || a=='N');
}
according to reference:getchar()
Returns the next character from the standard input (stdin).
Note that the standard input is a buffered I/O, so getchar() reads the first char in input buffer
when in scanf("%d",&n); you will input an int and a newline, then getchar() just read the newline
use scanf("%d\n", &n); instead, or as #BLUEPIXY said, add a getchar(); before a = getchar();
more detailed explanations about input buffer: Flush the input buffer
You can add this before a = getchar();
getchar();
So this 'eats' the new line character from the buffer. Because after you entered a number e.g. 3 into scanf("%d",&n); there is still a '\n' in the buffer and if you only have this: a = getchar();, \n gets read in
This question already has answers here:
C: Do-While Loop Repeating Too Much!
(3 answers)
Closed 9 years ago.
#include <stdio.h>
int main(void) {
char c = 'y', temp;
printf("Press y\n");
do {
printf("Press y to continue\n"); // read y again and again
scanf("%c", &c); // y entered
printf("%c", c); // loop should repeat but doesn't repeat
} while(c == 'y');
}
It will not. Because scanf reads the \n character on second iteration which cause the loop to terminate. Place a space before %cto consume this \n character left behind by previous scanf.
scanf(" %c",&c);
Try adding a space before %c which is a scanf quirk. The space absorbs the newline char after typing the 'y'.
scanf(" %c",&c);
This question already has answers here:
Scanf skips every other while loop in C
(10 answers)
Closed 8 years ago.
#include "stdio.h"
int main(void)
{
int order, nextp, N=3;
char cont;
nextp = 0;
printf("\nShould we continue (y or n): ");
scanf("%c", &cont);
if (cont != 'y') return;
for(; nextp < N; nextp++)
{
printf("Enter order number: ");
scanf("%d", &order);
printf("you have entered %d\n", order);
printf("okay now continue with cont\n");
printf("enter cont y or n: ");
scanf("%c", &cont);
if (cont != 'y')
{
printf("\nnot equal to y\n");
break;
}
printf("after intepreting t[0]");
}
return 0;
}
The output looks like this
Should we continue (y or n): y
Enter order number: 45
you have entered 45
okay now continue with cont
enter cont y or n:
not equal to y
The second input was skipped. Why?
because of newline character already in stdin , this is happening.
use
scanf(" %c", &cont);
instead of
scanf("%c", &cont);
note one space before %c.
After scanf("%d", &order); consumes the number (45 in this case), there is still a newline left after that. You can use scanf("%d\n", &order) to make it consume the return.
Another answer to this can be found here:
scanf() leaves the new line char in buffer?
This is why scanf is not typically preferred for character input. There's a left over carriage return after the previous input.
For example, if you were to add a getchar() after the order input, your problem would be solved, but that's not clean code. You can also see this explicitly by subsituting cont != 'y' to cont != '\n'.
Instead, use getchar() for all your input and check for \n
For most conversions scanf will skip whitespace, but for char format ("%c") you must skip white space by using an explicit space in the format (" %c") as explained here:
C - trying to read a single char
This is also explained in the scanf documentation, but it's confusing and may be better to use something else as others have mentioned.
You can use fflush()
printf("enter cont y or n: ");
fflush(stdin);
scanf("%c", &cont);