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.
Related
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.
I'm pretty new to C, and I have a problem with inputing data to the program.
My code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void) {
int a;
char b[20];
printf("Input your ID: ");
scanf("%d", &a);
printf("Input your name: ");
gets(b);
printf("---------");
printf("Name: %s", b);
system("pause");
return 0;
}
It allows to input ID, but it just skips the rest of the input. If I change the order like this:
printf("Input your name: ");
gets(b);
printf("Input your ID: ");
scanf("%d", &a);
It will work. Although, I CANNOT change order and I need it just as-is. Can someone help me ? Maybe I need to use some other functions. Thanks!
Try:
scanf("%d\n", &a);
gets only reads the '\n' that scanf leaves in. Also, you should use fgets not gets: http://www.cplusplus.com/reference/clibrary/cstdio/fgets/ to avoid possible buffer overflows.
Edit:
if the above doesn't work, try:
...
scanf("%d", &a);
getc(stdin);
...
scanf doesn't consume the newline and is thus a natural enemy of fgets. Don't put them together without a good hack. Both of these options will work:
// Option 1 - eat the newline
scanf("%d", &a);
getchar(); // reads the newline character
// Option 2 - use fgets, then scan what was read
char tmp[50];
fgets(tmp, 50, stdin);
sscanf(tmp, "%d", &a);
// note that you might have read too many characters at this point and
// must interprete them, too
scanf will not consume \n so it will be taken by the gets which follows the scanf. flush the input stream after scanf like this.
#include <stdlib.h>
#include <string.h>
int main(void) {
int a;
char b[20];
printf("Input your ID: ");
scanf("%d", &a);
fflush(stdin);
printf("Input your name: ");
gets(b);
printf("---------");
printf("Name: %s", b);
system("pause");
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void) {
int a;
char b[20];
printf("Input your ID: ");
scanf("%d", &a);
getchar();
printf("Input your name: ");
gets(b);
printf("---------");
printf("Name: %s", b);
return 0;
}
Note:
If you use the scanf first and the fgets second, it will give problem only. It will not read the second character for the gets function.
If you press enter, after give the input for scanf, that enter character will be consider as a input f or fgets.
you should do this way.
fgetc(stdin);
scanf("%c",&c);
if(c!='y')
{
break;
}
fgetc(stdin);
to read input from scanf after reading through gets.
scanf("%d", &a); can't read the return, because %d accepts only decimal integer. So you add a \n at the beginning of the next scanf to ignore the last \n inside the buffer.
Then, scanf("\n%s", b); now can reads the string without problem, but scanf stops to read when find a white space. So, change the %s to %[^\n]. It means: "read everthing but \n"
scanf("\n%[^\n]", b);
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void) {
int a;
char b[20];
printf("Input your ID: ");
scanf("%d", &a);
printf("Input your name: ");
scanf("\n%[^\n]", b);
//first \n says to ignore last 'return'
//%[^\n] read until find a 'return'
printf("---------\n");
printf("Name: %s\n\n", b);
system("pause");
return 0;
}
The scanf function removes whitespace automatically before trying to parse things other than characters. %c, %n, %[] are exceptions that do not remove leading whitespace.gets is reading the newline left by previous scanf. Catch the newline usinggetchar();
scanf("%d", &a);
getchar(); // catches the newline character omitted by scanf("%d")
gets(b);
https://wpollock.com/CPlus/PrintfRef.htm
Just use 2 gets() functions
When you want to use gets() after a scanf(), you make sure that you use 2 of the gets() functions and for the above case write your code like:
int main(void) {
int a;
char b[20];
printf("Input your ID: ");
scanf("%d", &a);
//the change is here*********************
printf("Input your name: ");
gets(b);
gets(b);
//the change is here*********************
printf("---------");
printf("Name: %s", b);
system("pause");
return 0;
}
For some reason my C program closes immediately after outputting "Enter a letter (D/E/F): "
I would like to be able to store 3 characters in the letters[] array so that I can print them later on.
I use Visual Studio 2017 and the program has no errors. It just seems to skips everything after printf("Enter a letter (D/E/F): ");.
I think the problem has to do with scanf_s, but I don't exactly know what the problem is or how to fix it. Here's my program:
#include <stdio.h>
int main(void)
{
char letters[3];
char ch;
printf("Enter a letter (A/B/C): ");
scanf_s(" %c", &ch);
letters[0] = ch;
printf("Enter a letter (D/E/F): ");
scanf_s(" %c", &ch);
letters[1] = ch;
printf("Enter a letter (G/H/I): ");
scanf_s(" %c", &ch);
letters[2] = ch;
printf("You entered %c, %c, and %c.", letters[0], letters[1], letters[2]);
getchar(); getchar(); // PAUSE
return 0;
}
Please help.
Changed scanf_s(" %c", &ch); to scanf_s(" %c", &ch, 1); and the program now works!
Thanks Weather Vane, Some programmer dude, Kal Karaman, Weather Vane again, and this webpage: http://faculty.edcc.edu/paul.bladek/CS131/scanf_s.htm!
Can someone run the following C program on your IDE and advise me what I am missing?.
#include<stdio.h>
#include<conio.h>
int main()
{
int a;
char s;
char n[10];
printf("What is your name?: ");
scanf("%s", &n);
printf("What is your age?: ");
scanf("%d", &a);
printf("Are you male or female?: ");
scanf("%c", &s);
printf("Your name is %s\nYour age is %d\nYour sex is %c\n", n, a, s);
getch();
return 0;
}
While we enter the age and hit the enter button, it slips and shows wrong output without evening asking for the third input "Are you male or female?". I tested it on Turbo C++, Dev C++, Code Blocks, all show the same error output.
Your problem is that the scanf("%c", &s); takes the new-line character. Maybe you could try the following scanf(" %c", &s); (important ist the white-space before %c) as described here Problems with character input using scanf() or How to do scanf for single char in C
It would be correctly to write
printf("What is your name?: ");
scanf("%s", n);
^^^
or even
printf("What is your name?: ");
scanf("%9s", n);
instead of
printf("What is your name?: ");
scanf("%s", &n);
and
printf("Are you male or female?: ");
scanf(" %c", &s);
^^^
instead of
printf("Are you male or female?: ");
scanf("%c", &s);
Otherwise in the last case a white space character is read into the variable s.
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);