Worlds worst programmer needing some advice.

Ok, so I have an assignment that I have some questions on. Now, I can only use the while and if statement only, so no for loops or char types. Anyways, I have to write a program that completes a hollow square from 1-20 and with a q or Q to act as a sentinel. Now, when I try to compile it, I get nothing back. Any pointers on what I'm doing wrong. Here is the code below.

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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
  // Lab 2a, practical control structures
// Programmer: 
// Editor(s) used: XP Notepad 
// Compiler(s) used: VC++ 2010 Express

#include <iostream>
using std::cin;
using std::cout;
using std::endl;

#include <string>
using std::string; 
using std::getline;

#include <cstdlib>




int main()

{
  //Jesse Burns program number 2: practical control structures
  string buf;
  int n;
  

  cout << "Enter a number between 1-20 to determine the size of the rectangle or press q to quit: " << endl;
  cin >> buf; n = atoi(buf.c_str());
  cin.ignore(1000,10);

  while(true)
	 if (buf == "Q" || buf == "q")break;
	 if (n==1)
		 cout << "*"<<endl;
	  if (n==2)
		 cout << "**"<<endl;
		 cout << "**"<<endl;
      if (n==3)
		 cout << "***"<<endl;
	     cout << "* *"<<endl;
		 cout << "***"<<endl;
      if (n==4)
		 cout <<"****"<<endl;
	     cout <<"*  *"<<endl;
		 cout <<"*  *"<<endl;
		 cout <<"*  *"<<endl;
		 cout <<"****"<<endl;
	 if (n==5)
		 cout << "*****"<<endl;
		 cout << "*   *"<<endl;
		 cout << "*   *"<<endl;
		 cout << "*   *"<<endl;
		 cout << "*****"<<endl;
	  if (n==6)
		  cout << "******"<<endl;
		  cout << "*    *"<<endl;
		  cout << "*    *"<<endl;
		  cout << "*    *"<<endl;
		  cout << "*    *"<<endl;
		  cout << "******"<<endl;
	   if (n==7)
		   cout << "*******"<<endl;
	       cout << "*     *"<<endl;
		   cout << "*     *"<<endl;
		   cout << "*     *"<<endl;
		   cout << "*     *"<<endl;
		   cout << "*     *"<<endl;
		   cout << "*******"<<endl;
		if (n==8)
			cout << "********"<<endl;
		    cout << "*      *"<<endl;
			cout << "*      *"<<endl;
			cout << "*      *"<<endl;
			cout << "*      *"<<endl;
			cout << "*      *"<<endl;
			cout << "*      *"<<endl;
			cout << "********"<<endl;
		if (n==9)
			cout << "*********"<<endl;
		    cout << "*       *"<<endl;
			cout << "*       *"<<endl;
			cout << "*       *"<<endl;
			cout << "*       *"<<endl;
			cout << "*       *"<<endl;
			cout << "*       *"<<endl;
			cout << "*       *"<<endl;
			cout << "*********"<<endl;
		if (n==10)
			cout << "**********"<<endl;
		    cout << "*        *"<<endl;
			cout << "*        *"<<endl;
			cout << "*        *"<<endl;
			cout << "*        *"<<endl;
			cout << "*        *"<<endl;
			cout << "*        *"<<endl;
			cout << "*        *"<<endl;
			cout << "*        *"<<endl;
			cout << "**********"<<endl;
		if (n==11)
			cout << "***********"<<endl;
		    cout << "*         *"<<endl;
			cout << "*         *"<<endl;
			cout << "*         *"<<endl;
			cout << "*         *"<<endl;
			cout << "*         *"<<endl;
			cout << "*         *"<<endl;
			cout << "*         *"<<endl;
			cout << "*         *"<<endl;
			cout << "*         *"<<endl;
			cout << "***********"<<endl;
		 if (n==12)
			 cout << "************"<<endl;
		     cout << "*          *"<<endl;
			 cout << "*          *"<<endl;
			 cout << "*          *"<<endl;
			 cout << "*          *"<<endl;
			 cout << "*          *"<<endl;
			 cout << "*          *"<<endl;
			 cout << "*          *"<<endl;
			 cout << "*          *"<<endl;
			 cout << "*          *"<<endl;
			 cout << "*          *"<<endl;
			 cout << "************"<<endl;
		  if (n==13)
			  cout << "*************"<<endl;
		      cout << "*           *"<<endl;
			  cout << "*           *"<<endl;
			  cout << "*           *"<<endl;
              cout << "*           *"<<endl;
			  cout << "*           *"<<endl;
			  cout << "*           *"<<endl;
			  cout << "*           *"<<endl;
			  cout << "*           *"<<endl;
			  cout << "*           *"<<endl;
			  cout << "*           *"<<endl;
			  cout << "*           *"<<endl;
			  cout << "*************"<<endl;
		   if (n==14)
			    cout << "**************"<<endl;
				cout << "*            *"<<endl;
				cout << "*            *"<<endl;
				cout << "*            *"<<endl;
				cout << "*            *"<<endl;
			    cout << "*            *"<<endl;
		        cout << "*            *"<<endl;
			    cout << "*            *"<<endl;
			    cout << "*            *"<<endl;
			    cout << "*            *"<<endl;
				cout << "*            *"<<endl;
			    cout << "*            *"<<endl;
				cout << "*            *"<<endl;
				cout << "*************"<<endl;
			if (n==15)
		    cout << "**************"<<endl;
			cout << "*            *"<<endl;
			cout << "*            *"<<endl;
			cout << "*            *"<<endl;
			cout << "*            *"<<endl;
			cout << "*            *"<<endl;
			cout << "*            *"<<endl;
			cout << "*            *"<<endl;
			cout << "*            *"<<endl;
			cout << "*            *"<<endl;
			cout << "*            *"<<endl;
			cout << "*            *"<<endl;
			cout << "*            *"<<endl;
			cout << "*            *"<<endl;
			cout << "**************"<<endl;
			if(n==16)
				cout << "***************"<<endl;
			cout << "***************"<<endl;
			cout << "*             *"<<endl;
			cout << "*             *"<<endl;
			cout << "*             *"<<endl;
			cout << "*             *"<<endl;
			cout << "*             *"<<endl;
			cout << "*             *"<<endl;
			cout << "*             *"<<endl;
			cout << "*             *"<<endl;
			cout << "*             *"<<endl;
			cout << "*             *"<<endl;
			cout << "*             *"<<endl;
			cout << "*             *"<<endl;
			cout << "*             *"<<endl;
			cout << "***************"<<endl;
			if(n==17)
		    cout << "****************"<<endl;
			cout << "*              *"<<endl;
			cout << "*              *"<<endl;
			cout << "*              *"<<endl;
			cout << "*              *"<<endl;
			cout << "*              *"<<endl;
			cout << "*              *"<<endl;
			cout << "*              *"<<endl;
			cout << "*              *"<<endl;
			cout << "*              *"<<endl;
			cout << "*              *"<<endl;
			cout << "*              *"<<endl;
			cout << "*              *"<<endl;
			cout << "*              *"<<endl;
			cout << "*              *"<<endl;
			cout << "*              *"<<endl;
			cout << "****************"<<endl;
			if(n==18)
			cout << "*****************"<<endl;
			cout << "*               *"<<endl;
			cout << "*               *"<<endl;
			cout << "*               *"<<endl;
			cout << "*               *"<<endl;
			cout << "*               *"<<endl;
			cout << "*               *"<<endl;
			cout << "*               *"<<endl;
			cout << "*               *"<<endl;
			cout << "*               *"<<endl;
			cout << "*               *"<<endl;
			cout << "*               *"<<endl;
			cout << "*               *"<<endl;
			cout << "*               *"<<endl;
			cout << "*               *"<<endl;
			cout << "*               *"<<endl;
			cout << "*               *"<<endl;
			cout << "*****************"<<endl;
			if(n==19)
			cout << "******************"<<endl;
			cout << "*                *"<<endl;
			cout << "*                *"<<endl;
			cout << "*                *"<<endl;
			cout << "*                *"<<endl;
			cout << "*                *"<<endl;
			cout << "*                *"<<endl;
			cout << "*                *"<<endl;
			cout << "*                *"<<endl;
			cout << "*                *"<<endl;
			cout << "*                *"<<endl;
			cout << "*                *"<<endl;
			cout << "*                *"<<endl;
			cout << "*                *"<<endl;
			cout << "*                *"<<endl;
			cout << "*                *"<<endl;
			cout << "*                *"<<endl;
			cout << "*                *"<<endl;
			cout << "******************"<<endl;
			if(n==20)
			cout << "*******************"<<endl;
			cout << "*                 *"<<endl;
			cout << "*                 *"<<endl;
			cout << "*                 *"<<endl;
			cout << "*                 *"<<endl;
			cout << "*                 *"<<endl;
			cout << "*                 *"<<endl;
			cout << "*                 *"<<endl;
			cout << "*                 *"<<endl;
			cout << "*                 *"<<endl;
			cout << "*                 *"<<endl;
			cout << "*                 *"<<endl;
			cout << "*                 *"<<endl;
			cout << "*                 *"<<endl;
			cout << "*                 *"<<endl;
			cout << "*                 *"<<endl;
			cout << "*                 *"<<endl;
			cout << "*                 *"<<endl;
			cout << "*                 *"<<endl;
			cout << "*******************"<<endl;
			
		
			cout << "Press ENTER to continue..."<<endl;
		    cin.get();
			
}
  


This is an infinite loop unless buff equals "Q" or "q"

1
2
while(true)
	 if (buf == "Q" || buf == "q") break;


you need to learn how to use braces properly for example this if statement on line 43
1
2
3
4
5
6
if (n==4)
	cout <<"****"<<endl;
	cout <<"*  *"<<endl;
	cout <<"*  *"<<endl;
	cout <<"*  *"<<endl;
	cout <<"****"<<endl;


should be
1
2
3
4
5
6
7
8
if (n==4)
{
	cout <<"****"<<endl;
	cout <<"*  *"<<endl;
        cout <<"*  *"<<endl;
        cout <<"*  *"<<endl;
        cout <<"****"<<endl;
}


Last edited on
You can use while loops, you don't need to do that horrid thing with the if statements - you can simulate the behavior of a for loop using a while loop quite easily.
Last edited on
You are missing a lot of braces (at least 20 pairs, the braces for the n=1 and buf="Q" statements are optional as there is only 1 line of code to be executed).

Since you are creating squares (going by dimensions, a space is thinner than a *) every time, there is a much cleaner way of doing it using a couple of while loops and 1 if statement (rather than 20 if statements) - well ok, this has 3, but 1 is the check for quitting and the other is a range check.

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
#include <iostream>
using std::cin;
using std::cout;
using std::endl;

#include <string>
using std::string;
using std::getline;

#include <cstdlib>


int main()
{
    string buf;
    int n;
    while (true) // yes this is an infinite loop, but allows multiple queries.
    {
        cout << "Enter a number between 1-20 to determine the size of the rectangle or press q to quit: " << endl;
        cin >> buf;
        n = atoi(buf.c_str());
        cin.ignore(1000,10);
        if (buf == "Q" || buf == "q")
            break;
        if (n>=1 && n<=20)
        {
            int rows=1;
            {
                while (rows<=n)
                {
                    int columns=1; // set here as we want to reset this value every row 
                    while (columns<=n)
                    {
                        if (columns==1 || columns==n || rows==1 || rows==n) //basically if it is on the perimeter
                        {
                            cout << "*";
                        }
                        else
                        {
                            cout << " ";
                        }
                        columns++; // add 1 to columns
                    }
                    cout << endl; // will only activate after every column in that row has been done.
                    rows++; // adds 1 to rows
                }
            }
        }
    }
    return 0;
}
Last edited on
That code must have taken you a billion years to write.

I just owned you with mine. Plus, it can print a square of any size:

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
#include <iostream>

using namespace std;

void printSquare(int n)
{
    int i = 0;

    while (i < n)
    {
        cout << '*';

        char c = ' ';

        if (i == 0 || i == n - 1)
        {
            c = '*';
        }

        int j = 0;

        while (j++ < n - 2)
        {
            cout << c;
        }

        if (n != 1)
        {
            cout << '*';
        }

        cout << endl;

        i++;
    }
}

int main()
{
    char ans;

    do
    {
        cout << "Enter the size of the square." << endl;
        int n;
        cin >> n;

        printSquare(n);

        cout << "Type q or Q to quit." << endl;
        cin >> ans;
    } while (tolower(ans) != 'q');

    return 0;
}

A little thinking can save you a lot of writing.

Edit: also, how come you compile with VS but edit in Notepad? o.O
Last edited on
Just a minor point about the above post, there should be a check on one of the unconditional stars checking if n!=1, as it is, when you choose 1, it prints out "**".

Also, it would hardly take a billion years, 270 lines isn't much in serious project.
Last edited on
Even VC++2008 Express Edition has good intellisense, who would write code in Notepad with an IDE like that?
@manudude03, +1, noted and fixed.
I don't know if manudude or josue noticed this but he clearly said he can't use for loops he can only use while loops. Though it would be a fairly easy transition between the two.
Also, edit:

line 46: }while(tolower(ans) != 'q'); would be nicer.
I only worry about ideas; technicalities are easily circumvented. I have edited the code above.
@giblit yes, I did see that, hence why I haven't used any for loops in the code I gave.
You can use while loops, you don't need to do that horrid thing with the if statements - you can simulate the behavior of a for loop using a while loop quite easily.


I know. That's what sucks so bad, is that the instructor is "introducing" us to the for loop and char statement next week. Meaning I can't use it for this one. It's just so confusing trying to coordinate string statements and words to match a particular pattern. I just don't get it. I understand complex formulas and calculations using the while and if statements, but how do you coordinate those strings in such a way?
This isn't that complicated... All you have to do is break it down into steps.... Think how you would type the square...

first you have the top row...

then you have 'x' rows that only have end/beginning borders...

Finally you have your last row...

The only variable here is length: how long are these rows? So I hope you can take it from there...


jackbruns28
I understand complex formulas and calculations using the while and if statements...


um... complex? I really don't think this surpasses basic algebra. The math gets a lot more complicated than this later when you start working on other interesting projects...

also, char declars a character. just like string declars a string. the difference between the two is that one is a class, and the other is not.
Topic archived. No new replies allowed.