Can Someone please help me? I worte this code it compiles fine at first round but when it loops it doesnt print out properly. I cant find the mistake.. Thank you! When i compile it with Xcode first it gave the right square hollow but when i input second time it did not print out any square.
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
#include <string>
using std::string;
#include <cstdlib>
int main()
{
int a;
int b;
int num=0;
string buf;
//initialize a and b
a = 1;
b = 1;
// ask user to repeat the process again at end of the first promt
while( true )
{
cout << "Please enter size of square between #1-20: \n";
cin >> buf; num = atoi (buf.c_str());
cin.ignore(1000, 10);
if( num < 1 || num > 20 )
break;
//process of printing square
while ( num >= a)
{
b = 1;
while ( num >= b )
{
if ( a == 1 || a == num || b == 1 || b == num )
cout << "*";
else
cout << " ";
b++;
}
cout << endl;
a++;
}
}
}
You are not resetting the a and b variables, you need to put these lines
at the beginning inside of the
while(true)
loop