Tuesday, July 28, 2009

C++ HELP! Confused with COUNTERS!?

I'm in a basic high school C++ class, and I've been understanding everything and I thought I had For and While loops down. We must create a program that calculates how many numbers, from 1-100, are divisible by 2, 3, and 7. I have figured up to this much for the While loop:





#include%26lt;iostream%26gt;


using namespace std;





int main ()


{


for (int num=1;num%26lt;=100;num++)


{


int remainder=0;


remainder = num % 2;


if (remainder == 0)


{


count2++;


}








remainder = num % 3;


if (remainder == 0)


{


count3++;


}





remainder = num % 7;


if (remainder == 0)


{


count7++;


}


}





system("PAUSE");


return 0;


}





I just can't get the counters to work basically, all help very much appreciated!

C++ HELP! Confused with COUNTERS!?
they seem fine except you don't print anything out. if they aren't working, what are they not doing?
Reply:Where are you declaring the counters???? You are just using them.





You shouldn't declare inside the for loop either.
Reply:It looks like you forgot to declare your counters as int type at the beginning of the program. Also, are you trying to add the 7 to the counter , then increment? It looks like you need to just increment using counter++
Reply:I belive you need to initialize count2, count3 and count7 to 0 before you use them.


No comments:

Post a Comment