Thread: Help im at school

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    2

    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

    Code tags added by kermi3

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    2
    well i just remebered that u cant use modulus with doubles...

  3. #3
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595

    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.


    Good Luck with your program,

    Kermi3
    Lead Moderator
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    >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
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

  5. #5
    alpha
    Guest
    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 <math>. Thanks.

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    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 <math> without the preceding 'c'.

    -Skipper
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

  7. #7
    alpha
    Guest
    oh, okay, thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I've been slacking off in high school, am I going to hell
    By Silvercord in forum A Brief History of Cprogramming.com
    Replies: 31
    Last Post: 06-01-2003, 01:00 PM
  2. I Hate My School (rant!!!!)
    By Dalren in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 04-03-2003, 07:10 AM
  3. The purpose of school
    By Shadow12345 in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 11-17-2002, 04:12 AM
  4. School Shooting in Germany
    By Golden Bunny in forum A Brief History of Cprogramming.com
    Replies: 27
    Last Post: 04-27-2002, 01:47 PM
  5. Question about going to a technical school
    By Goalie35 in forum C++ Programming
    Replies: 1
    Last Post: 08-30-2001, 11:34 AM