# Why is this loop infinite?

Page 1 of 1

## 9 Replies - 833 Views - Last Post: 05 April 2010 - 10:09 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=166177&amp;s=9822f54c1c0fe151ceb1ca2b93584346&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 beetle_slayer

Reputation: 1
• Posts: 16
• Joined: 29-March 10

# Why is this loop infinite?

Posted 04 April 2010 - 10:39 PM

My assignment is to choose a number from 7 to 15 and print a square x rows by x columns. I have followed an example in the book, but when I use a while loop, it repeats forever. Why is this happening?
```#include <iostream>

#include <string>

using namespace std;

int main( )

{

int number=0,x=0,y=0;

cout << "Please enter a number from 7 to 15 or 0 to quit.";

cin >> number;

while (number != 0)

{

if (number >=7 && number <=15)

{

for (int x = 1; x <= number; x++ )

{

for (int y = 1; y <= number; y++ )

cout << "*";

cout << '\n';

}

}

else if (number <7 || number > 15)

{

cout << "Invalid entry." << endl;

cout << "Please enter a number from 7 to 15 or 0 to quit. ";

cin >> number;

}

}

return 0;

}

```

Is This A Good Question/Topic? 0

## Replies To: Why is this loop infinite?

### #2 n8wxs

• --... ...-- -.. . -. ---.. .-- -..- ...

Reputation: 972
• Posts: 3,878
• Joined: 07-January 08

## Re: Why is this loop infinite?

Posted 04 April 2010 - 10:47 PM

Put a break; statement after the for() body.

This post has been edited by n8wxs: 04 April 2010 - 10:47 PM

### #3 erik.price

• D.I.C Lover

Reputation: 485
• Posts: 2,690
• Joined: 18-December 08

## Re: Why is this loop infinite?

Posted 04 April 2010 - 10:47 PM

Because if this if statement evaluates to true:
```if (number >=7 && number <=15)

{

for (int x = 1; x <= number; x++ )

{

for (int y = 1; y <= number; y++ )

cout << "*";

cout << '\n';

}

}
```

Then you don't ask the user to enter another number, so the number will stay the same, and the loop will keep repeating.

Prompt the user for another choice after the outer for loop

### #4 Locke

• Sarcasm Extraordinaire!

Reputation: 526
• Posts: 5,604
• Joined: 20-March 08

## Re: Why is this loop infinite?

Posted 04 April 2010 - 10:49 PM

In addition to what erik.price said...Just switch else if to else, as that would eliminate useless computation since we're talking about valid or invalid input. If it ain't valid, then it's invalid.

The program will always prompt for input again, even if an invalid value was entered. If another valid number is entered (after the first execution), it'll print that one out, too. Once the user inputs 0, it will stop.

### #5 Tapas Bose

• D.I.C Regular

Reputation: 23
• Posts: 472
• Joined: 09-December 09

## Re: Why is this loop infinite?

Posted 04 April 2010 - 10:53 PM

If you want to exit the loop after getting proper input and displaying "*" you can can try it :
```        // this is your modified while loop
while (number != 0)
{
if (number >=7 && number <=15)

{
for (int x = 1; x <= number; x++ )
{

for (int y = 1; y <= number; y++ )
cout << "*";

cout << '\n';
}

number = 0; // I have added this line, this will make number = 0 and that makes
// the while condition false, hence loop will terminate.
}

else if (number <7 || number > 15)
{
cout << "Invalid entry." << endl;

cout << "Please enter a number from 7 to 15 or 0 to quit. ";

cin >> number;
}
}
```

This post has been edited by Tapas Bose: 04 April 2010 - 10:54 PM

### #6 codeaholic

Reputation: 0
• Posts: 27
• Joined: 06-November 09

## Re: Why is this loop infinite?

Posted 05 April 2010 - 12:07 AM

since you have tried yourself, I think you deserve to get the answer.

```#include <iostream>
#include <string>

using namespace std;

int main( )

{

int number=0,x=0,y=0;

do{

cout << "Please enter a number from 7 to 15 or 0 to quit.";
cin >> number;

}
while ( number > 15 || number < 7 && number !=0 );

while ( x < number)
{
for (; y< number; y++)
cout << "*"; // creates on row of * s

cout << "\n";
x++;
y = 0; // initilases y to 0 so the inner loop execute agian

}

system("PAUSE");
return EXIT_SUCCESS;

}

```

This will do the job.
Make sure you UNDERSTAND it

and don't forget to thank me if you think I helped

### #7 erik.price

• D.I.C Lover

Reputation: 485
• Posts: 2,690
• Joined: 18-December 08

## Re: Why is this loop infinite?

Posted 05 April 2010 - 12:37 AM

@codeaholic no need to completely change the structure of the OP's program. It will work fine once one of the strategies we suggested earlier in the thread is implemented

This post has been edited by erik.price: 05 April 2010 - 12:37 AM

### #8 beetle_slayer

Reputation: 1
• Posts: 16
• Joined: 29-March 10

## Re: Why is this loop infinite?

Posted 05 April 2010 - 09:10 PM

I added a cout statement inside the outer loop. As I was reviewing my book, it is indeed a common mistake as the author points out to forget to put the cout/cin insode the loop. This is extra credit and the second part is to make the square hollow such as:
enter a number: 8
********
*------*
*------*
*------*
*------*
*------*
*------*
********
If I had to guess, I would say it would involve putting another for loop inside the inner loop. Is this a messy idea? Or is their a more efficient way of doing this?

Here is my updatd code working properly for part one.
```#include <iostream>

#include <string>

using namespace std;

int main( )

{

int number=0,x=0,y=0;

cout << "Please enter a number from 7 to 15 or 0 to quit. ";

cin >> number;

while (number != 0)

{

if (number >= 7 && number <= 15)

{

for (int x = 1; x <= number; x++)

{

for (int y = 1; y <= number; y++)

cout << "*";

cout << '\n';

}

cout << "Please enter a number from 7 to 15 or 0 to quit. :";

cin >> number;

}

else if (number < 7 || number > 15)

{

cout << "Invalid entry." << endl;

cout << "Please enter a number from 7 to 15 or 0 to quit. ";

cin >> number;

}

}

return 0;

}

```

This post has been edited by beetle_slayer: 05 April 2010 - 09:11 PM

### #9 jjl

• Engineer

Reputation: 1120
• Posts: 4,646
• Joined: 09-June 09

## Re: Why is this loop infinite?

Posted 05 April 2010 - 09:17 PM

i dont understand the purpose of your while loop, put the while loop in where your entering your number. Keep looping until conditions are met
```#include <iostream>

using namespace std;

int main()
{
int number=0;
do{
cout << "Please enter a number from 7 to 15 or 0 to quit.";
cin >> number;
}while(number<7||number>15); //keep looping while number is less that 7 or greater than 15

for (int x = 0; x < number; x++ )
{
for (int y = 0; y < number; y++ )
cout << "*";
cout <<endl;
}
cin.ignore();
cin.get();

return 0;
}

```

### #10 beetle_slayer

Reputation: 1
• Posts: 16
• Joined: 29-March 10

## Re: Why is this loop infinite?

Posted 05 April 2010 - 10:09 PM

OK, the instructor wanted while loops so I revised and here is my final code. How would I go about getting rid of the "break" in the second loop?
```#include <iostream>

#include <string>

using namespace std;

int main( )

{

int number;

cout << "Please enter a number from 7 to 15 or 0 to quit. ";

cin >> number;

while (number != 0)

{

while (number >= 7 && number <= 15)

{

int x = 1;

while (x++ <= number)

{

int y = 1;

while (y++ <= number)

cout << "*";

cout << '\n';

}

cout << '\n';

//beginning of second square

int a = 0;

while (a++ <= number)
{

int b = 1;

while (b++ <= number)

cout << "*";

cout << '\n';

int c = 2;

while (c++ < number)

{
cout << "*";

int d = 2;

while (d++ < number)

cout << " ";

cout << "*";

cout << '\n';

}

int e = 1;

while (e++ <= number)

cout << "*";

cout << '\n';

break; // I know this is not good practice, but I need it here.

}

cout << "Please enter a number from 7 to 15 or 0 to quit. :";

cin >> number;

}

while ((number < 7 || number > 15) && (number != 0))

{

cout << "Invalid entry." << endl;

cout << "Please enter a number from 7 to 15 or 0 to quit. ";

cin >> number;

}

}

return 0;

}

```