|
|||||||
| Forum Home | Register | Members List | Mark Forums Read |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 | ||||||||
|
Junior Member
Location: Martinsville, IN |
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. Code:
//Matthew D. Burpo - Beginning C++ - December 18, 2008 - Third Program - Basic Quiz
#include <iostream>
using namespace std;
int main()
{
int answer;
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";
cout <<"Answer: ";
cin >> answer;
cin.ignore();
if (answer==3)
{
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";
cout <<"Answer: ";
cin >>answer;
cin.ignore();
if (answer==24)
{
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";
cout <<"Answer: ";
cin >>answer;
cin.ignore();
if (answer==4)
{
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;
}
Code:
//Matthew D. Burpo - Beginning C++ - December 18, 2008 - Third Program - Basic Quiz
#include <iostream>
using namespace std;
int main()
{
int answer;
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"
"Answer: ";
cin >> answer;
cin.ignore();
if (answer==3)
{
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"
"Answer: ";
cin >>answer;
cin.ignore();
if (answer==24)
{
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"
"Answer: ";
cin >>answer;
cin.ignore();
if (answer==4)
{
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;
}
|
||||||||
|
|
|
|
|
#2 | ||||||||
|
Administrator
Location: London, UK |
Wow, keep up the good work. I'm sure you'll be a pro in no time at all and completely pwn your programming classes. Yaustar has provided a great coding example that I'm sure you'll find really useful. Another great resource is the book Code Complete which has a ton of useful tips and tricks.
The main issues I can see here are:
Of course you are welcome to have a difference of opinion with the conventions I try to follow; none the less I hope this helps.
__________________
Michael 'Adrir' Scott :: Games, Virtual Worlds, Education Networking | Research | Teaching Last edited by Adrir : 12-18-2008 at 03:46 PM. |
||||||||
|
|
|
|
|
#3 | ||||||||
|
Junior Member
Location: Martinsville, IN |
Code:
(4 == answer) |
||||||||
|
|
|
|
|
#4 | ||||||||
|
Member
|
I agree mostly w/ Adrir w/ a few exceptions.
First, my thoughts on the two examples. The way you layout your code dosen't REALLY matter. From my experience, you are told to format your code according the standard provided by your job anyways. But I like the first example. Breaking your calls to cout onto multiple lines made me double check to make sure it was valid at first, I've never seen anyone code like that . Second, and this is just a portability thing, flip through the options in your editor and make sure that you are doing tabs as spaces. The tab character can fuck things up for people working in linux, the spacing may not be consistent. Personally, I prefer 4 spaces, but it looks here to be about 8, but again, thats just my preference. You don't waste space w/ un-neded empty lines, which is also a personal preference of mine. You also don't do that crazy: if (asdf) { } bracket notation either which I don't like. And I would have to agree w/ adrir w/ using spacing more. (Mostly w/ the logical operations, like (variable==5) to (variable == 5) But again, its all about preference. For instance i could care less if there is a space after the 'if' or not. Declaring variables when you use them. Hmm, personally I don't care to a certain extent. For instance, i have 0 problems as far as variable declaring is concerned w/ your code. But when you have a 250 line function, I care if you declared the variable used on line 157 at line 4. Just be reasonable is all.To me all of this is personal preference, don't expect a real, or clear answer. Your style will grow and evolve as you get more experienced, or you'll just be straight up required to use a specific style As far as better practices go, If i were to see this in a code review I would recommend the following: 1.) You are using the entire std namespace, yet you only use cout. Don't get into the habbit of just including namespace when you need a specific class. It creates a bigger executable by linking in the entire std namespace! This may not seem like a big deal now, but when you have hundreds of object files to link against, every little bit helps Instead do this: using std::cout; or for string objects: using std::string; Or, some nazzi's don't 'use' anything at all, until they actually use it. They would include iostream and their cout calls would look like this: std::cout << "hi guyz" << std::endl; Thats to much for me, so i opt to just use the object in the namespace i want. IMO this is a very good habbit to get into early, because its a giant bitch to break later. 2.) answer isn't initialized to anything. This is big man, big. It may not seem like it now, but the shit i've seen and days I've spent tracking bugs that originated because of uninitialized variables. Stop that, always initialize, no questions asked. 3.) if (answer==3). 3? Wtf is 3? No magic numbers! :P Seriously though, if you really want to get into good habbits, don't use magic numbers like this. Either define an enum up top, or declare variables w/ meaningful names and check if the answer given is equal to the variable/enum. Again, from experience. When i look through a gigantic legacy function and find out that the variable 'a' I've been tracking for a 100 lines has changed from the magic number 2 to the magic number -1 only to be returned (and the returned value is expected to be -1, 5 or 6) And each one changes the state of the game dramatically I wana kick faces ![]() 4.) Your doing multiple "things" in one function. I would break up each question into a seperate operation (function) so that the code for each break in logic is sectioned off. It 's easier to debug a bug in question 2 when you know that all the logic for question 2 is isolated in function AskQuestionTwo(); That's it for the fundamental changes I would recommend in the style of the code. I would personally put all the questions and answers in some sort of expendable data structure and create a generic AskQuestion() function that takes in the list of questions and correct answer for easier extendability, but thats also over-engineering the problem here (ask two simple questions). If this was to go out i would argue its a must, to make it easily expendable. To go all out would be to make a it data driven w/ a formated file and a generic question asking library. Then simply load the questions and answers, load the data either w/ your library or pass it to it and then call the AskQuestion() function. But I say this as just an example, obviously it over engineers the problem at hand. Remember, over-engineering isn't good either. I'm not sure how much general programming experience you have to really know what i mean by over-engineering, but you'll learn that yourself someday ![]() On a side note, to Adrir. I've never, ever seen someone using a namespace or class within a function. I don't doubt it's possible, but what are the advantages to this? I would have to strictly disagree it's conventional, solely because I've never seen it done ![]() |
||||||||
|
|
|
|
|
#5 | ||||||||
|
Member
|
It's not "correct". Technically, both ways are "correct" but it is prefered by some because it helps detect one specific error.
Lots of people make the mistake of doing '=' for a compare, by accident of course, if you were to type: (answer = 4) By accident, this would compile and you would have a logic bug. (It would always evaluate to true) if you force yourself into the habit of constant before variable when you do make this mistake you get a compile error and bam! bug fixed. Some people swear by it, I've learned to go w/o it and very rarely make the mistake. I don't think I've made this particular mistake for at least over half a year now. |
||||||||
|
|
|
|
|
#6 | ||||||||
|
Junior Member
Location: Martinsville, IN |
nef,
I'm pretty sure I understand the changes you're pointing me to. I'm gonna start a new program and try some things out. ![]() |
||||||||
|
|
|
|
|
#7 | ||||||||
|
Member
|
gg no captchas ?
![]() Last edited by yaustar : 12-19-2008 at 07:06 AM. |
||||||||
|
|
|
|
|
#8 | |||||||||
|
Member
|
Quote:
![]() It's good you care about these things early. Most people I feel end up breaking bad habits or building good ones after they've already blown their feet off.... a couple times :P |
|||||||||
|
|
|
|
|
#9 | ||||||||
|
Moderator
Location: Netherlands |
Nice advices there, nef. I might even have learned something from that! (I use 'magic numbers' exhaustively.) Although I absolutely hate putting accolades on a line of their own...
![]() I have a question about the code MDBurpo posted. As I see it, the code: Code:
cout <<"What is the programmer's full name?\n"; cout <<"1.) Matthew Benjamin Durpo\n"; cout <<"2.) David Matthew Burpo\n"; Code:
cout <<"What is the programmer's full name?\n" "1.) Matthew Benjamin Durpo\n" "2.) David Matthew Burpo\n"
__________________
I'm a web developer by profession, but a game developer by heart. Uh oh! The princess is in another signature! |
||||||||
|
|
|
|
|
#10 | ||||||||||
|
Administrator
Location: London, UK |
Quote:
I have to say I completely missed number 2. I guess I need more practice reviewing code! ![]() Quote:
You can declare the directve within any scope. So why not? The main advantage being that it is easier than explicitly typing the namespace for every function call but can reduce the risk of calling incorrect objects or functions.
__________________
Michael 'Adrir' Scott :: Games, Virtual Worlds, Education Networking | Research | Teaching |
||||||||||
|
|
|
![]() |
«
Previous Thread
|
Next Thread
»
| Thread Tools | |
| Display Modes | |
|
|
Powered by vBulletin® Version 3.6.9
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
All times are GMT -8. The time now is 10:51 AM.




















Just be reasonable is all.







Linear Mode

