# Thread: Help im at school

1. ## Help im at school

im writing a program that checks for "magic numbers" what it does is add 1 +2+3+4+5... and checks to see if each is a perfect square. my problem is that modulus doesnt work it gives me a illegal operand error every time.

why is this?

here is the code im using
Code:
```void MagNum(int num, int mag)
{
int temp = num, counter = 1;
double test, square;

temp ++;

while (counter <= num)
{
square = sqrt(double (mag));
test = square % 1;
if(test == 0)
{
counter ++;
cout << "Magic number = " << mag;
mag = temp + mag;
}

else
{
mag = temp + mag;
}
}
}```
my problem is just in the loop

2. well i just remebered that u cant use modulus with doubles...

3. ## Code Tags

I am posting this because you did not use code tags on this thread. In the furture please use Code Tags. They make your code MUCH easier to read and people will be much more likely to help you if you do. And they'll be happier about helping you

For example:

Without code tags:

for(int i=0;i<5;i++)
{
cout << "No code tags are bad";
}

With Code Tags:
Code:
```for(int i=0;i<5;i++)
{
cout << "This code is easy to read";
}```
This is of course a basic example...more complicated code is even easier to read with code tags than without.

I've added code tags for you this time. They can be added by putting [code] at the beginning of your code and [/code] at the end. More information on code tags may be found at the link in my signature. I also suggest you take a look at the board guildlines if you have not done so already.

This is a common first post mistake, just remember to use [code] tags in the future and you'll get much more help.

If this is your first time posting here the welcome, and if there's anything I can do or any questions I can answer about these forums, or anything else, please feel free and welcome to PM me.

Kermi3

4. >well i just remebered that u cant use modulus with doubles...
Yes, as a matter of fact, you can. The function is fmod(). Include <cmath> with your other headers. fmod() takes two 'doubles' as arguments.

-Skipper

5. I know this is off-topic. I'm still learning c++, but why would you need to #include <cmath> when all that header file does is #include [itex]. Thanks.

6. Good question. Either will work, but you are right...with an explanation.

The standard C++ library contains a number of headers from the C library. Those C library files should be preceded by 'c' - <stdio.h> to <cstdio> - for example, to be standard-compliant. (Not necessarily related to your question, however.)

One of my references does illustrate the use of <cmath> as opposed to <math.h> and I've gotten used to the practice. Frankly, I've never tried [itex] without the preceding 'c'.

-Skipper

7. oh, okay, thanks.