0

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;
}

3 Answers3

2

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.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157
0

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

LearningC
  • 3,182
  • 1
  • 12
  • 19
0

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

Govind Parmar
  • 20,656
  • 7
  • 53
  • 85
Akitha_MJ
  • 3,882
  • 25
  • 20