### Creating squares & triangles using for loops

Hi I'm doing an exercise on Accelerated C++ book chapter 2.. I have to create a square using the for loop by executing row by row (i think?) anyway here's the code:
 ``1234567891011121314151617181920212223`` ``````const int sqrrows = 4; // rows to build const string sqrwidth = "\t"; // using tab as space between the square? const string::size_type cols = sqrwidth.size(); // tabs' size for (int r=0; r!=sqrrows; ++r) { string::size_type cm = 0; while (cm != cols){ if ( r == 0 || r == sqrrows -1 || cm == 0 || cm == cols -1) { cout << "*"; } } if (r == 1 || r == sqrrows - 2) { cout << sqrwidth; ++r; } cout << endl; }``````

Here's the full code of what I was doing - which is similar to what I'm trying to do and where I'm trying to follow:
 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677`` ``````#include #include #include #include #include using namespace std; inline void keep_window_open() {char ch;cin>>ch;} int main() { cout << "how many padding would you like? "; int pad = 1; cin >> pad; cout << endl; cout << "Hello, enter your name! "; string name; cin >> name; const string greeting = "Enjoy the show " + name + "!"; // defining the rows and columns const int rows = pad * 2+3 ; const string::size_type cols = greeting.size() + pad * 4 + 2; cout << endl; // write rows, invariant = r so we know what we've written so far for (int r = 0; r != rows; ++r) { string::size_type c = 0; // invariant to know how many characters we have while (c != cols) { // is it time to write the greeting? if (r == pad + 1 && c == pad + 1) { cout << greeting ; c += greeting.size(); }else { //are we on borders? if (r == 0 || r == rows - 1 || c == 0 || c == cols - 1) cout << "*" ; else cout << " "; ++c;- } } cout << endl; } const int sqrrows = 4; // rows to build const string sqrwidth = "\t"; // using tab as space between the square? const string::size_type cols = sqrwidth.size(); // tabs' size for (int r=0; r!=sqrrows; ++r) { string::size_type cm = 0; while (cm != cols){ if ( r == 0 || r == sqrrows -1 || cm == 0 || cm == cols -1) { cout << "*"; } } if (r == 1 || r == sqrrows - 2) { cout << sqrwidth; ++r; } cout << endl; } keep_window_open(); return 0; }``````

any help greatly appreciated!
Topic archived. No new replies allowed.