# Logical error in C program



## atg72 (Oct 14, 2004)

#include <stdio.h>
main()
{
int i, c=0, x, sum=0;

for (i=0; i<5; i++)
{
scanf("%d", &x);
if (x%3==0)
continue;

sum+=x;
c++;
}
printf("Average = %.2f\n",(float)sum/c);
}

The problem with that program is with command continue; Can anyone help me to find what else can i use to fix it?


----------



## mt2002 (Sep 14, 2005)

> The problem with that program is with command continue; Can anyone help me to find what else can i use to fix it?


Fix what? What is your program supposed to do? What is it currently doing? What are you currently trying to do?

(Also, continue is not a command, its a keyword.)


----------



## pvc_ (Feb 18, 2008)

why do you have a "c++;" inside your for loop?? for loop increments the loop counter by itself.
I'm not sure what you are trying to do, but usually break is used instead of continue to break out of a loop. Not sure why you wanna skip one iteration.


----------



## burnthepc (Aug 12, 2007)

I think I get what you're trying to do: count the non-divisable-by-3 numbers in a file and take the average. This will probably work:

#include <stdio.h>
main()
{
int i, c=0, x, sum=0;

for (i=0; i<5; i++)
{
scanf("%d", &x);
if ((x%3) !=0)
{
sum+=x;
c++;
}
} // note extra bracket
printf("Average = %.2f\n",(float)sum/c);
}

What happens if all 5 numbers are divisable by 3?


----------



## pvc_ (Feb 18, 2008)

oops, ignore my last comment about that "C++;" line. I just realized you are using "i" for your loop counter.


----------

