for ..loop newb question

This is a discussion on for ..loop newb question within the C++ Programming forums, part of the General Programming Boards category; Hi, i just pretty much started taking c++ and our teacher is having us do for...loops. i got the basic ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    1

    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

  2. #2
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    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.

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    6
    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

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,074
    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.
    Sent from my iPadŽ

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newb Question Character Counting
    By Wilder in forum C Programming
    Replies: 13
    Last Post: 06-22-2008, 11:37 PM
  2. I need help to compile this code...
    By wise_ron in forum C Programming
    Replies: 17
    Last Post: 05-07-2006, 12:22 PM
  3. newb question: probs with this program
    By ajguerrero in forum C Programming
    Replies: 5
    Last Post: 04-19-2006, 08:04 AM
  4. newb question
    By Jan79 in forum C++ Programming
    Replies: 5
    Last Post: 06-18-2003, 09:59 AM
  5. Win app, newb question
    By Blender in forum Windows Programming
    Replies: 9
    Last Post: 02-04-2003, 11:17 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21