-
for ..loop newb question
Hi, i just pretty much started taking c++ and our teacher is having us do for...loops. i got the basic concept of it but theres one assignment i do not understand and its driving me crazy and i have the biggest headache.
The assignment was to make a program that asks the users for the length and heightand then it makes a hollow rectangle.. i got the top and bottom horizontal lines and the left vetical line but i can't get the right vetical line
Code:
#include <iostream>
#include <stdlib.h>
using namespace std;
int main()
{
int length;
int height;
cout << "Enter length: ";//user enters the length of the line
cin >> length;
cout << "Enter Height: ";//user enters the height of the line
cin >> height;
cout << endl;
cout << endl;
for (int line = 0; length-1 > line; line += 1){//user enters a number and
//for loop displays a line
cout << "*";// displays the length of the line
}
for (int line = 0; height-2 > length; height +=1){//Could not figure it out
//to make the vertical line on the right side of the rectangle
cout << "*" <<endl;
}
for (int line = 0; height > line; line += 1){ //for loop for height
cout << "*"<< endl;// displays the height .. the endl makes it go to
//the next line
}
for (int line = 0; length > line; line += 1){//user enters a number and for
//loop displays a line
cout << "*";// displays the length of the line
}
cout << endl;
cout << endl;
cout << endl;
system("pause");
}
i don't want anyone to tell me how to do it but give me a hint or some pointers.
thanks
-mike
-
You are going to have to place the right line while you are placing the left. You will need to put the appropriate number of spaced between the *'s so that it ends up being a box. You are on the right track so far, GL.
-
That was a fun one :)
http://www.agilman.org/crap/code/line.zip
basically there are two cases when you want to print *...
when x or y =0 (first row and column) or when x=width or y=height (the last row and column)
It might be hard to figure out my code at first, but its pretty neat, I had fun solving this one ;)
-
Consider what you int variable "line" will equal when you're at the ends of the box. Use that to conditionally decide if you should put a star or a space.