Below is my code:
I'm brand new to C code.
I am able to stop the code from calculating the average after "12345" is entered but the code is calculating "12345: and not breaking the loop.
I also want the user to be able to enter as many numbers as they want until they enter "12345"
Do I need to remove the while statement with the counter?
Code:
#include <stdio.h>
int main ()
{
/* variable definition: */
int count, value,sum;
double avg;
/* Initialize */
count = 0;
sum = 0;
avg = 0.0;
// Loop through to input values
while (count < 20)
{
printf("Enter as many positive Integers as you like"
" When finished simply type 12345 to calculate the average \n");
scanf("%d",&value);
if (value >= 0) {
sum = sum + value;
count = count + 1;
if (value == 12345)
break;
}
else
{
printf("Value must be positive\n");
}
}
// Calculate avg. Need to type cast since two integers will yield an integer
avg = (double) sum/count;
printf("average is %lf\n " , avg );
return 0;
}