I worked on this yesterday about 5 hours and got the code to work using help from this site, but I think the way I did it was a cheater way, I used a scanf command. Anyways I want to fix this the correct way. Thanks guys! Oh the code compiles but the average that gets spit out is wrong. I would like to conceptually understand what I am doing wrong as well as finish the assignment.
#include <stdio.h>
#include <stdlib.h>
double get_number(int num);
main () {
double n1,n2,n3;
double average;
printf("\nCompute the average of 3 integers\n");
printf("--------------------------------\n");
n1 = get_number(1);
n2 = get_number(2);
n3 = get_number(3);
average = (n1 + n2 + n3)/3;
printf("The average is %0.2f\n",average);
}
double get_number(int num) {
double value = 0;
int c;
printf("Please input number %i: ", num);
while ((c = getchar()) != '\n') {
if ( (c<'0') || (c>'9') ) {
printf("Incorrect character entered as a number - %i\n",c);
exit(-1);
}
else {
c =value;
}
}
return(value);
}