#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;
}
Why is this loop infinite?while loop is repeating
Page 1 of 1
9 Replies - 518 Views - Last Post: 05 April 2010 - 10:09 PM
#1
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?
Replies To: Why is this loop infinite?
#2
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
Re: Why is this loop infinite?
Posted 04 April 2010 - 10:47 PM
Because if this if statement evaluates to true:
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
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
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.
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
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 :
Hope this will help you.
// 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
#6
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.
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
#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
#7
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
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.
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
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
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;
}
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote






|