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);
Related
This question already has answers here:
Behaviour of scanf when newline is in the format string
(5 answers)
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.
Below is my program.
#include<stdio.h>
int main(){
int i,n,m;
int sum=0;
scanf("%d",&n);
for(i=0;i<n;i++){
printf("Enter the %d mark:\n",i+1);
scanf("%d\n",&m); //here I used \n
sum=sum+m;
}
printf("%d\n",sum);
printf("%.2lf",(double)sum/n);
return 0;
}
The output I got is like
3
Enter the 1 mark:
3
4
Enter the 2 mark:
2
Enter the 3 mark:
5
9
3.00
what is the effect of \n in the scanf for this output?
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)
How to read / parse input in C? The FAQ
(1 answer)
Closed 2 years ago.
So, i just want to know how to deal with this, the code doesnt have a purpose besides testing.
why does the the enter i press go to the getchar()command?
or is that the case to begin with? i assume it is.
here is the code
#include<stdio.h>
int main(){
int x,y,z;
printf("Enter two nums\n");
scanf("%d %d", &x, &y);
z=x/y;
printf("%d is answ\n", z);
puts("Press enter to exit");
getchar();
return 0;
}
This question already has answers here:
Removing trailing newline character from fgets() input
(14 answers)
Closed 2 years ago.
#include <stdio.h>
#include <stdlib.h>
int main()
{
char name [50];
char age [50];
printf("Please enter your name:\n");
fgets(name, 20, stdin);
printf("Please enter your age:\n");
fgets(age, 20, stdin);
printf("Hello %s, you are %s years old", name, age);
return 0;
}
//sorry for such a trvial question I am new to programming, but Why does the last printf create new
lines after each placeholder?
You are getting input from stdin, so you are probably ending your entries by hitting Enter: therefore you end your line with the \n character and fgets() stores it into the name and age variables.
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!