Beginning Programmer: Which of these is the preferred coding method? (C++)

Recommended Posts

MDBurpo    122
I'm enrolled in ITT Tech. I'm in the third week of my first term. It will be quite a while before I get to any real programming class as I need to knock out a bunch of general education first. I'm taking it upon myself to learn C++ in my spare time. I just started today, probably 3 hours ago... I've noticed a lot of similarities to PHP. I'm on my third basic program. The first was a modified Hello World program. Basically a multiline self-introduction. Yay me. The second was similar, but slightly more complex. It's a self-introduction that presents itself line by line as the the user presses enter. Third, I'm currently working on a quiz reviewing the information found in the previous program. You're presented a question, you type your answer, you're right or wrong and continue the quiz. I'm curious on the best way to format my coding. I see two ways to do it cleanly, and I'm not sure which way is correct or preferred.
//Matthew D. Burpo - Beginning C++ - December 18, 2008 - Third Program - Basic Quiz
#include <iostream>
using namespace std;
int main()
{
cout <<"What is the programmer's full name?\n";
cout <<"1.) Matthew Benjamin Durpo\n";
cout <<"2.) David Matthew Burpo\n";
cout <<"3.) Matthew David Burpo\n";
cout <<"4.) Benjamin Matthew Durpo\n";
cout <<"\n";
cin.ignore();
{
cout <<"\n";
cout <<"That's correct!\n";
cout <<"\n";
cout <<"Press 'Enter' to continue.\n";
cin.get();
}
else
{
cout <<"\n";
cout <<"Incorrect answer! The programmer is Matthew David Burpo.\n";
cout <<"\n";
cout <<"Press 'Enter' to continue.\n";
cin.get();
}
cout <<"How old is Matthew?\n";
cout <<"\n";
cin.ignore();
{
cout <<"\n";
cout <<"That's Correct!\n";
cout <<"\n";
cout <<"Press 'Enter' to continue.\n";
cin.get();
}
else
{
cout <<"\n";
cout <<"Incorrect answer! Matthew is 24 years old.\n";
cout <<"\n";
cout <<"Press 'Enter' to continue.\n";
cin.get();
}
cout <<"Matthew wants to be a:\n";
cout <<"1.) Video Game Designer\n";
cout <<"2.) Video Game Programmer\n";
cout <<"3.) Video Game Producer\n";
cout <<"4.) Both 1 & 2\n";
cout <<"5.) Both 1 & 3\n";
cout <<"6.) Both 2 & 3\n";
cout <<"7.) 1, 2, & 3\n";
cout <<"\n";
cin.ignore();
{
cout <<"\n";
cout <<"That's correct!\n";
cout <<"\n";
cout <<"Press 'Enter' to continue.\n";
cin.get();
}
else
{
cout <<"\n";
cout <<"Incorrect answer! Matthew wants to be a video game programmer and designer.\n";
cout <<"\n";
cout <<"Press 'Enter' to continue.\n";
cin.get();
}
cout <<"Press 'Enter' to exit the program...\n";
cin.get();
return 0;
}


OR
//Matthew D. Burpo - Beginning C++ - December 18, 2008 - Third Program - Basic Quiz
#include <iostream>
using namespace std;
int main()
{
cout <<"What is the programmer's full name?\n"
"1.) Matthew Benjamin Durpo\n"
"2.) David Matthew Burpo\n"
"3.) Matthew David Burpo\n"
"4.) Benjamin Matthew Durpo\n"
"\n"
cin.ignore();
{
cout <<"\n"
"That's correct!\n"
"\n"
"Press 'Enter' to continue.\n";
cin.get();
}
else
{
cout <<"\n"
"Incorrect answer! The programmer is Matthew David Burpo.\n"
"\n"
"Press 'Enter' to continue.\n";
cin.get();
}
cout <<"How old is Matthew?\n"
"\n"
cin.ignore();
{
cout <<"\n"
"That's Correct!\n"
"\n"
"Press 'Enter' to continue.\n";
cin.get();
}
else
{
cout <<"\n"
"Incorrect answer! Matthew is 24 years old.\n"
"\n"
"Press 'Enter' to continue.\n";
cin.get();
}
cout <<"Matthew wants to be a:\n";
"1.) Video Game Designer\n"
"2.) Video Game Programmer\n"
"3.) Video Game Producer\n"
"4.) Both 1 & 2\n"
"5.) Both 1 & 3\n"
"6.) Both 2 & 3\n"
"7.) 1, 2, & 3\n"
"\n"
cin.ignore();
{
cout <<"\n"
"That's correct!\n"
"\n"
"Press 'Enter' to continue.\n";
cin.get();
}
else
{
cout <<"\n"
"Incorrect answer! Matthew wants to be a video game programmer and designer.\n"
"\n"
"Press 'Enter' to continue.\n";
cin.get();
}
cout <<"Press 'Enter' to exit the program...\n";
cin.get();
return 0;
}


I appreciate any input. I'm an OCD perfectionist and would like to start off right with good coding habits. Thanks in advance!

Share on other sites
nerd_boy    720
Hoim... I'm certainly not a professional, and haven't worked in the field professionally, but as long as its neat, clean, readable, and consistent, it should be good. And so long as it works for you, too. My personal preference 'tween the two would probably be the former. Probably mostly because I didn't realize strings on seperate lines concatanated(sp) like that to form one in C++. :|

Though, now that I look at the latter one again, I do see something that doesn't jive with me(again, personal preference), and may make it cleaner. Instead of having:
cout << "foobar""biz""woohoo!";

,I would probably go with:
cout << "foobar"        "biz"        "woohoo!";

Just looks kinda cleaner to me. >_> Best of luck!

Share on other sites
gdunbar    2198
Note that each "cout <<" generates a function call when running, whereas if you do the multiple line concatenation, the compiler will put the string together, and make just one function call. And making a function call has some cost associated with it. So, in that sense, the second is "better", and should be preferred. Note that on today's processors this is a relatively miniscule difference, but it's still probably a good habit to have.

I do agree with nerd_boy that you should indent the subsequent lines of the string for clarity; whether you want to simply indent, or make them all line up, is a matter of personal preference.

Geoff

Share on other sites
Telastyn    3777
Wait, you're putting brackets on a new line and are wondering about cout format?!? (I'm kidding, really) Or worse yet, you're asking about code format first rather than structure? Good old ITT.

But umm, the first is fine. I've seen the second with << at the start of each continuation line, and that's okay. I've seen it with + at the end of each line, that's okay. Doing string concatenation via that mechanism is a little icky (imo).