Computing.Net > Forums > Programming > Help Making C++ Rectangle

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Help Making C++ Rectangle

Reply to Message Icon

Name: kool_zero
Date: October 10, 2009 at 10:04:27 Pacific
OS: Windows Vista
Product: Microsoft Visual studio .net 2003 (full)
Subcategory: C/C++
Tags: C++
Comment:

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!



Sponsored Link
Ads by Google

Response Number 1
Name: Razor2.3
Date: October 10, 2009 at 10:39:50 Pacific
Reply:

Use for loops. it'll solve your problem, and it's considered a better coding practice (because it avoids this bug).


0

Response Number 2
Name: kool_zero
Date: October 10, 2009 at 10:52:13 Pacific
Reply:

I have to use while loops :( Gotta love teachers haha


0

Response Number 3
Name: Razor2.3
Date: October 10, 2009 at 10:53:47 Pacific
Reply:

Fine then. You never reset the value of iWidthCount.


0

Response Number 4
Name: kool_zero
Date: October 10, 2009 at 11:41:52 Pacific
Reply:

so don't increment the iWidthCount? or what? I am confused?


0

Response Number 5
Name: Razor2.3
Date: October 10, 2009 at 12:12:51 Pacific
Reply:

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.


0

Related Posts

See More



Response Number 6
Name: kool_zero
Date: October 11, 2009 at 15:46:01 Pacific
Reply:

Could you please post what the code should look like because I am still having problems?


0

Response Number 7
Name: Razor2.3
Date: October 11, 2009 at 15:52:53 Pacific
Reply:

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?


0

Sponsored Link
Ads by Google
Reply to Message Icon





Use following form to reply to current message:

Login or Register to Reply
LoginRegister


Sponsored links

Ads by Google


Results for: Help Making C++ Rectangle

Help with C++ char's www.computing.net/answers/programming/help-with-c-chars/9909.html

help in c language www.computing.net/answers/programming/help-in-c-language/14135.html

Need help with C programming www.computing.net/answers/programming/need-help-with-c-programming/6970.html