drawing a rectangle

drawing a user specified rectangle

Page 1 of 1

2 Replies - 2408 Views - Last Post: 11 October 2007 - 01:11 AM Rate Topic: -----

#1 sugarflaps  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 11-October 07

drawing a rectangle

Post icon  Posted 11 October 2007 - 12:39 AM

//Lyndsey Scott
//Tutorial 3
//Simple c++ program while and for loops
//09th October 2007

#include <iostream> // using the main library
using namespace std;

int main ()
{ // indicates begining of the program

int yc, xr ; char char1 ;

cout << " please enter the character beng used for drawing the rectangle " << endl ;
cin >> char1 ;

cout << " please enter the number of columns you would like " ;
cin >> yc ;

cout << " please enter the number of rows you would like " ;
cin >> xr ;

for ( int y = 0 ; y < yc ; y ++ )
{
cout << char1 = yc ;
}
for ( int x = 0 ; x < xr ; x ++ )
{
cout << char1 = xr ;
}
for ( int y = 0 ; y < yc ; y ++ )
{
cout << char1 = yc ;
}
return 0 ;
}


This is what i have but it still doesnt work i want to build a rectangle of user specifications which are inputed any help would be awesome. it should look like this ####
####
#### or whtever the user inputs. Thankyou

Is This A Good Question/Topic? 0
  • +

Replies To: drawing a rectangle

#2 jjhaag  Icon User is offline

  • me editor am smartastic
  • member icon

Reputation: 45
  • View blog
  • Posts: 1,789
  • Joined: 18-September 07

Re: drawing a rectangle

Posted 11 October 2007 - 01:03 AM

in your cout << statements, you shouldn't have

char1 = yc

since you're trying to output the character, you just want char1.

and to produce a rectangle with xr rows and yc columns, you should probably use a nested pair of for-loops:
for (int x = 0; x < xr; x ++) {
	for (int y = 0; y < yc; y ++) {
		cout << char1;
	}
	cout << endl;
}


the code will produce xr rows of characters with yc columns of characters in each. your previous code will result in a single row, with yc+xr+yc characters. this is because you run through the first loop yc times, each time outputting the character, then run through the second loop xr times outputting a character, and then repeat the first loop again. in the code above, the statement cout << endl prints a newline, with starts a new row on a new line.

hope that helps,

-jjh
Was This Post Helpful? 0
  • +
  • -

#3 sugarflaps  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 11-October 07

Re: drawing a rectangle

Posted 11 October 2007 - 01:11 AM

Thankyou that fixed my program xx
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1