I am very much a beginner to programming in C so please help out here. I am trying to write a program that loops asking the user to enter a number, if the number is positive it adds it to the total, and if it is negative it ends the program and displays the average, lowest input, and highest input. Unfortunately no matter what I change with the low and high things I keep getting 'nan' for the low, and whatever the negative number for the high.. please help!
#include<stdio.h>
int main(void)
{
float input;
float total;
float low;
float high;
float average;
int count=0;
printf("\n\nPlease enter a positive number to continue or a negative number");
printf(" to stop: ");
scanf("%f", &input);
while (input > 0)
{
count = count + 1;
printf("\nPlease enter a positive number to continue or a negative");
printf(" number to stop: ");
scanf("%f", &input);
total = total + input;
if ( low < input )
{
( low = input );
}
else
{
( low = low );
}
}
if (input < high)
{
(high = input);
}
else
{
(high = high);
}
average = total / count;
printf("\n\n\nCount=%d",count);
printf("\n\n\nTotal=%f",total);
printf("\nThe average of all values entered is %f\n", average);
printf("\nThe low value entered: %f\n", low);
printf("\nThe highest value entered: %f\n", high);
return 0;
}
After compiling it with gcc and testing it with the numbers 1, 5, 4, then -1 I get the following output
Count=3
Total=8.000000
The average of all values entered is 2.666667
The low value entered: nan
The highest value entered: -1.000000