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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
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.