Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Newbie "For" loop problem

Status
Not open for further replies.

Yarbz2

Technical User
Sep 28, 2002
2
US
I'm very new at this and am stuck on a loop problem.
I'm trying to make an averageing loop but the loop won't ask for the next input. This is what I have:

#include <stdio.h>

void main (void)

{
float sum = 0, score, average;
int number;

printf(&quot;Number of Floats to average? &quot;);
scanf(&quot;%i&quot;, &number);
printf(&quot;\n&quot;);

for (int x = 1; x <= number; x++)
{
sum = sum + score;
printf(&quot;Score %i= &quot;, x);
scanf(&quot;%.2f&quot;, &score);
printf(&quot;\n&quot;);
}

printf(&quot;\nYour total is %.2f\n&quot;, sum);
average = sum / number;
printf(&quot;\nThe Average of the %i Scores = %.2f\n\n&quot;, number, average);

}

The output will number the lines but places -107374176.00 in as the score no matter what I do.

Help!!!
Thanks
Yarbz2
 
You have 2 problems in your code :

1) scanf doesn't support the %.2f. Just use %f.
2) the line sum = sum + score;
should be located AFTER score is entered - otherwise you will always miss the last input.


/JOlesen
 
Hi:

Here's my take on your work:

The two biggest problems:

1) Place the sum after the user enters a value.

2) My compiler doesn't like:
scanf(&quot;%.2f&quot;...

other things:

3) change scanf(&quot;%i&quot; .. to scanf(&quot;%d&quot;...

4) your compiler may let you define x in the for loop, but, unfortunately, mine does not. (SCO Open Server V).

5) Although this syntax:
sum = sum + score;

works, &quot;C&quot; veterans like to see:
sum += score;

the latter is also a few clock ticks more efficient.

Regards,

Ed

#include <stdio.h>

void main (void)
{
float sum = 0, score, average;
int number, x;

printf(&quot;Number of Floats to average? &quot;);
scanf(&quot;%d&quot;, &number);
printf(&quot;\n&quot;);

for (x = 1; x <= number; x++)
{
printf(&quot;Score %i= &quot;, x);
/* scanf(&quot;%.2f&quot;, &score); */
scanf(&quot;%f&quot;, &score);
/* sum = sum + score; */
sum += score;
printf(&quot;\n&quot;);
}

printf(&quot;\nYour total is %.2f\n&quot;, sum);
average = sum / number;
printf(&quot;\nThe Average of the %i Scores = %.2f\n\n&quot;, number, average);
}
 
Thanks guys! It worked

You said your compiler didn't like &quot;%.2f&quot;
what are your setting on your compiler... cause mine didn't catch it. And do you know of a way I can set mine better?

Thanks again,
Yarbz2
 
Yarbz:

I think I've mislead you. Sorry! scanf(&quot;%.2f .. compiles, but fails at runtime. As JOlesen says, &quot;scanf doesn't support %.2f&quot;.

My runtime error was the for loop completes without scanf asking for input.

Regards,


Ed
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top