#include <stdio.h>
main()
{
float number, sum = 0, average;
int count = 0;
printf("Input real numbers (0 is end)n");
scanf_s("%f", &number);
for (number > 0;)
{
scanf_s("%f", &number);
sum += number;
count++;
}
if (count == 0)
{
printf("Line doesnt contain numbersn");
}
average = sum / count;
printf("%f", average);
}
I can’t see why this code wont work, and why it keeps breaking, but i assume theres something wrong with the for command, and my inability to comprehend what mistake im making, the program is supposed to take a string of numbers and let you keep adding different numbers to it until you type in 0, in which it will give you the average for that string, but it instead it ends after one number, and returns 0.
I keep trying to fix it, but i only made the code worse, and now im stuck looking at tutorials for the for command, however this is a very niche problem and i dont know what to do or what to write, ive tried number != 0, count++; number > 0, count++; i tried putting the count underneath, i tried using scanf, but nothing seems to work
Jarletoss is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.