Imdad Hossain
Imdad Hossain

Reputation: 1

After hitting enter to get the user input using scanf, just a new line created

after running the program it asks for age but do not take the input

int main()
{
float age1, age2, age3, avg;
age1 = age2 = 24.0;
printf("How old are you?\n");
scanf("%f\n",&age3);

avg = (age1+age2+age3)/3;
printf("the average age is %f\n",avg);
return 0;
}

Upvotes: 0

Views: 54

Answers (3)

Akitha_MJ
Akitha_MJ

Reputation: 4294

scanf("%f",&age3); Try with this

Upvotes: 0

LearningC
LearningC

Reputation: 3162

The \n in scanf("%f\n",&age3); is causing the problem. remove the \n. refer Using "\n" in scanf() in C

Upvotes: 0

John Bollinger
John Bollinger

Reputation: 181469

Remove the \n from the end of your scanf() format. Any run of one or more whitespace characters (e.g. spaces, tabs, newlines, ...) in the format matches a run of any number of the same. Thus, your scanf() keeps matching whitespace -- as many newlines as you choose to enter, for example -- until it sees the next non-whitespace character.

Upvotes: 2

Related Questions