0

I need to create a rectangle using for loop and a nested for loop
So far the code I've got is:

#include<iostream>
using namespace std;

int main()
{
	int row, i, width, spaces;

	cout <<"Enter the number of rows: ";
	cin >> row;
	cout <<"Enter the rectangle width: ";
	cin >> width;

	i=0;
	for(i=0; i<row; i++)
		cout << "O";
	cout << endl;	
	for(i=0; i<width; i++)
		cout << "O"<< endl;

	system("pause");
	return 0;
}
3
Contributors
2
Replies
3
Views
5 Years
Discussion Span
Last Post by firstPerson
0

As the problem instructions say, you need to have the second loop nested inside the first loop. in this case, all you really need do is remove the two cout lines between the loops, and move the endl off of the the inner loop's output, and you should be good to go:

for(i=0; i<row; i++)
    {
        for(i=0; i<width; i++)
        {
            cout << "O";
        }
        cout << endl;
    }

You may need to modify this slightly if you need to make a box rather than a filled rectangle.

Note that it is almost always best to explicitly brace any blocks, even when there is only one following statement inside the block. The only real exception to this that I can think of is when using the if/else if/else idiom.

Edited by Schol-R-LEA: n/a

0

As the problem instructions say, you need to have the second loop nested inside the first loop. in this case, all you really need do is remove the two cout lines between the loops, and move the endl off of the the inner loop's output, and you should be good to go:

for(i=0; i<row; i++)
    {
        for(i=0; i<width; i++)
        {
            cout << "O";
        }
        cout << endl;
    }

You may need to modify this slightly if you need to make a box rather than a filled rectangle.

Note that it is almost always best to explicitly brace any blocks, even when there is only one following statement inside the block. The only real exception to this that I can think of is when using the if/else if/else idiom.

Next time try to guide him please.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.