Trouble with a counting loop

This is a discussion on Trouble with a counting loop within the C++ Programming forums, part of the General Programming Boards category; Hello. My program takes some preset numbers, for instance 20 to 30, then calculates for each number whether its proper ...

  1. #1
    Registered User
    Join Date
    Jan 2004
    Posts
    49

    Trouble with a counting loop

    Hello. My program takes some preset numbers, for instance 20 to 30, then calculates for each number whether its proper divisors when added together, are less than, more than, or equal to the original number. It worked fine as a simple, run "only once and restart" program, but then I set it up using a "while loop" menu and found an unexpected behavior. The first time through, no matter which series of numbers I select, it works fine. Subsequent runs adds "10" to the desired numbers. For example, the first time through it checks 20, 21...30. The next time through, rather than rechecking 20-30, it now checks 31...32...to 41.
    I tried using some flush commands at the end of suspect statements, but no luck there.
    I would appreciate any help.
    Code:
    #include <iostream>    // cout, cin, <<, >>
    #include <iomanip>     // maybe I will use this, maybe not.
    #include <string>      // string
    #include <cstdlib>     // exit()
    using namespace std;
    
    
    int sumOfDivisors(int getNum);
    
    int x = 0;
    int sum = 0;
    int getNum = 0;
        
    
    int main()
    {
    
    	const string MENU =
    		"This program will check several series of numbers\n"
            "to see if they are deficient, perfect or abundant.\n"
    		"Enter the appropriate key to check a particular series\n"
    		"of numbers.\n"
    		" a - 20 to 30;\n"
    		" b - 490 to 500;\n"
    		" c - 8120 to 8130;\n"
    		" q - to quit;\n"
    		"---> ";
    	cout << MENU;            
    
        char choice;
    	cin >> choice;
        cout << "\n";
    
    	while (choice != 'q')
    	{
    	
        switch(choice)
    	{
    	case 'a':
    		  getNum = 19 + x;
    		  break;
    		
    	case 'b':
    		  getNum = 489 + x;
    		  break;
    		
    	case 'c':
    		  getNum = 8119 + x;
    		  break;
    		
    	case 'q':
    		exit(1);
    	}
    	
        for (x = 0; x <= 10; ++x)
    	{
    		getNum++;	      
    	
        int sum = sumOfDivisors(getNum);
    	
    	cout << "The sum of the divisors for "
    		 << getNum << " is: " << sum << "\n\n" << flush;
    	if (sum < getNum)
    		cout << "This number is deficient.\n\n"<< flush;
    	else if (sum == getNum)
    		cout << "This number is perfect!\n\n" << flush;
    	else
    		cout << "This number is abundant.\n\n" << flush;
    	    cout << "The variable getNum now equals: " << getNum << "\n";
    	}
    	cout << "\n" << endl;
        cout << MENU;
    	cin >> choice;
    	}
      }
    	
    /*******************************************************************
     * sumOfDivisors() calculates the number of divisors and adds them.
     *
     * Receive: getNum, an integer
     * Returns: sum, an integer
     *
     * Modification history: added menu, March 2004. ---ALT
     *********************************************************************/
    
    sumOfDivisors(int getNum)
       {
    	   int pd = 2;
    	   int sum = 1;
    
    	   while (pd < getNum)
    	   {
    		  	   if (getNum % pd == 0)
    			   sum = sum + pd;
    		   pd++;
    	   }
    	   return sum;
       }
    Semper Fi!

  2. #2
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    set the value of x to 0 just before the switch command.

    In your program, x is the loop counter that goes from 0 to 10, so x is equal to 10 at the beginning of the next loop through the while(){}


    (Better yet, ask yourself, "Why do the cases have things like the following?")
    Code:
    getnum = 19 + x;
    
    getnum = 489 + x;
    
    // etc.
    What does x do for you here?




    Dave

  3. #3
    Registered User
    Join Date
    Jan 2004
    Posts
    49

    Thumbs up

    Ahh....I can see clearly now. Your question was well posed, I don't think I will make this mistake again. Thank you!!!
    Semper Fi!

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Semper Fi?

    Anchors aweigh!

    Dave

  5. #5
    Registered User
    Join Date
    Jan 2004
    Posts
    49
    Isn't that just my luck...I have to get help from a swabbie...
    JUST KIDDING!!!!! My old man was career Navy. Good people.
    I got out of the USMC in 1987.
    Semper Fi!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Visual Studio Express / Windows SDK?
    By cyberfish in forum C++ Programming
    Replies: 23
    Last Post: 01-22-2009, 01:13 AM
  2. Replies: 1
    Last Post: 10-27-2006, 01:21 PM
  3. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 10:21 PM
  4. trouble with a for loop with an if statement nested in
    By phoenix-47 in forum C++ Programming
    Replies: 4
    Last Post: 12-14-2005, 03:24 PM
  5. I need help as soon as possible.
    By hyrule in forum C++ Programming
    Replies: 7
    Last Post: 11-09-2005, 04:49 PM

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