stuck in a loop

This is a discussion on stuck in a loop within the C++ Programming forums, part of the General Programming Boards category; Can someone help me out a little and point out my error here.... I got the program to run, but ...

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    48

    stuck in a loop

    Can someone help me out a little and point out my error here....

    I got the program to run, but it should end after five runs. Instead it goes on and on and on..... Thanks.

    Code:
    #include <iostream>
    #include <cmath>
    using namespace std;
    
    //Functions used ...
    void instructions ();	
    int calculations ();
    //-----------------------------------------------------
    int main ()
    {
    	instructions ();
    
    	for(int x=0; x<=5; ++x)
    	calculations ();
    	
    	return 0;
    }
    //------------------------------------------------
    int calculations ()
    {
    	int numNumbers = 0;
    	do
    	{
    		cout << "Enter the number of numbers in your group "
    			 << "and then press the <Enter> key. "; 
    		cin	 >> numNumbers;
    		cout << endl;
    
    		if(numNumbers <= 0)
    			cout << "Error! You must enter a number greater than 0." << endl << endl;
    	}
    	while(numNumbers <= 0);
    
    	int largest = 0;
    	int smallest = 0;
    	float total = 0.0;
    
    	for (int counter = 0; counter < numNumbers; ++counter)
    	{
    		int num;
    		cout << "Enter a number ";
    		cin >> num;
    		total += num;
    
    		if(counter == 0)
    		{
    			largest = num;
    			smallest = num;
    		}
    		else if (num > largest)
    		{
    			largest = num;
    		}
    		else if (num < smallest)
    		{
    			smallest = num;
    		}
    	}
    
    	float avg = total / numNumbers;
    
    	cout << "The largest number in the group is " << largest << endl;
    	cout << "The smallest number in the group is " << smallest << endl;
    	cout << "The average of the numbers are " << avg << endl;
    
    	cout << endl;
    
    	return 0;
    }
    Thanks.

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    1,198
    Code:
    	
    for(int x=0; x<=5; x++)
        calculations ();
    should be
    Code:
    for(int x=0; x<5; x++)
        calculations ();
    since you put x to 0 when you created the variable in the loop it will loop six times if you do it the way as your code shows.

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    48
    Thanks, I did fix that to read x<5.

    I noticed that when I run the program, if I use a group sized 0 or negative of any number, it changes the number of times the loop runs. If I enter groups of numbers 4, 4, 1 , 0, -2.....it will continue for 2 more loops. Is there a way to make it recognize that zero or a negative # group should count toward the total number of loops?

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    1,198
    Well its actually not the for loop in main that continues again, its the do while loop in calculations. To make the program do what you want it to do you can just do something like:
    Code:
    if(numNumbers <= 0)
        {
            cout << "Error! You must enter a number greater than 0." << endl << endl;
            return 1;
        }

  5. #5
    Registered User
    Join Date
    Sep 2003
    Posts
    48
    Thanks! That fixed it perfectly.
    Everything else I've done has always been return 0. I didn't even think of changing it to 1.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Rewriting a for loop as a while/do-while loop
    By Ashfury in forum C++ Programming
    Replies: 7
    Last Post: 04-27-2007, 02:20 PM
  2. return to start coding?
    By talnoy in forum C++ Programming
    Replies: 1
    Last Post: 01-26-2006, 02:48 AM
  3. loop needed also how to make input use letters
    By LoRdHSV1991 in forum C Programming
    Replies: 3
    Last Post: 01-13-2006, 04:39 AM
  4. Help! Stuck in a loop!
    By raell in forum C++ Programming
    Replies: 2
    Last Post: 12-17-2003, 09:47 AM
  5. Stuck in a loop!.....Get me out of here!!
    By rabmaz in forum C Programming
    Replies: 3
    Last Post: 09-01-2002, 09:16 AM

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