This question already has answers here:
How to do scanf for single char in C [duplicate]
(11 answers)
Closed 8 years ago.
I have problem with this code:
printf("Select your math: \n'+'addition \n'-'subtraction \n'*'multiplication \n'/' division \n");
char do_math;
scanf("%c", &do_math);
printf("Type 1 st number: ");
Problem is, that program doesn't wait until I type "do_math" but it displays "Type 1 st number: " right after first printf. Any ideas?
The only way your compiler will miscompile that is if you deliberately defined scanf() as a do-nothing macro. Don't blame your compiler!
There is probably a previous scanf() call in the program that left a carriage return in the input buffer. You can confirm that hypothesis by printing the value of do_math.
Try scanf(" %c", &do_math); (with a space before %c) to discard such whitespace.
(It's also a good idea to pay attention to the return value from scanf().)
Related
This question already has answers here:
The program doesn't stop on scanf("%c", &ch) line, why? [duplicate]
(2 answers)
Closed 1 year ago.
I'm struggling on a question proving scanf() and getchar() can both retrieve a character from the input.
However, when I try to put them inside the same program, only the first function is running properly. The latter is discarded completely.
#include <stdio.h>
char letter;
int main()
{
printf("I'm waiting for a character: ");
letter = getchar();
printf("\nNo, %c is not the character I want.\nTry again.\n\n",letter);
printf("I'm waiting for a different character: ");
scanf("%c",&letter);
printf("Yes, %c is the one I'm thinking of!\n",letter);
return(0);
}
output
I have tried switching the places of those two functions but it is of no use.
Can someone help me find the issue and provide a way to fix this? The only requirement is that the program takes input twice, once by the getchar() function and once via scanf()
The second read attempt just reads whitespace (the end of line character, since you pressed enter after the first letter). Simply replace it with this:
scanf(" %c", &letter);
The space before % will tell scanf to read the next non-whitespace character.
This question already has answers here:
scanf() leaves the newline character in the buffer
(7 answers)
Closed 2 years ago.
I'm still learning the basics of C right now and I wanted to build a simple 'enter y to loop the program' that I've seen in a lot of console programs and wrote the following code to test it out. When I do execute it however, it does work as intended once after which the program just exits. It would help me a lot if anyone told me what I'm doing wrong here :
int main()
{
char l;
do
{
printf("Loop successful.\n");
fflush(stdin); //I heard this has to be used because scanf keeps the enter key in buffer or something like that
scanf_s("%c", &l);
} while (l == 'y');
}
I also get a "Missing integer argument to 'scanf(_s)' that corresponds to conversion specifier '2'" warning and I don't seem to understand what I'm being warned against.
fflush(stdin); //I heard this has to be used because scanf keeps the enter key in
That's wrong, fflushing stdin is undefined behaviour, what you need to do is consume the newlines lefted by the previous scan, just switch from
scanf_s("%c", &l);
to
scanf_s(" %c", &l); // Notice the space before %
This question already has answers here:
scanf() leaves the newline character in the buffer
(7 answers)
C skipping one command of a function? [duplicate]
(2 answers)
Closed 6 years ago.
I've been given the pretty simple task of writing a program that will take two characters and then print the letters inbetween them using a for() loop.
Here's my code:
#include <stdio.h>
int main() {
char a, b;
printf("\nEnter the first character: ");
scanf("%c", &a);
printf("\nEnter the second character: ");
scanf("%c", &b);
for(char i = a; i <= b; i++) {
printf("%c ", i);
}
return 0;
}
When I run it, I am prompted to enter the first character correctly but when I press enter it only runs the next printf() and then terminates.
No errors or warnings or anything on compilation. Another similar question I found that was apparently solved does not work for me either.
Thanks in advance.
You have to consume the \n in stdin left by first scanf.
Fastest fix
scanf(" %c", &b);
The space before %c tells to scanf to ignore all whitespaces before to read the char.
If I read your code correctly, by pressing enter, you would enter the second character, which would most probably (depending on the environment) start with a numeric value of 13, which would be smaller than any letter, so the loop's body is executed only once.
This question already has answers here:
Simple C scanf does not work? [duplicate]
(5 answers)
Closed 7 years ago.
I have a question, We were asked by our teacher to write a rock paper scissors program using the if else statement
my problem is
if i code it like this
char a, b;
clrscr();
printf("\n Enter player 1 value");
scanf("%c", &a);
printf("\n Enter Player 2 value:);
scanf("%c", &b);
my problem is when i code it like this after entering the 1st value it ignores the second one and just goes on the if statements
and i found a solution which is putting space on %c on the second scanf which looks like this (found a similar program)
scanf(" %c", &b);
and it works but now i don't know why ??
can anyone explain to me why it was being ignored and why putting a space solves that problem ?? it will gladly help
thanks in advance
by adding a space you exclude the whitespaces created by the previous scanf.
scanf() stops as soon it finds a whitespace so if the string start with a whitespace you get nothing.
use scanf("%[^\n]", &variable) to get everything (space included) or even better
scanf("%30[^\n]", &variable) to get everything with a size limit on the input (in this case 30).
There is also another function that lets you read from a stream:
fgets(&variable, sizeof variable, stdin); check the doc out here(http://www.cplusplus.com/reference/cstdio/fgets/)
This question already has answers here:
abnormal behavior of scanf [duplicate]
(3 answers)
Closed 8 years ago.
Today I encountered with problem that when I use twice scanf which accept character as input then second scanf skipped.
I tried to figure out I came to the conclusion that when we press enter key after first scanf the second scanf is skipped because enter key is take as input in the second scanf.
Can some please explain what is the exact reason with it?
int main()
{
char ch;
int num;
scanf("%d",&num);
scanf("%c",&ch);//This is skipped but its accept input when space as scanf(" %c",&ch)
}
scanf("%c", &ch);
It will read '\n' from the previous scanf since you are inputting a number and pressing enter, if you don't write it like scanf(" %c", &ch);. That way it will ignore '\n' and wait until you enter a valid char.