# stuck in a loop

• 10-14-2003
sweetly
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.
• 10-14-2003
Shakti
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.
• 10-14-2003
sweetly
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?
• 10-14-2003
Shakti
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;     }```
• 10-14-2003
sweetly
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.