DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Page 1 of 3 123 LastLast
Results 1 to 15 of 31

Thread: function to draw a rectangle

  1. #1
    Join Date
    Mar 2008
    Posts
    62

    function to draw a rectangle

    My spring break is next week and I really am trying to get this program done before it and have nothing to do all week and trying to work ahead.. For that matter I am trying to make my program create a rectangle and also be able to fill it. If confused I will put my assignment in this post. Since I really don;t know how to start it I don't want anyone to think I am just asking for answers but any help would be greatly appreciated.

    First off the instructions are as follows:

    Write a program that asks the user to type in the height and width of a rectangle. It also asks the user to type in a character in which to fill the rectangle. The program prints the top and bottom of the rectangle using a hyphen ("-") and the sides using a pipe ("|"). The interior of the rectangle will be filled with the character the user types. Because of the fill character, the height and width of the rectangle cannot be less than 3, because if it is less than three, there would be no place to fill. If the user types an invalid height or width, the program should tell them it is invalid and ask them to type it in again. The program should keep accepting heights and widths until the user types a negative number for the height.

    You should use the following functions:
    Draw_Rectangle The height, width and character are passed into this function. It calls the functions Draw_Top_Bottom and Draw_Middle. Nothing is returned from this function.

    Draw_Top_Bottom This function accepts an integer as the formal parameter and draws the top (or bottom) of the rectangle. Nothing is returned from this function.

    Draw_Middle This function accepts the height, width and the filler character as the parameters and prints the rows of the rectangle. Nothing is returned from this function.

    Get_Number This function has a prompt (a string) passed into the function and asks the user to type what the prompt asks. The function returns the number that is typed in. It does not check for the negative number (that is done in main)

    Get_Character This function asks the user to type in the filler character and returns it.

    The code I have is

    [code=c++]

    #include <iostream>
    using namespace std;
    char Get_Character();
    int Get_Number();
    void Draw_Rectangle();
    void Draw_Top_Bottom();
    void Draw_Middle();

    int main()
    {

    int height, width;
    char symbol;

    cout << "Programmed by Jim Johnson";
    cout << endl << endl;

    cout << "Type a negative for the height to exit!";
    cout << endl;

    cout << "Enter the height: ";
    height = Get_Number();

    cout << "Enter the width: ";
    width = Get_Number();

    cout << "Enter the character to fill the rectangle with: ";
    symbol = Get_Character();
    cout << endl;


    return 0;
    }

    int Get_Number()
    {
    int x;
    bool inputInvalid = true;
    while (inputInvalid)
    {

    cin >> x;

    if (x < 0)
    {
    exit(1);
    }
    else if (x == 0 || x == 1 || x == 2)
    {
    cout << "Invalid!! Height can not be less than 3." << endl << "Type a negative height to exit! Try Again !!!" << endl << "Enter the height: ";
    }
    else
    {
    inputInvalid = false;
    }
    }
    return x;
    }


    The input I need is:

    Programmed by <your name>

    Type a negative for the height to exit!
    Enter the height: 7
    Enter the width: 5
    Enter the character to fill the rectangle with: +
    - - - - -
    :+++:
    :+++: (By the way i am trying to make this figure even out in spacing)
    :+++:
    :+++:
    :+++:
    _ _ _ _ _

    Enter the height at the first prompt and the width at the second!
    Type a -1 for the first prompt to exit!
    Enter the height: 1
    Invalid: Height cannot be less than 3.
    Type a negative height to exit! Try again!!!
    Enter the height: 0
    Invalid: Height cannot be less than 3.
    Type a negative height to exit! Try again!!!
    Enter the height: 5
    Enter the width: 1
    Invalid: Width cannot be less than 3.
    Try Again!!!
    Enter the width: -4
    Invalid: Width cannot be less than 3.
    Try Again!!!
    Enter the width: 2
    Invalid: Width cannot be less than 3.
    Try Again!!!
    Enter the width: 10
    Enter the character to fill the rectangle with: @

    _ _ _ _ _ _ _ _ _ _
    @@@@@@@:
    @@@@@@@:
    @@@@@@@:
    _ _ _ _ _ _ _ _ _ _

    Enter the height at the first prompt and the width at the second!
    Type a -1 for the first prompt to exit!
    Enter the height: 1
    Invalid: Height cannot be less than 3.
    Type a negative height to exit! Try again!!!
    Enter the height: -4

    [/code]

    Any help I would really appreciate it. I am really confused on how these rectangles are and how to fill them.

  2. #2
    Join Date
    Aug 2005
    Location
    Melbourne...Australia
    Posts
    279
    So where are your Top / Bottom and Fill functions?

    It is really just a case of a couple of simple loops operating between the values the user enters for height and width..

    You might benefit from creating a flowchart to capture the desired flow of the program.

    I could easily give you the code but you won't learn anything that way!
    Last edited by Code_Nerd; 03-05-2008 at 05:11 AM.

  3. #3
    Join Date
    Mar 2008
    Posts
    62
    I am not asking for the code...i am just asking for a way to get started....ive never seen this before

  4. #4
    Join Date
    Aug 2005
    Location
    Melbourne...Australia
    Posts
    279
    Quote Originally Posted by jimJohnson
    I am not asking for the code...i am just asking for a way to get started....ive never seen this before
    I have explained a way you can implement this..

    eg.
    Code:
    Pseudo:
    
    Is it Top of Bottom Row?
        If Yes
          Is it First or last spot on row?
             If Yes - Print '|'
             If No print '-'
       If No
          Is it First or last spot on row?
             If Yes - Print '|'
             If No print 'user character'
    Repeat until end
    
    End

  5. #5
    Join Date
    Mar 2008
    Posts
    62
    we havent done over print yet...this is what i got right now am I at least getting close or just way off:

    #include <iostream>
    using namespace std;
    char Get_Character();
    int Get_Number();
    void Draw_Rectangle(int height, int width);
    void Draw_Top_Bottom();
    void Draw_Middle();

    int main()
    {

    int height, width;
    char symbol;

    cout << "Programmed by Jim Johnson";
    cout << endl << endl;

    cout << "Type a negative for the height to exit!";
    cout << endl;

    cout << "Enter the height: ";
    height = Get_Number();

    cout << "Enter the width: ";
    width = Get_Number();

    cout << "Enter the character to fill the rectangle with: ";
    cin >> symbol;

    Draw_Rectangle(height, width);






    return 0;
    }

    void Draw_Rectangle(int height, int width)
    {
    int a = height;
    int b = width;
    }

    void Draw_Top_Bottom()
    {
    int width, count = 0;
    char top, bottom, _;


    top = _ * width;
    cin >> top;

    }

    int Get_Number()
    {
    int x;
    bool inputInvalid = true;
    while (inputInvalid)
    {

    cin >> x;

    if (x < 0)
    {
    exit(1);
    }
    else if (x == 0 || x == 1 || x == 2)
    {
    cout << "Invalid!! Height can not be less than 3." << endl << "Type a negative height to exit! Try Again !!!" << endl << "Enter the height: ";
    }
    else
    {
    inputInvalid = false;
    }
    }
    return x;
    }

  6. #6
    Join Date
    May 2007
    Posts
    843
    I saw your post in another forum

  7. #7
    Join Date
    Mar 2008
    Posts
    62
    thank u captain obvious im trying to do stuff ive never done before

  8. #8
    Join Date
    Mar 2008
    Posts
    62
    i apologize for that last comment I am getting so frustrated with functions here is what i got right now I am posting in more than one forum because there is nothing in out book about this stuff nor have we gone over it and it is just frustrating...can anyone tell me if I am close

    #include <iostream>
    using namespace std;
    char Get_Character();
    int Get_Number();
    void Draw_Rectangle(int height, int width, char symbol);
    void Draw_Top_Bottom(int height, int width);
    void Draw_Middle();

    int main()
    {

    int height, width;
    char symbol;


    cout << "Programmed by Jim JOhnson";
    cout << endl << endl;

    cout << "Type a negative for the height to exit!";
    cout << endl;

    cout << "Enter the height: ";
    height = Get_Number();

    cout << "Enter the width: ";
    width = Get_Number();

    Draw_Top_Bottom(char );

    Draw_Middle();

    cout << "Enter the character to fill the rectangle with: ";
    symbol = Get_Character();

    return 0;
    }

    void Draw_Top_Bottom()
    {


    }

    void Draw_Middle(char symbol)
    {



    }

    void Draw_Rectangle(int height, int width, char symbol)
    {



    }

    int Get_Number()
    {
    int x;
    bool inputInvalid = true;
    while (inputInvalid)
    {

    cin >> x;

    if (x < 0)
    {
    exit(1);
    }
    else if (x == 0 || x == 1 || x == 2)
    {
    cout << "Invalid!! Height can not be less than 3." << endl << "Type a negative height to exit! Try Again !!!" << endl << "Enter the height: ";
    }
    else
    {
    inputInvalid = false;
    }
    }
    return x;
    }

  9. #9
    Join Date
    Aug 2005
    Location
    Melbourne...Australia
    Posts
    279
    I find it hard to believe you have been given a task without having covered it in class!

    Your Get Number function looks like it will work, why havent you attempted the others? I gave you the pseudocode to complete your task, all you need to do is turn it into c++ code..

  10. #10
    Join Date
    Mar 2008
    Posts
    62
    can someone please tell me if I am getting close?

    #include <iostream>
    using namespace std;
    char Get_Character();
    int Get_Number();
    void Draw_Rectangle(int height, int width, char symbol);
    void Draw_Top_Bottom(int width);
    void Draw_Middle();

    int main()
    {

    int height, width;
    char symbol;


    cout << "Programmed by Bryan Kruep";
    cout << endl << endl;

    cout << "Type a negative for the height to exit!";
    cout << endl;

    cout << "Enter the height: ";
    height = Get_Number();

    cout << "Enter the width: ";
    width = Get_Number();

    cout << "Enter the character to fill the rectangle with: ";
    symbol = Get_Character();

    Draw_Top_Bottom(int width);

    return 0;
    }


    int Get_Number()
    {
    int x;
    bool inputInvalid = true;
    while (inputInvalid)
    {

    cin >> x;

    if (x < 0)
    {
    exit(1);
    }
    else if (x == 0 || x == 1 || x == 2)
    {
    cout << "Invalid!! Height can not be less than 3." << endl << "Type a negative height to exit! Try Again !!!" << endl << "Enter the height: ";
    }
    else
    {
    inputInvalid = false;
    }
    }
    return x;
    }

    void Draw_Top_Bottom()
    {
    int width = 0;

    if (width >= 3)
    {
    Draw_Top_Bottom(width);
    }

    }

  11. #11
    Join Date
    Mar 2008
    Posts
    62
    just updating my work to let u guys know i am working on it...any ideas what my problems are let me know

    #include <iostream>
    using namespace std;
    char Get_Character();
    int Get_Number();
    void Draw_Rectangle(int height, int width, char symbol);
    void Draw_Top_Bottom(int width);
    void Draw_Middle();

    int main()
    {

    int height, width, i;
    char symbol;


    cout << "Programmed by Jim Johnson";
    cout << endl << endl;

    cout << "Type a negative for the height to exit!";
    cout << endl;

    cout << "Enter the height: ";
    height = Get_Number();

    cout << "Enter the width: ";
    width = Get_Number();
    for (i = 0; width.length(); i++);
    {
    cout << width[i] << " ";
    width[i] = '-';
    }

    cout << "Enter the character to fill the rectangle with: ";
    symbol = Get_Character();



    return 0;
    }


    int Get_Number()
    {
    int x;
    bool inputInvalid = true;
    while (inputInvalid)
    {

    cin >> x;

    if (x < 0)
    {
    exit(1);
    }
    else if (x == 0 || x == 1 || x == 2)
    {
    cout << "Invalid!! Height can not be less than 3." << endl << "Type a negative height to exit! Try Again !!!" << endl << "Enter the height: ";
    }
    else
    {
    inputInvalid = false;
    }
    }
    return x;
    }

    void Draw_Top_Bottom()
    {
    int width = 0;

    if (width >= 3)
    {
    Draw_Top_Bottom(width);
    }

    }

  12. #12
    Join Date
    Mar 2008
    Posts
    62
    Making further updates....slowly starting to get it but really confused on this function material

    #include <iostream>
    using namespace std;
    char Get_Character();
    int Get_Number();
    void Draw_Rectangle(int height, int width, char symbol);
    void Draw_Top_Bottom();
    void Draw_Middle();

    int main()
    {

    int height, width;
    char symbol;


    cout << "Programmed by Jim Johnson";
    cout << endl << endl;

    cout << "Type a negative for the height to exit!";
    cout << endl;

    cout << "Enter the height: ";
    height = Get_Number();

    cout << "Enter the width: ";
    width = Get_Number();

    cout << "Enter the character to fill the rectangle with: ";
    symbol = Get_Character();

    Draw_Rectangle(int height, int width, char symbol);



    return 0;
    }


    int Get_Number()
    {
    int x;
    bool inputInvalid = true;
    while (inputInvalid)
    {

    cin >> x;

    if (x < 0)
    {
    exit(1);
    }
    else if (x == 0 || x == 1 || x == 2)
    {
    cout << "Invalid!! Height can not be less than 3." << endl << "Type a negative height to exit! Try Again !!!" << endl << "Enter the height: ";
    }
    else
    {
    inputInvalid = false;
    }
    }
    return x;
    }

    void Draw_Top_Bottom(int width)
    {

    for (int count = 1; count <= width; count++)
    cout << '-';



    }

  13. #13
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    Looks like you are making progress. Make sure to pay attention to the specifications you have been given, such as the signature of the getNumber function (including a string argument) and that the input is supposed to be tested in the main function rather than in getNumber.

  14. #14
    Join Date
    Mar 2008
    Posts
    62
    I got a few errors seeing if anyone has any suggestions....

    1>c:\documents and settings\don & diane kruep\desktop\project 3\project 3.cpp(35) : error C2144: syntax error : 'int' should be preceded by ')'

    1>c:\documents and settings\don & diane kruep\desktop\project 3\project 3.cpp(35) : error C2660: 'Draw_Rectangle' : function does not take 0 arguments

    1>c:\documents and settings\don & diane kruep\desktop\project 3\project 3.cpp(35) : error C2059: syntax error : ')'

    1>c:\documents and settings\don & diane kruep\desktop\project 3\project 3.cpp(89) : error C2470: 'Get_Character' : looks like a function definition, but there is no parameter list; skipping apparent body

    #include <iostream>
    using namespace std;
    char Get_Character();
    int Get_Number();
    void Draw_Rectangle(int height, int width, char symbol);
    void Draw_Top_Bottom();
    void Draw_Middle();

    int main()
    {

    int height, width;
    char symbol;


    cout << "Programmed by Jim Johnson";
    cout << endl << endl;

    cout << "Type a negative for the height to exit!";
    cout << endl;

    cout << "Enter the height: ";
    height = Get_Number();

    cout << "Enter the width: ";
    width = Get_Number();

    cout << "Enter the character to fill the rectangle with: ";
    symbol = Get_Character();

    Draw_Rectangle(int height, int width, char symbol);



    return 0;
    }


    int Get_Number()
    {
    int x;
    bool inputInvalid = true;
    while (inputInvalid)
    {

    cin >> x;

    if (x < 0)
    {
    exit(1);
    }
    else if (x == 0 || x == 1 || x == 2)
    {
    cout << "Invalid!! Height can not be less than 3." << endl << "Type a negative height to exit! Try Again !!!" << endl << "Enter the height: ";
    }
    else
    {
    inputInvalid = false;
    }
    }
    return x;
    }

    void Draw_Rectangle(int width, int height, char symbol)
    {
    using namespace std;

    }


    void Draw_Top_Bottom(int width)
    {
    using namespace std;
    for (int count = 1; count <= width; count++)
    cout << '-';
    }

    void Draw_Middle(char symbol)
    {
    using namespace std;

    }

    char Get_Character
    {
    using namespace std;

    }

  15. #15
    Join Date
    Mar 2008
    Posts
    62
    making final updates for the night...if anyone has other suggestions please do not hesitate and give me additional pointers...preferably as non technical dealing w\ functions as possible

    #include <iostream>
    using namespace std;
    char Get_Character();
    int Get_Number();
    void Draw_Rectangle(int height, int width, char symbol);
    void Draw_Top_Bottom();
    void Draw_Middle();

    int main()
    {

    int height, width;
    char symbol;


    cout << "Programmed by Jim Johnson";
    cout << endl << endl;

    cout << "Type a negative for the height to exit!";
    cout << endl;

    cout << "Enter the height: ";
    height = Get_Number();

    if (height < 0)
    {
    exit(1);
    }

    else

    cout << "Enter the width: ";
    width = Get_Number();

    cout << "Enter the character to fill the rectangle with: ";
    symbol = Get_Character();

    Draw_Rectangle(height, width, symbol);

    return 0;
    }


    int Get_Number()
    {
    int x;
    bool inputInvalid = true;
    while (inputInvalid)
    {

    cin >> x;


    if (x == 0 || x == 1 || x == 2)
    {
    cout << "Invalid!! Height can not be less than 3." << endl << "Type a negative height to exit! Try Again !!!" << endl << "Enter the height: ";
    }
    else
    {
    inputInvalid = false;
    }
    }
    return x;
    }

    void Draw_Rectangle(int width, int height, char symbol)
    {


    }


    void Draw_Top_Bottom(int width)
    {

    for (int count = 1; count <= width; count++)
    cout << '-';
    }

    void Draw_Middle(char symbol)
    {


    }

    char Get_Character()
    {


    for(int col = 0; col < width; ++ col)
    {

    cout << symbol;
    }
    }

Similar Threads

  1. Replies: 8
    Last Post: 03-08-2003, 07:25 PM
  2. Replies: 2
    Last Post: 06-05-2001, 01:55 PM
  3. calling ping thru vb
    By Michael Shutt in forum VB Classic
    Replies: 6
    Last Post: 04-05-2001, 02:25 AM
  4. How do I detect an FTP timeout?
    By Julian Milano in forum VB Classic
    Replies: 0
    Last Post: 08-10-2000, 09:16 PM
  5. Trying to print a PDF File from VB
    By Kunal Sharma in forum VB Classic
    Replies: 2
    Last Post: 04-25-2000, 03:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center