Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 14th, 2006, 5:42 PM   #1
funkey_monkey
Programmer
 
Join Date: May 2006
Posts: 34
Rep Power: 0 funkey_monkey is on a distinguished road
Framing Program

Hi,

I've wrote a program to ask a user to input their name and then frame a greeting to them. For example, if their name was Jimmy, the output would look like:


*****************
* *
* Hello, Jimmy! *
* *
*****************

Here is my program. Please provide me with feedback as to my style, clarity, efficiency and methodology.

Thanks,

FM.

#include <iostream>
#include <string>

int main()
{
    // Read name
    std::cout << "What is your name? :" ;
    std::string name;
    std::cin >> name;

    // Crete String to output
    name = "Hello, " + name + "!";
    std::cout << name << std::endl;

    // Determine length of name
    int i = name.size();
    std::string Line( ( i + 4 ), '*' );

    // Loop around 5 times to create the 5 lines of the output
    for( int r = 0; r != 5; ++r )
    {

        // Start a new line
        std::cout << std::endl;

        // Build first and last lines
        if ( r == 0 || r == 4 )
        {
            for( int s = 0; s != (i + 4); ++s )
            {
                std::cout << "*";
            }
        }

        // Build second and fourth lines
        else if( r == 1 || r == 3 )
        {
            for( int t = 0; t != (i + 4); ++t )
            {
                if ( t == 0 || t == (i + 3) )
                {
                    // Print * at both end of line
                    std::cout << "*";
                }
                else
                {
                    // Fill with spaces
                    std::cout << " ";
                }
            }
        }
        else
        {
            // Print out greeting line
            std::cout << "* " + name + " *";
        }
    }

    return 0;
}
funkey_monkey is offline   Reply With Quote
Old Nov 14th, 2006, 7:12 PM   #2
melbolt
Professional Programmer
 
melbolt's Avatar
 
Join Date: Feb 2005
Location: PA, USA
Posts: 254
Rep Power: 4 melbolt is on a distinguished road
looks pretty good to me

if you're looking for advice, the only thing i can really think to say is to put that i+4 into a variable for readability purposes. just makes the code a tad easier to follow.

if you really want to get anal about it you could put a way to handle strings that exceed a certain length, as to avoid word wrapping issues.
__________________
I have never let my schooling interfere with my education. -Mark Twain-

Xbox live gamertag: melbolt
melbolt is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Language display in program Prm753 C++ 3 May 30th, 2006 6:45 PM
Help me program this with Watcom! DBZ Other Programming Languages 3 Feb 26th, 2006 10:41 PM
Creating a program to test a program sixstringartist C 8 Jan 21st, 2006 2:15 PM
Program gets aborted on large input shinni C++ 3 Mar 12th, 2005 9:26 AM
airport Log program using 3D linked List : problem reading from file gemini_shooter C++ 0 Mar 2nd, 2005 5:12 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:06 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC