Why is this loop infinite?

while loop is repeating

Page 1 of 1

9 Replies - 621 Views - Last Post: 05 April 2010 - 10:09 PM Rate Topic: -----

#1 beetle_slayer  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • 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  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 971
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#3 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#4 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 520
  • View blog
  • Posts: 5,596
  • 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.
Was This Post Helpful? 0
  • +
  • -

#5 Tapas Bose  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 23
  • View blog
  • 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;
                }
        }

Hope this will help you.

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

Was This Post Helpful? 0
  • +
  • -

#6 codeaholic  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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.
READ the code.
Make sure you UNDERSTAND it

and don't forget to thank me if you think I helped :D ;)
Was This Post Helpful? -2
  • +
  • -

#7 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#8 beetle_slayer  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#9 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1046
  • View blog
  • Posts: 4,449
  • 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;
}


Was This Post Helpful? 0
  • +
  • -

#10 beetle_slayer  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • 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;

}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1