Could it be the "%"?

This is a discussion on Could it be the "%"? within the C++ Programming forums, part of the General Programming Boards category; Ok, here is my problem. This program runs flawlessly until i input a number bigger than 5 char long. If ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    29

    Could it be the "%"?

    Ok, here is my problem. This program runs flawlessly until i input a number bigger than 5 char long. If i do it just outputs "1."

    But if i put an int that is 8 char long than it outputs a "3"

    Please help




    Code:
    switch(resp)
      {
        case 1:
          cout << "Binary - Decimal..." << endl;
          cout << "Enter the number needing converting:" << endl;
          cin >> num;     
          val = (num / 100000000);
          if(val >= 1.0)
            {
            cout << "Has to be under 8 numbers long!" << endl;
            break;
            }
            
          else
            a= num % 10;
            if (a == 1)
              {
              x = x + 1;
              }
            
            cout << x << endl;
            
          num2 = num / 10;
          
            a= num2 % 10;
            if (a == 1)
              {
              x = x + 2;
              }
              
              cout << x << endl;
              
          num2 = num2 / 10;
          
            a= num2 % 10;
            if (a == 1)
              {
              x = x + 4;
              }
            
            cout << x << endl;
            
          num2 = num2 / 10;
          
            a= num2 % 10;
            if (a == 1)
              {
              x = x + 8;
              }
              
              cout << x << endl;
              
          num2 = num2 / 10;
          
            a= num2 % 10;
            if (a == 1)
              {
              x = x + 16;
              }
              
              cout << x << endl;
              
          num2 = num2 / 10;
          
            a= num2 % 10;
            if (a == 1)
              {
              x = x + 32;
              }
              
              cout << x << endl;
              
          num2 = num2 / 10;
          
            a= num2 % 10;
            if (a == 1)
              {
              x = x + 64;
              }
              
              cout << x << endl;
              
          num2 = num2 / 10;
          
            a= num2 % 10;
            if (a == 1)
              {
              x = x + 128;
              }
    Talitore - Using DEV-C++

  2. #2
    Registered User
    Join Date
    Oct 2002
    Posts
    29
    Never mind i spoke to soon. I got it to work. I just changed all the "num2" to "num"
    Talitore - Using DEV-C++

  3. #3
    UNBANNED OneStiffRod's Avatar
    Join Date
    Jan 2002
    Posts
    669
    The modulus (%) returns the REMAINDER after division. So if you divide something that returns 100000.5 the modulus would return 5 as the result.

    Also, an INT can only hold a number max of 128, if unsigned 256. Try a long or something bigger if you're using values like 10,000.

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Originally posted by OneStiffRod
    Also, an INT can only hold a number max of 128, if unsigned 256. Try a long or something bigger if you're using values like 10,000.
    You mean a CHAR, right?
    Also, I believe that the signed/unsigned behaviour of a char is undefined without specifying it, so it's up to the compiler (mine makes it unsigned).
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    UNBANNED OneStiffRod's Avatar
    Join Date
    Jan 2002
    Posts
    669
    >>You mean a CHAR, right?

    NOPE. although he says char in his post his code is about NUMS.
    //////////////////////////////////////////////////////////////////////////////////
    The following is for his info...

    An int is 1byte long 00000000 or 8 bits, each bit can hold 1 of 2 values, either a 1 or 0.

    2^8 or 2 to the 8th power is 256, that's the max an int or value that's 1byte long can hold. A signed INT, which is the defualt for INT and other number vars, takes 1bit as the sign for negative values so it's 2^7 = 128 positive and -127 negative 0 is counted as a positive value.

    Code:
    So an INT is... 1byte
    INT                               -127 to 128
    unsigned INT                 0 to 255     only positive values (256 total values counting 0)
    A LONG is....2bytes
    LONG                             -32,767 to 32,768
    unsigned LONG               0 to 65,535
    My Avatar says: "Stay in School"

    Rocco is the Boy!
    "SHUT YOUR LIPS..."

  6. #6
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Originally posted by OneStiffRod
    >>You mean a CHAR, right?

    NOPE. although he says char in his post his code is about NUMS.
    //////////////////////////////////////////////////////////////////////////////////
    The following is for his info...

    An int is 1byte long 00000000 or 8 bits, each bit can hold 1 of 2 values, either a 1 or 0.

    2^8 or 2 to the 8th power is 256, that's the max an int or value that's 1byte long can hold. A signed INT, which is the defualt for INT and other number vars, takes 1bit as the sign for negative values so it's 2^7 = 128 positive and -127 negative 0 is counted as a positive value.

    Code:
    So an INT is... 1byte
    INT                               -127 to 128
    unsigned INT                 0 to 255     only positive values (256 total values counting 0)
    A LONG is....2bytes
    LONG                             -32,767 to 32,768
    unsigned LONG               0 to 65,535
    An int is not 1 byte, it's 4 bytes (2 bytes in DOS).
    And since 2^(4*8) = 4294967296, there is plenty of room for his numbers.

    Compile this program and you'll see. I've also attached the output (the picture below).
    Code:
    #include <iostream.h>
    
    int main()
    {
       cout << "int         " << sizeof(int) << endl;
       cout << "char        " << sizeof(char) << endl;
       cout << "short int   " << sizeof(short int) << endl;
       cout << "float       " << sizeof(float) << endl;
       cout << "double      " << sizeof(double) << endl;
       cout << "long int    " << sizeof(long int) << endl;
       cout << "long double " << sizeof(long double) << endl;
       return 0;
    }
    Also, there is no datatype called long and short, they are just modifiers for int (long can also modify a double). But usually, the compiler allows you to just type long instead of long int.
    Attached Images Attached Images  
    Last edited by Magos; 11-05-2002 at 12:14 PM.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  7. #7
    UNBANNED OneStiffRod's Avatar
    Join Date
    Jan 2002
    Posts
    669
    Ok. I give to you bro...

    I get my info from books that tell me an int is 1byte. Obviously it's implementation dependant. Is it the same in UNIX?
    My Avatar says: "Stay in School"

    Rocco is the Boy!
    "SHUT YOUR LIPS..."

  8. #8
    Shadow12345
    Guest
    OneStiffRod's numbers are the same that are in some of my books too. However my books also say that the size varies. So, in short, you're both correct?????

  9. #9
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Of course it's compiler/implementation dependant, but having just 1 byte as an integer seems rather inconvenient. Are your books old?

    Try and compile/run that code, and see what results you get.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  10. #10
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    My book says 2, not 1 or 4!!

  11. #11
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    The compiler decides the size, not the book .
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  12. #12
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Originally posted by OneStiffRod
    Ok. I give to you bro...

    I get my info from books that tell me an int is 1byte. Obviously it's implementation dependant. Is it the same in UNIX?
    a char must be exactly one byte.
    a byte must have at least eight bits.
    a short must be at least two bytes.
    an int must be at least two bytes.
    a long must be at least four bytes.

    you are wrong.
    hello, internet!

  13. #13
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    moi, on unicode systems, char's are two bytes.

  14. #14
    UNBANNED OneStiffRod's Avatar
    Join Date
    Jan 2002
    Posts
    669
    My Avatar says: "Stay in School"

    Rocco is the Boy!
    "SHUT YOUR LIPS..."

  15. #15
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Originally posted by OneStiffRod
    http://msdn.microsoft.com/library/de...ype_ranges.asp

    Shut YOUR LIPS moi........
    no, you shut up and go home. i posted the ansi requirements on a conforming C compiler. microsoft visual c++, whose specifications you have posted, is only ONE of many C compilers, and it's data storage types meet or exceed the standard requirements. (my conforming implementation, for example, uses 12 byte long doubles). please stop talking about things you know nothing about, of course first tell me where the hell you got that one byte int from first....
    hello, internet!

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21