Skip to main content
gamedev.net
🔒 Locked

[Tutorial] C++ Beginners Tutorial

Started by nooblet Oct 23, 2006 at 6:44 PM 22 replies 6.6k views
Original Post
nooblet
nooblet
Hello, I have just started to make program's for beginners so please tell me if it helps!

/* Username & Password input / Lesson_01
   Author: Cameron Smith
   6:34.40 CT / Oct 23rd, 2006 */

// Our include's
#include <iostream>
#include <string>

// The namespace
using namespace std;

// The main program
int main()
{
	// Create a new string
	string username;
	// Create a new string
	string password;

	// Ask's user for their username
	cout << "Can I please have your username? ";

	// Read's input
	cin >> username;

	// If the username is correct
	if( username == "Cameron" )
	{
		// Ask's user for their password
		cout << "Can I please have your password? ";
		
		// Read's input
		cin >> password;
		
		// If the password is correct
		if( password == "Test0123" )
		{
			// Welcomes the user and creates a new line
			cout << "Welcome " << username << " how are you today?" << endl;
		}
		
		// If the password is wrong
		else if( password != "Test0123" )
		{
			// User receives an incorrect input line.
			cout << "Incorrect input." << endl;
		}
	}
	
	// If the username is wrong
	else if( username != "Cameron" )
	{
		// User receives an incorrect input line.
		cout << "Incorrect input." << endl;
	}

	system("Pause"); // Please stick with std::cin.get()
}



nooblet
nooblet
Yes, I figured that would happen, but it's a lot less coding for me eh? [grin]
Ravuya
Ravuya
If I had to name one thing I see covered poorly a lot, it's probably namespace use. While you're saving typing here, you are representing a poor version of namespace use, and I'd prefer you explicitly specify it wherever possible.

Also, I'd like to point out that your cin won't handle input with spaces in it correctly. You should endeavour to use getline(cin, myString) in this situation.

Also, having else if(name != "Cameron") is probably unnecessary. A compiler might not optimize this out, so you should just say else to save yourself some typing and a lot of cycles.

Finally, System("pause") or cin.get() at the end of your program isn't great, and I know Fruny's talked about that particular thing before (indeed, it gets Oluseyi's goat as well). I don't mind so much, but you should comment it to explain that it's generally a DOS-only thing and the overreaching purpose for it.

P.S. shouldn't your main function take arguments? And return a value?

Just some criticism -- I think it's a good idea to demonstrate basic concepts to users but it's also important to make sure that you have the best possible representation of the basic concepts. I'm sure someone will be by to take a nice rip out of my post as well. [grin]
Twisol
Twisol
You might want to change "system("pause")" to a cin.get(), since the comment next to the system() call is a bit contradicting to the line. Just say "cin.get(); // Is a much better choice than system("pause")" or something.
Zakwayda
Zakwayda
Quote:
Original post by nooblet
Yes, I figured that would happen, but it's a lot less coding for me eh? [grin]
If you're posting code for demonstrative purposes, expect to be nitpicked :-) And saying it's 'less coding for you' certainly won't help - if you're offering up illustrative examples in a beginners' forum, it's your responsibility to do things the Right Way where at all possible.

Anyway, I'm not sure how egregious an offense 'using namespace std' is in a single-file example program, but it wouldn't be too hard to avoid in this case. You're only using string twice, so you might as well scope it with std::. A 'using std::cout' and 'using std::endl' at the beginning of your 'main' function would take care of the rest, and set a better example for any beginning C++ coders who might read your code.

I'm sure there are lots of other things that could be mentioned, but I'll just point out that the conditionals in the 'else ifs' are redundant. Your intended purpose may be added clarity, but it's not particularly idiomatic, so I'd leave them out if I were you.
nooblet
nooblet
Thanks everyone for helping me out, if I am going to be explaining this on my site, I want it done right [grin]
Twisol
Twisol
On Ravuya's thing about cin.get() and system("pause"), it's bad when you run a program that uses it just before it ends from cmd.exe (start->run->type "cmd.exe"), but it's fine enough if you don't (like, you double-click on its icon to start it).
Drilian
Drilian
Quote:
Original post by nooblet
Yes, I figured that would happen, but it's a lot less coding for me eh? [grin]


It offends my delicate coder sensibilities that you use the "using namespace std" bit, and then when told that it's not a good idea you simply say "it's a lot less coding for me."

As has been said already, if you're writing for beginners, as you say you are, then you should probably do things The Right Way (TM), vs. doing it the Way That's Convenient For You.

Writing a tutorial is not an exercise in convenience; it's something that should be done very seriously and very thoroughly. Don't skimp because it's "easier." Do it right. And when someone suggests something that would make it better (i.e. more "right" or more correct), don't just shrug it off.

I also feel strongly that people who are not EXPERTS and who are not attempting to impart the true meaning of the rationale for why things are done the way they are (instead of just showing how they're done) should not be writing example anythings for anyone.

Having a line like:
system("Pause");

As a beginner, I might think any or all of the following:
Why use system("Pause")?
What does the system function even DO?
Does the "Pause" in quotes just display flavor text, or does it actually do something?
Why is there a comment that says stick with std::cin.get()?
What does that "std::" mean? I don't see it anywhere else!
Why is he showing me how to use some nebulous function named "system" when there's a better way that he mentions?
Does this guy really know what he's doing?

So again, only experts should write tutorials. Hell, only experts who are also good at explaining their expertise should write tutorials. While I'll probably get rated down for being as harsh as I've been, it needs to be said. Leave the tutorial/sample writing to the people who really, truly know what they're doing.

Please, don't confuse the beginners. Learning to code in C++ is hard enough as it is.
Fruny
Fruny
What's an include? What's a namespace? Why would I need any of them? cout? What does that mean? And those << and >>, what are they for? Why do you sometimes use // and sometimes not? And those ;, what do they mean? Why did you write int main() ? When you say it creates a new line, where does that new line come from?

Your tutorial doesn't really answer any of those questions. It should (and much more), if it is to be of any use to a beginner. And it should answer them correctly. It's harder than it looks.

And please fix those damn apostrophes: includes, asks...
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
jpetrie
jpetrie
Quote:

I also feel strongly that people who are not EXPERTS and who are not attempting to impart the true meaning of the rationale for why things are done the way they are (instead of just showing how they're done) should not be writing example anythings for anyone.

Quote:

So again, only experts should write tutorials. Hell, only experts who are also good at explaining their expertise should write tutorials. While I'll probably get rated down for being as harsh as I've been, it needs to be said. Leave the tutorial/sample writing to the people who really, truly know what they're doing.

Quoted for truth and emphasis.

What you have presented, even if it is accompanied by some explanitory text, is a great example of a bad tutorial. The internet has enough of those, there is honestly no reason for you to contribute to the flood.

The calibur of the code you've presented does not suggest mastery to me, and a well crafted "tutorial" can only be written by such a person. Are you writing in some attempt to "cement your knowledge?" If so, and if that helps you, great. But there is no reason to make those writing available to the public, because it just contributes noise, and there is already way too much noise.

I respect your desire to "give back" to the community, and I'm sorry if this sounds harsh, but it's the truth.

Oluseyi made a great post about this very issue in a thread a while back, but I can't for the life of me find it now... :
noNchaoTic
noNchaoTic
That would be no use to man, nor beast without a hefty accompanying document to explain everything. Also... wtf is that last line? You say to use the get method, yet use the system call? Strange.
Steven ToveySPUify | Twitter
nooblet
nooblet
Ok, if you didn't read above, I am making this a more advanced tutorial explaining it, I just commented so the user can see what I did, that is the final code. Thanks though for the critisim.
Zakwayda
Zakwayda
Quote:
Original post by nooblet
Ok, if you didn't read above, I am making this a more advanced tutorial explaining it...
Well, be sure to also read the comments regarding the wisdom (or lack thereof) of writing tutorials in the first place without sufficient experience or expertise.

Don't take it hard though. Although your original intent was perhaps to help others, it's actually you yourself who stands to benefit the most from this thread. Read and consider all the suggestions that have been made, and then perhaps re-post the code with improvements. But, instead of framing it as a 'tutorial', just say that you're seeking constructive feedback on your coding technique and style. The folks here will most likely be happy to help out in this case.

And who knows, maybe in 10 or 20 years you actually will be in a position to write a tutorial :-)
Drilian
Drilian
Quote:
Original post by nooblet
Ok, if you didn't read above, I am making this a more advanced tutorial explaining it, I just commented so the user can see what I did, that is the final code. Thanks though for the critisim.


Perhaps you weren't following, so I'll be more blunt.

Bad tutorials are worse than no tutorials. Your code is not what many would call "good code." Therefore, it's a "bad idea" to write a tutorial around said code.

If you're trying to help yourself learn, then by all means, do so. We would all be glad to help you out.

If you're trying to help others learn, don't. There are other people more qualified than you (or even me) to do so. And they do.

Don't write a tutorial. It won't be good. It won't be worthwhile. And it'll do more harm to other beginners than it will help. If you're helping yourself, it's not a tutorial for others anyway. If you are, then you'd be failing at that goal.

Learn for yourself. Let the experienced do the teaching. Don't be the blind leading the blind.
nooblet
nooblet
There are a lot of people that code like me, but you know what! I like the idea of not writing any more tutorials :D But my team I created enjoys the tutorials I DO create on the forums a while back, and I just told one of the head people and he seemed excited, but maybe they just code the same way. I have benefited others but in a different language :), so I thought i'd give C++ a try, but it seems I can't yet :X. I however don't use the iostream library anymore, stickin with SDL and learning OpenGL. I shall take the advice and not write my tutorials on here anymore, but will on my website ;).
Mr_Fhqwhgads
Mr_Fhqwhgads
Quote:
Original post by Andrew Russell
"using namespace std;" is a fairly terrible habit to teach beginners.


If I may ask..why is it bad idea?
Blupix Games
Twisol
Twisol
Mr_..something, it dumps the entire contents of the 'std' namespace into the global namespace. Now, on the off chance you had a function or class or object with the same name as anything in 'std', then you could potentially (especially with classes and objects) have collisions. It's far better to declare your program to be using specific elements of the 'std' namespace, like so:

#include <iostream>using std::cout;using std::cin;using std::endl;int main(){    cout << "Hello, World" << endl;    return 0;}


This way, you declare yourself to be using only the elements you need.

EDIT: That'll probably compile, but I can't check for sure at the moment. It's pretty basic though.
Zahlman
Zahlman
Fruny and jpetrie (and possibly others) both said it at least as well, but:

Quote:
Original post by nooblet
Hello, I have just started to make program's for beginners so please tell me if it helps!


Sorry - based on the discussion in this thread and the fact that I've never heard of you as "an expert", I don't need to read the source to tell you it doesn't. :(

I can also tell you, from the personal experience of many *long* posts in this forum, that you very often can't say everything you need to just in the source comments.

(Also, in English, the apostrophe is not used to form plurals or in verb conjugations. I wouldn't pick on your misuse, but for the consistency thereof.)
Driv3MeFar
Driv3MeFar
Quote:
Original post by jpetrie
Oluseyi made a great post about this very issue in a thread a while back, but I can't for the life of me find it now... :


This one, perhaps (from the thread Zahlman linked to)?
Quote:
Original post by Oluseyi
Tutorials Considered Harmful, 'I agree,' says Promit.

It is hard - very, very hard - to write a high-quality C++ tutorial. The reason why is simple: C++ is not an easy language for a beginner to learn, and a tutorial aimed at beginners is bound to be tortuous as it tries to explain interdependent concepts in linear fashion. Basically, C++ tutorials are losing propositions.

*shrug*

Good luck.


Edit: Fixed links to Washu's journal. I strongly encourage the OP to at least read Promit's post, as it is highly relevant.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.