I have begun using Absolute Beginner's Guide to C to learn the C programming language, and I am trying to work through examples in the book. However, I don't seem to be able to get the below copied program (page 31) to run. I realize that the scanf function doesn't play nice with getchar, but it's what I am being told to use at this point. I am hoping someone can give me some pointers here. Tried running after successful compilation in Dev C++ and the command line. Result for avg I am getting is a very large random number.
Code:
* Written by David Ghelerter, finished on December 25, 2013*/
/* Filename: avg.exe*/
/* Computes an average*/
#include <stdio.h>
main()
{
float gr1, gr2, gr3; /* Variables to hold grades */
float avg; /* Variable holds averages*/
/* Asks for a students grades */
printf("What grade did the first student get ?\n");
scanf(" %.1f", &gr1);
fflush(stdin);
printf(" What grade did the second student get ?\n");
scanf(" %.1f", &gr2);
fflush(stdin);
printf("What grade did the thrird student get ?\n");
scanf("%.1f", &gr3);
fflush(stdin);
avg = (gr1 + gr2 + gr3)/3.0;
printf("\nThe student average is %.2f", avg);
getchar();
return 0;
}