# Thread: Cant get out of infinite while loop

1. ## Cant get out of infinite while loop

hey im stuck on this problem. whatever i do, i cant get out of the infinite while loop when a negative number is entered. i want it to end the program when a number less than 0 is entered. when i put any negative number, it repeats

"Divisors for "num" are:
1"

thanks

Code:
```#include <iostream>
using namespace std;
int main()
{
int x, num, sum=0, test;

while (num>0) {
cout<<"\nEnter a positive number between 1 and 1000."<<endl;
cout<<"Enter -99 to exit.\n"<<endl;
cin>>num;

do {
test=5;
cout<<"\nDivisors for "<<num<<" are: "<<endl;
cout<<" 1";
for (x=2;x<num;x++)
{
if ((num%x)==0)
{
cout<<"  "<<x;
test=0;
sum=(sum+x);
}
else if (test!=0)
test=1;
}

sum=(sum+1);
if (num==sum)
cout<<"\nThe number "<<num<<" is a perfect number"<<endl;

if (test==1)
{ cout<<" "<<num<<endl;
cout<<"\nThe number "<<num<<" is prime."<<endl;
}
} while (num<0 || num>1000);
}

system("pause");			//Pauses the console
return 0;
}```

2. You need to initialize num I think. You test it before it ever has a value.

3. wait, i dont see why it should even go into the first while loop because on my compiler itd give an error saying that num is not a number (because its an address) and cant compare it to 0

4. >>saying that num is not a number (because its an address) and cant compare it to 0
Not sure why.
>int x, num, sum=0, test;

>>and cant compare it to 0
Dunno man, how do you figure we check for NULL pointers? Although perhaps only the == operator is defined for comparison with int, I doubt it.

**EDIT**
I'll be back tomorrow with a more useful reply, I'm going to sleep.

5. Change the first loop to a do-while loop:

Code:
```do
{
cout<<"\nEnter a positive number between 1 and 1000."<<endl;
cout<<"Enter -99 to exit.\n"<<endl;
cin>>num;
} while (num>0);```
That way, num is always assigned before it is tested (and you should always use do-whiles if the body of the loop must execute at least once). I noticed you don't test for -99 yet either...

6. >>when i put any negative number, it repeats
Alright, well if everything else inside the loop is working fine and you just want it to go back to the beginning when the input is invalid, then you can use:
Code:
```//Input into 'num', and then:
if(num < 0)
continue;```
continue just tells it to go back to the top of the loop, skipping everything below.

7. You might want to consider putting your first output into a function with an argument that relates to the state of your request. The user will never know there was an error if he puts in a negative number at the moment because you don't tell him that. I'de make something like this.

I always assume users don't read what I put in the readme's / directions / introduction. It saves my inbox ^_^

Code:
```void introState(int state);

void introState(int state)
{
switch(state)
case 1:
{          std::cout << "\nEnter a positive number between 1 and 1000."
<<     endl
<< "Enter -99 to exit.\n"
<<     endl;
break;
}
case 2:
{
std::cout << "Incorrect input, program only accepts posative integer input."
<< "\n" << std::flush;
break;
}
default:
{
<< "\n" << std::flush;
break;
}
}```
I'm not sure if switch will allow enums after the case keyword, but it's worth looking into for some added abstraction (to let you greater understand the problem at hand). Not sure if anything special outside of the realm of #define's goes on with enums.

Edit:

I'm not sure if that was sarcasm or not Hunter2.... but in case it was yeah you can compare pointers for NULL in if statements... *has been lacking in the ability to sense sarcasm lately*..

8. I'm not sure if that was sarcasm or not Hunter2.... but in case it was yeah you can compare pointers for NULL in if statements... *has been lacking in the ability to sense sarcasm lately*..
No, actually I wasn't sarcastic. I know you can check, if(ptr == NULL) or != or whatever, but I'm not 100% sure if you're allowed to compare a pointer to an integral type using >, < and related operators.

9. ok everything works perfect. thanks

10. You might want to check the part of your program that determines if the number is a perfect number. I entered several perfect numbers in and it only shows them being a perfect number everyonce in a while. I'm just home for lunch so I don't have a lot of time to explain or help fix, but I noticed that you never reset the value of sum to 0 after each time the program runs through, so this could be what is causing the problem.

Edit: Here you go, this fixes it.

Code:
```if (num==sum)
{
cout<<"\nThe number "<<num<<" is a perfect number"<<endl;
sum = 0;
}

if (test==1)
{ cout<<" "<<num<<endl;
cout<<"\nThe number "<<num<<" is prime."<<endl;
sum = 0;
}
sum = 0;    // reset sum to 0
} while (num<0 || num>1000);```

11. Originally Posted by Hunter2
No, actually I wasn't sarcastic. I know you can check, if(ptr == NULL) or != or whatever, but I'm not 100% sure if you're allowed to compare a pointer to an integral type using >, < and related operators.
Hmm...in asm you can but in C++ I'm not sure..

12. Try it and see. Your compiler will warn you if you're doing something wrong. And if you really must compare a pointer's value (address) to a direct integer, you can always cast it.

For the record, NULL is 0 in C++, so it's definately legal to compare a pointer to 0.

Quzah.

13. >>Try it and see.
Good idea Looks like all comparisons are fine with 0. It's just that I remembered laserlight getting shot down because of some assumption that pointers are synonymous with int. Come to think about it, I think it had something to do with attempting to do multiplication on a pointer or something, so it's quite unrelated to the subject at hand. My mistake.