//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
drawing a rectangledrawing a user specified rectangle
Page 1 of 1
2 Replies - 2067 Views - Last Post: 11 October 2007 - 01:11 AM
Replies To: drawing a rectangle
#2
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:
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
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
#3
Re: drawing a rectangle
Posted 11 October 2007 - 01:11 AM
Thankyou that fixed my program xx
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote




|