Cant get out of infinite while loop

This is a discussion on Cant get out of infinite while loop within the C++ Programming forums, part of the General Programming Boards category; hey im stuck on this problem. whatever i do, i cant get out of the infinite while loop when a ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    3

    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. #2
    Registered User
    Join Date
    Oct 2004
    Posts
    32
    You need to initialize num I think. You test it before it ever has a value.

  3. #3
    Arggggh DeepFyre's Avatar
    Join Date
    Sep 2004
    Posts
    227
    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
    Keyboard Not Found! Press any key to continue. . .

  4. #4
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>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.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  5. #5
    Budding Synth Programmer samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Trefforest
    Posts
    368
    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...
    MSVC++ 6.0

  6. #6
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>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.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  7. #7
    Registered User
    Join Date
    Mar 2004
    Posts
    220
    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:
      {
                  std::cout << "Out of bounds error, please email admin@domain.com the details."
                                 << "\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*..
    Last edited by Tronic; 04-12-2005 at 03:59 PM.
    OS: Windows XP Pro CE
    IDE: VS .NET 2002
    Preferred Language: C++.

  8. #8
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    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.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  9. #9
    Registered User
    Join Date
    Apr 2005
    Posts
    3
    ok everything works perfect. thanks
    Last edited by elusive; 04-12-2005 at 09:05 PM.

  10. #10
    Registered User
    Join Date
    Apr 2005
    Posts
    3
    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);
    Last edited by Sharpi; 04-12-2005 at 11:02 PM.

  11. #11
    Registered User
    Join Date
    Mar 2004
    Posts
    220
    Quote 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..
    OS: Windows XP Pro CE
    IDE: VS .NET 2002
    Preferred Language: C++.

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  13. #13
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>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.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 06-14-2009, 11:24 PM
  2. Cosine fucntion and infinite loop.
    By youareafever in forum C Programming
    Replies: 2
    Last Post: 11-07-2008, 03:45 AM
  3. Infinite Loop with GetAsyncKeyState
    By guitarist809 in forum Windows Programming
    Replies: 1
    Last Post: 04-18-2008, 12:09 PM
  4. Switch statement = infinite loop
    By Lucid003 in forum C++ Programming
    Replies: 10
    Last Post: 10-10-2005, 12:46 AM
  5. stays in loop, but it's not an infinite loop (C++)
    By Berticus in forum C++ Programming
    Replies: 8
    Last Post: 07-19-2005, 11:17 AM

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