rectangle

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
// Includes
#include "stdafx.h"
#include <iostream>
#include <conio.h>
using namespace std;

// Main Function
int main()
{
	int a;
	int b;
	int length;
	int width;

	cout << "This program will display a rectangle using the x character.\n\n";

	cout << "Enter 1st number between 2 and 10: ";
	cin >> a;
	cout << "Enter 2nd number between 2 and 10: ";
	cin >> b;

	while ( a < 2 || a > 10 || b < 2 || b > 10)
	{
		cout << "Your numbers must be between 2 and 10: ";
		cin >> a;
		cin >> b;
	}
	if ( a > b )
	{
		length = a;
		width = b;
	}
	else if ( a < b )
	{
		length = b;
		width = a;
	}
	else
	{
		width = length = a;
	}
	cout << "\n\nThe length of this rectangle is " << length << " and the width of this rectangle is " << width << "\n\n" << endl;

	for (a = 1; a <= width; ++a)
	{
		for ( b = 1; b <= length; ++b)
		{
			cout << "X" << endl;
		}
	}
	_getch();

	return 0;
}


sorry for all these questions, this should be my last one tonight, my program i suppose to take a and b and make them length and width, well i got that, but then its suppose to display a rectangle using the character X.

Example: if the user entered 2 and 5 the length would be 5 and the width would be 2, then it would display this rectangle

XXXXX
XXXXX

my problem is i cant get it to display a rectangle, its only display something like this

X
X
X
X
X
X
X
X
X
X

Any ideas?
Last edited on
Substitute

1
2
3
4
5
6
7
	for (a = 1; a <= width; ++a)
	{
		for ( b = 1; b <= length; ++b)
		{
			cout << "X" << endl;
		}
	}


for

1
2
3
4
5
6
7
8
	for (a = 1; a <= width; ++a)
	{
		for ( b = 1; b <= length; ++b)
		{
			cout << "X";
		}
		cout << endl;
	}
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
// Includes
#include "stdafx.h"
#include <iostream>
#include <conio.h>
using namespace std;

// Main Function
int main()
{
	int a;
	int b;
	int length;
	int width;

	cout << "This program will display a rectangle using the x character.\n\n";

	cout << "Enter 1st number between 2 and 10: ";
	cin >> a;
	cout << "Enter 2nd number between 2 and 10: ";
	cin >> b;

	while ( a < 2 || a > 10 || b < 2 || b > 10)
	{
		cout << "Your numbers must be between 2 and 10: ";
		cin >> a;
		cin >> b;
	}
	if ( a > b )
	{
		length = a;
		width = b;
	}
	else if ( a < b )
	{
		length = b;
		width = a;
	}
	else
	{
		width = length = a;
	}
	cout << "\n\nThe length of this rectangle is " << length << " and the width of this rectangle is " << width << "\n\n" << endl;

	for (a = 1; a <= length; a++)
	{
		for ( b = 1; b <= width; b++)
		{
			cout << "X" << endl;
		}
		cout << endl;
	}
	_getch();

	return 0;
}


Heres my new code, apparently i missed something now it displays:

i enter 2 and 5 and it showed:

X
X

X
X

X
X

X
X

X
X

any ideas on what i missed or got wrong.
never mind found it.

my cout << "X" << endl;

should be changed to

cout << "X";


thank you.
Last edited on
Mark the both your threads as resolved.:)
Topic archived. No new replies allowed.