0

I have made a program where it creates a rectangle according to the users input. If the user inputs 8, the program will make a 8 x 8 rectangle. I have all of the code to make a rectangle. But what i am trying to do is have the program ask the user whether they would like to recreate another rectangle, which it does, but every time the program asks for new input it displays the old rectangle that was created before. Not the new one the user asked to be created. How do i get rid of the input buffer from before to be able to create a new rectangle? Thanks!

#include <iostream>

using namespace std;

int main ()

{
    //declaring variables
    int size;
    char choice;

    cout << "**************** Drawing Squares Program ******************************" << endl;
    cout << "* Algorithm generates a hollow square, using the character +, - and | *" << endl;
    cout << "* Acceptable size dimension: Any value from 3 to 20. Choose carefully.*" << endl;
    cout << "***********************************************************************" << endl;
    
    cout << "Side size = ";
    cin >> size;
    
    while (size < 3 || size > 21)
    {
        cout << endl;
        cout << "Number is either too big or small. Please rechoose." << endl;
        cout << endl;
        cout << "Side Size: ";
        cin >> size;
    }
    
    while (choice != 'n')
    {
        //the beginning for loop will create one single + in the beginning of the line.
        for (int FirstL = 0; FirstL < 1; FirstL++)
        {
            cout << '+';
                
            //for loop creates the line across the top. It starts at two because we have + on both ends of the line.
            for (int straightTop = 2; straightTop < size; straightTop++)
            {
                cout << "-";
            }
                
            cout << "+"; //creates the + at the end of the line. (For top row)
            cout << endl;
                
            //this for loop creates the vertical line. It is a nedsted for loop because the for loop has to create two of the same exact vertical lines side by side but with space inbetween them so the line goes across to the other side.
            for (int VerticalLine = 2; VerticalLine < size; VerticalLine++)
            {
                cout << "|";
                        
                for (int verticalSpace = 2; verticalSpace < size; verticalSpace++)
                {
                    cout << " "; //creates the space between both vertical lines.
                }
                        
                    cout << "|\n"; //\n is another form of endl;
                }
                
                cout << "+"; //creates + at the end of the vertical line
                
                //for loop creates the bottom line.
                for (int straightBottom = 2; straightBottom < size; straightBottom++)
                {
                    cout << "-";
                }   
                
                //creates + at the end of the line.
                cout << "+" << endl;
        }
        
        cout << "Great! Would you like to play again?: ";
        cin >> choice;
        
        if (choice == 'n')
        {
            cout << "Thanks for playing!" << endl;
        }
        
        if (choice == 'y')
        {
            size = 0;
        }
    }
    
    return 0;
}
1
  • 2
    Your program has undefined behavior since it reads choice before it's initialized in the first iteration of while(choice != 'n'). Commented Feb 3, 2021 at 21:59

1 Answer 1

2

You ask for the size outside of your running loop. So your program flow looks like

  1. Ask for size
  2. Print rectangle
  3. Ask if they want another
  4. If yes, return to step 2

You need to move that size code into the loop. So it'll look like:

while (choice != 'n')
{
    
    cout << "Side size = ";
    cin >> size;
    
    while (size < 3 || size > 21)
    {
        cout << endl;
        cout << "Number is either too big or small. Please rechoose." << endl;
        cout << endl;
        cout << "Side Size: ";
        cin >> size;
    }

    // ...

You also never initialize choice before the loop. You need to initialize it to be 'y'. That'll be easiest when you declare it (this is also a good habit to get into whenever declaring variables):

char choice = 'y';

Live example: https://ideone.com/EwE5iz

Sign up to request clarification or add additional context in comments.

2 Comments

I have been ripping my hair out for the past 3 DAYS trying to figure out how to get it to work. And you solved it in 5. Thank you! I cannot thank you enough! @scohe001
@EduardoMunozAlvarez the programmer's best friend is the debugger. With the debugger you can step through the program line-by-line and watch what happens as it happens. Often just watching what happens is enough to go. "Oh. Oh smurf." and add the little bit you're missing ow move a line to where it's needed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.