![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: May 2006
Posts: 34
Rep Power: 0
![]() |
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;
} |
|
|
|
|
|
#2 |
|
Professional Programmer
Join Date: Feb 2005
Location: PA, USA
Posts: 254
Rep Power: 4
![]() |
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 |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
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 |