Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

chris200x9

macrumors 6502a
Original poster
Jun 3, 2006
906
0
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;
}
 

CaptainZap

macrumors regular
Jan 17, 2007
170
0
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;
}

I would check out your variable names in your while loop. Your num1 is doing nothing.
 

toddburch

macrumors 6502a
Dec 4, 2006
748
0
Katy, Texas
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
 

chris200x9

macrumors 6502a
Original poster
Jun 3, 2006
906
0
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

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
 

HiRez

macrumors 603
Jan 6, 2004
6,265
2,630
Western US
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
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.
 

toddburch

macrumors 6502a
Dec 4, 2006
748
0
Katy, Texas
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
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.