This question already has answers here:
Problems with C scanf("%c") function to read characters one by one
(4 answers)
Closed 2 years ago.
Is it possible to prompt the user to enter a char a second time and have the new char replace the original one? When I try this it will not allow me to enter a char the second time.
#include <stdio.h>
int main(){
char x;
printf("enter value: ");
scanf("%c", &x);
printf("enter value: ");
scanf("%c", &x);
}
#include<stdio.h>
int main(){
char x='p';
printf("enter value: ");
scanf("%c", &x);
printf("val is %c \n",x);
printf("enter 2nd value: ");
getchar();//flush the buffer
scanf("%c", &x);
printf("val is %c \n",x);
}
Problem was you was not clearing the buffer getchar function will clear it.
Related
This question already has answers here:
scanf() leaves the newline character in the buffer
(7 answers)
fgets instructions gets skipped.Why?
(3 answers)
Closed 4 months ago.
The following code works great:
int main() {
char inputString[100];
printf("\nEnter a sentence: ");
fgets(inputString, sizeof(inputString), stdin);
printf("\nYou entered: %s\n", inputString);
return 0;
}
in that it displays the user's sentence on a new line. But as soon as I add in a new scanf() function:
int main() {
int num;
char inputString[100];
printf("Enter a number: ");
scanf("%d", &num);
printf("\nEnter a sentence: ");
fgets(inputString, sizeof(inputString), stdin);
printf("\nYou entered: %s\n", inputString);
return 0;
}
The program no longer works. Everything set to be printed prints, but it never gives the user an opportunity to enter the sentence. The code just ends after the number is inputted. I don't understand why this is happening, or why this seemingly minor addition causes this error.
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 can't understand why this does exactly what I want. The part where I used two scanf's in the loop confuses me. I compiled it using devcpp.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int dend, dsor, q, r;
char c;
while(c!='n')
{
printf("enter dividend: ");
scanf("%d", &dend);
printf("enter divisor: ");
scanf("%d", &dsor);
q=dend/dsor;
r=dend%dsor;
printf("quotient is %d\n", q);
printf("remainder is %d\n", r);
scanf("%c", &c);
printf("continue? (y/n)\n");
scanf("%c", &c);
}
system("PAUSE");
return 0;
}
FWIW, your code invokes undefined behavior. In the part
char c;
while(c!='n')
c is an uninitialized local variable with automatic storage and you're trying to use the value of c while it is indeterminate.
That said, first scanf("%c", &c); is used to eat up the newline present in the input buffer due to the press of enter key after previous input. You can read about it in details in another post.
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);
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Simple C scanf does not work?
Why does scanf("%c", &letter); not working. the rest is working
#include <stdio.h>
main(){
int number;
float number1;
char letter;
char letter2 [5];
printf("Enter an int: ");
scanf("%d", &number);
printf("Enter a float: ");
scanf("%f", &number1);
printf("Enter a letter: ");
scanf("%c", &letter);
printf("Enter a string: ");
scanf("%s", letter2);
printf("INT = %d\n", number);
printf("FLOAT = %f\n", number1);
printf("LETTER = %c\n", letter);
printf("LETTER2= %s\n", letter2);
getch();
}
This is because the newline (return key) after feeding float is counted as a character.
This is not a bug but it is due to fact that "\n" is considered a character in C and if you have to ignore it, you need to do that manually.
The simplest solution for your case is to eat up the newline as follows:
scanf("%f", &number1);
getchar();
This Link will help.
scanf reads the whitespace that is left in the buffer from the previous line. To skip the whitespace, add a space to the scanf:
scanf(" %c", &letter);
The space means "skip whitespace" and the the %c means "read the following character.