1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Creating a box using C++

Discussion in 'Software Development' started by jacy, Feb 9, 2006.

Thread Status:
Not open for further replies.
Advertisement
  1. jacy

    jacy Thread Starter

    Joined:
    Jul 19, 2004
    Messages:
    120
    Hi,
    I am trying to create a box using C++. I am only able to create half of the box. Here is my code.

    #include<iostream.h>
    int main ()
    {
    int i;
    for(i=0;i<10;i++)
    cout<<'*'<<"\n"; //prints a vertical line
    for(i=0;i<10;i++)
    cout<<'*'<<" "; //prinsts a horizontal line
    return 0;
    }
    Here is how the output looks like.
    *
    *
    *
    *
    *
    *
    *
    *
    * * * * * * * * *

    Now i dont know how to print a vertical line upwards and the other horizontal line , in order to make a complete box. Can anyone please suggest me something, thanks.
     
  2. aewarnick

    aewarnick

    Joined:
    Sep 3, 2002
    Messages:
    828
    Do one row at a time in the loop and do the first and last rows separate from the loop.

    **** //first
    * * //loop
    * * //loop
    **** //last

    Coding this is extremely simple. You have the idea. You just need to tweak your thinking; do one ROW at a time.
     
  3. jacy

    jacy Thread Starter

    Joined:
    Jul 19, 2004
    Messages:
    120
    Thanks aewarnick, This is how my program looks like now.

    #include <iostream>
    #include <iomanip>
    using namespace std;
    int main ()
    {
    int i;
    for (i=0; i<5; i++)
    cout<<'*'<<" "; //prints the first row
    i=1;
    while (i<5)
    {
    cout<<"\n"<<'*'<<setw(12)<<'*'; //prints the second row and so on
    i++;
    }
    cout<<"\n";
    for (i=0;i<5; i++)
    cout<<'*'<<" "; // prints the last row
    cin.get();
    return 0;
    }

    Thanks a lot am getting the desired output.
     
  4. jacy

    jacy Thread Starter

    Joined:
    Jul 19, 2004
    Messages:
    120
    Hi can u also provide me with some intermediate C++ coding exercises, thanks
     
  5. aewarnick

    aewarnick

    Joined:
    Sep 3, 2002
    Messages:
    828
    The best thing to do is to think of something you want to do, start coding it and ask questions when you need help.
     
  6. Sponsor

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Loading...
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/441340

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice