This question already has answers here:
fgets doesn't work after scanf [duplicate]
(7 answers)
scanf() leaves the newline character in the buffer
(7 answers)
Closed 2 years ago.
I'm trying to use fgets() to get a string from the user but whenever I run the program it skips right over the fgets(), meaning it won't let me input anything
printf("*********************************\n");
printf(" Stock 3\n");
printf("*********************************\n");
printf("Enter stock name: ");
fgets(name,10,stdin);
printf("Enter the number of shares: ");
scanf("%d", &share3);
printf("Enter the buy price, current price, and fees: ");
scanf("%f, %f, %f", &buy_p3, ¤t_p3, &fees3);
puts("");
I believe it has something to do with a \n skipping it but I'm not sure where it is coming from. Any help appreciated. Thanks!
Related
This question already has answers here:
scanf() leaves the newline character in the buffer
(7 answers)
scanf Getting Skipped [duplicate]
(1 answer)
Closed last year.
I am currently working on a code and I keep having issues with my code. I will add it below, but for some reason, whenever I ask for the input, it skips over the second scanner and goes straight to the third. Does anyone know what the issue may be?
#include <stdio.h>
int main()
{
char a,b,c,d;
printf("Please input the price of your shopping items:");
printf("\n");
printf("Item 1:");
scanf("%c", &a);
printf("Item 2:");
scanf("%c", &b);
printf("\n");
printf("Item 3:");
scanf(" %c", &c);
return 0;
}
This question already has answers here:
Program doesn't wait for user input with scanf("%c",&yn);
(5 answers)
scanf() leaves the newline character in the buffer
(7 answers)
Closed 1 year ago.
int main()
int c,s;
char h;
printf("Enter the carbon\n");
scanf("%d",&c);
printf("Enter the hard\n");
scanf("%c",&h);
printf("Enter strength\n");
scanf("%d",&s);
printf("%d%d",c,s);
printf("%c",h);
}
Output:
Enter the carbon
4
Enter the hard
Enter strength
My program is not reading character data types. Can anyone tell me the reason?
The compiler is not showing any error.
This question already has answers here:
Why does scanf ask twice for input when there's a newline at the end of the format string?
(7 answers)
Closed 1 year ago.
I have a simple code:
#include <stdio.h>
int main(void) {
int alter;
printf("Your age: ");
scanf("%d\n", &alter);
printf("your age is %d\n", alter);
}
Output is:
Your age: <entering some_number(1)> (enter)
(terminal is waiting for another value)
<entering some_number(2)> (enter)
your age is number(1)
why does it wait for me to enter 2 values?
thanks
There should be be \n in the scanf. It should be
scanf("%d", &alter);
This question already has answers here:
scanf() leaves the newline character in the buffer
(7 answers)
Closed 1 year ago.
if (choice=1){
printf("Enter the name of the patient:\n");
scanf("%d", name);
printf("Enter the date of birth of the patient:\n");
scanf("%s", birthmonth, birthday, birthyear);
printf("Enter the gender of the patient:\n");
scanf("%s", gender);
printf("PRESS ANY KEY TO CONTINUE");
getch();
}
Whenever I run this part of my code, it only allows me to enter the name of the patient. Does anyone know why this happens?
You're using an integer placeholder (%d) instead of (%s) for a string in the first scanf statement.
And in the second scanf statement, you should be using 3x %s, to get input to the three variables.
This question already has answers here:
scanf() leaves the newline character in the buffer
(7 answers)
Closed 3 years ago.
for(i=0;i<requestnum;i++)
{
bookcount++;
printf("\n\nEnter Booking Request #%d", bookcount);
printf("\nLast Name:");
fgets(OneClient[i].LastName,100,stdin);
if(!strcmp(OneClient[i].LastName, "\n"))
{
printf("Processed %d successful requests out of %d submitted
requests.\nGenerating management report.\nThank you for using the hotel
reservation system.\nBye!",succescount, bookcount);
exit(1);
}
printf("First Name:");
scanf(" %s", OneClient[i].FirstName);
}
The fgets does its work in the first loop, but when the second loop occurs, it scans and stores a blank character and doesnt wait for a user input, I used fgets because I need to terminate the loop when a blank is entered by the user. Help fix my problem please?
fgets is reading \n left out by scanf in the stream.
Just replace scanf with fgets.
for(i=0;i<requestnum;i++)
{
bookcount++;
printf("\n\nEnter Booking Request #%d", bookcount);
printf("\nLast Name:");
fgets(OneClient[i].LastName,100,stdin);
......
.....
printf("First Name:");
/*Assumed FirstName is not pointer*/
fgets(OneClient[i].FirstName, sizeof(OneClient[i].FirstName), stdin);
}