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;
}
choicebefore it's initialized in the first iteration ofwhile(choice != 'n').