Discussion in 'Mac Programming' started by chris200x9, Nov 12, 2007.

1. chris200x9 macrumors 6502a

Joined:
Jun 3, 2006
#1
the output of this is just th same number, why wouldnt it be the total of all the numbers that make it up?

Code:
```#include <iostream>
using namespace std;

int main () {
int num;

cout << "input a number" << endl;
cin >> num;
cout << endl;
while (num < 0 )
{
num1= num;
num = num + num;

num--;
}
cout << num;
return 0;
}
```

2. CaptainZap macrumors regular

Joined:
Jan 17, 2007
#2
Code:
```#include <iostream>
using namespace std;

int main () {
int num;

cout << "input a number" << endl;
cin >> num;
cout << endl;
while (num < 0 )
{
[B]num1[/B]= num;
num = num + num;

num--;
}
cout << num;
return 0;
}
```

3. toddburch macrumors 6502a

Joined:
Dec 4, 2006
Location:
Katy, Texas
#3
If you consider this logic,

Code:
```  while (num < 0 )
{
num1= num;
num = num + num;

num--;
}
```
Let's run an example number through. Let's pick 4.

while (4 < 0) { .... }

Humm. What's wrong with that picture?

Todd

4. chris200x9 thread starter macrumors 6502a

Joined:
Jun 3, 2006
#4
omfg I feel more retarded than those people in that one current event article on here

I dont get why it just outputs the negative limit of integers

5. HiRez macrumors 603

Joined:
Jan 6, 2004
Location:
Western US
#5
Probably because it loops throught that many times, decrementing num by 1 each time until it reaches the limit. At that point I'm not sure what happens. I suppose it either wraps around, or zeroes out all the bits thereby making num >= 0 and terminating the loop.

6. toddburch macrumors 6502a

Joined:
Dec 4, 2006
Location:
Katy, Texas
#6
I don't know what you did to change the logic since my last post, but if you just flipped the relational operator to this:
Code:
```while (num [color=red]>[/color] 0 )
{
num1= num;
num = num + num;

num--;
}
```
then that might not be getting you to where you want to go either.

Let's look at the logic again. We'll pick 4 again.
Code:
```while (4 > 0 )
{
//num1= num;   not sure what this line is supposed to do???
num = 4 + 4 ;   // double num

num--;      // subtract one - it's now 7
}
```
Every iteration will double the number and then subtract 1. When num, a 4 byte integer, reaches one more than X'7FFFFFFF' (2gb), it will go negative, since the sign bit will be on, and the WHILE loop will quit.

What is your loop supposed to be doing?

Todd