Help Making C++ Rectangle

Microsoft Visual studio .net 2003 (full)
October 10, 2009 at 10:04:27
Specs: Windows Vista
I have to build a rectangle that looks like this:

**********
**********
**********

per user input. Here is the code I have. Right now it only shows up similar to this:

***************
*
*
*

#include <iostream>
using namespace std;
void main() {
	int iHeight = 0;
	int iWidth = 0;
	int iHeightCount = 1;
	int iWidthCount = 1;
	cout << "Enter the height of the rectangle: ";
	cin >> iHeight;
	cout << "Enter the width of the rectangle: ";
	cin >> iWidth;
	cout << endl;
	while (iHeightCount <= iHeight)
	{
		while (iWidthCount <= iWidth)
		{
			cout << "*";
			++iWidthCount;
		}
		cout << "*";
		++iHeightCount;
		cout << endl;
	}
	cout << endl;
}

All help is greatly appreciated!


See More: Help Making C++ Rectangle

Report •


#1
October 10, 2009 at 10:39:50
Use for loops. it'll solve your problem, and it's considered a better coding practice (because it avoids this bug).

Report •

#2
October 10, 2009 at 10:52:13
I have to use while loops :( Gotta love teachers haha

Report •

#3
October 10, 2009 at 10:53:47
Fine then. You never reset the value of iWidthCount.

Report •

Related Solutions

#4
October 10, 2009 at 11:41:52
so don't increment the iWidthCount? or what? I am confused?

Report •

#5
October 10, 2009 at 12:12:51
so don't increment the iWidthCount?
Only if you want to end up in an infinite loop.

I am confused[.]
Well, if you don't mind the mangled output, you could see what is happening by changing the test on your second while to this:

while ((cout << iWidthCount) && iWidthCount <= iWidth)
(As you might have guessed, it shows you the value of iWidthCount whenever the inner loop is tested.)

Alternatively, you could just use VS' debugger to go though your code step-by-step, and watch your variables.


Report •

#6
October 11, 2009 at 15:46:01
Could you please post what the code should look like because I am still having problems?

Report •

#7
October 11, 2009 at 15:52:53
Depends on how you answer these questions:
1) What is the value of iWidthCount at the start of the inner while?

2) What is the value of iWidthCount when you break out of the inner while?

3) What is the value of iWidthCount at the start of the inner while the second time through the outer loop?

4) Must you use nested whiles, or did he just demand two while loops?


Report •


Ask Question