# 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. ## 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"

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;
}```

2. Never mind i spoke to soon. I got it to work. I just changed all the "num2" to "num"

3. 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. 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).

5. >>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```

6. 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.

7. 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?

8. 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. 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.

10. My book says 2, not 1 or 4!!

11. The compiler decides the size, not the book .

12. 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.

13. moi, on unicode systems, char's are two bytes.

14. Originally posted by OneStiffRod
http://msdn.microsoft.com/library/de...ype_ranges.asp