[C++] Tut by m3m for beginners

m3m

Newbie Spellweaver
Joined
Mar 9, 2007
Messages
40
Reaction score
0
Location
127.0.0.1
Ok, we all know I'm not the best at C++, but I do know a little bit.
I am going to break down the "Hello World" application and explain what each part does.

Here is the way I would code the "Hello World" App.

Code:
#include <iostream>
 
using namespace std;
 
int main()
{
 
cout<<"Hello World!!\n";
cin.get();
 
}

Ok, the #include part is called the header, you NEED that in every program.

The int main() part tells the computer (or whatever) that this is the main part of the program.

Cout is basically a text thing. Do cout<<" [text] \n";
The \n makes it move to the next line and it is optional to include.

You HAVE to have the ; at the end of every command statement.

cin.get(); - It basically makes a pause. If you don't include that, the program will run, then close within a second. cin.get() waits untill you press "enter" to move on in the code.

After int main() there is a "{".
And at the end there is a "}".

These are called brackets, they tell the computer that there are commands inside of that. After main() to the end is one big command. (It's not that big though, lol.)

There is also commands inside of commands.

Like this:

Code:
#include <iostream>
 
using namespace std;
 
int main()
{
 
int number;
 
cout<<"What number am I thinking of?\n";
cin>> number;
cin.get();
if ( number == 6 ) {
     cout<<"Good, you guessed it right\n";
     }
else ( number != 6 ) {
     cout<<"Wrong!\n";
     }
cin.get();
}

Ok, theres a bit more explaining to do now...

int number; - It identifys that a variable that is a number (hense the name "number") is going to be said and you would like to store the number that is said.

cin>> number; - That is what stores the number information.

cin.get(); - Waits for them to press enter, remember? I put it there so that they have time to put in their number and press enter to submit it.

if ( number == 6 ) { - means that if the number equals 6, everything after the bracket "{" will happen untill the end bracket "}".

else ( number != 6 ) { - means that if any other number that does not equal 6 is put in, the stuff after the first bracket "{" happens untill the end bracket "}".
Not sure if you need the ( number != 6 ) part, but I put it in to be safe.

Again cin.get(); - Very helpful, makes a pause for the person to read the cout from after they answer. Then when they press enter, the program will close.

Notice the end bracket "}" to the program.

Notes:
  • cin.get(); waits untill the person presses enter to move on.
  • ";" is needed at the end of every command line.
  • Make SURE you have a header on every app you make.
  • "==" means equals to, "!=" means does not equal.
  • When using int [variable]; you can change the int to a char. int stands for integer, meaning any WHOLE number with no decimals. char stands for character, meaning words or letters with no spaces.
I hope you found this useful and educational
m3m - [C++] Tut by m3m for beginners - RaGEZONE Forums

This is the stuff I started with.
 
Re: C++ Tut by m3m for beginners

There are many errors and the way you explained things doesn't really explain much. Plus it seems to me you forgot to do what you were going to: explain each part. For yourself to revise, be sure you understand these (please look them up):
* #include (it's not a header at all)
* char (vs. char*)
* perhaps the term function
I'm not sure what the major opinion is, but I find both cout and cin nothing but troublesome. printf() and scanf() might be more logical, and will definitely be met far more often.

Try much appreciated, anyway. :smilie3:
 
Re: C++ Tut by m3m for beginners

There are many errors and the way you explained things doesn't really explain much.
QFT.

protip: learn the language nearly completely before you try to teach it, or at least the parts that you're going to teach... it's quite obvious, really.

I'm not sure what the major opinion is, but I find both cout and cin nothing but troublesome. printf() and scanf() might be more logical, and will definitely be met far more often.
Me too, because every time I see << or >> I think "left shift" and "right shift". The OOP way also adds quite a huge amount of overhead code - compare outputting the same string using cout and printf(). Sometimes OOP is better, sometimes it's worse - depending on what it's being used for.

Comparative "Hello World" in C:
Code:
#include <stdio.h>
main() {
 printf("Hello world!\n");
}
#include is a preprocessor directive that includes the contents of "stdio.h", which defines functions such as printf() (used below) among others.
main() defines a function named 'main' - this is the entrypoint of the program, the first function to be executed. Code within a function is considered a block, and blocks are delimited by { and }. Blocks themselves contain zero or more statements, each terminated by a semicolon.
The printf() line invokes a function named 'printf', which is used for formatted output. It gets passed an argument of "Hello world!\n", which is a string (actually, the address of that string in the memory) to be printed. The \n is an escape sequence which means newline.

else ( number != 6 )
Error: expected statement or { after 'else'.
 
Re: C++ Tut by m3m for beginners

Why do you criticize this m3m if he's trying to help, lol. =)
The same reason anyone ever helps or criticizes another person: because it makes us feel superior. Besides that, it's always good to point out mistakes for them to learn from. :winky:
 
Re: sage

No. You need to learn more as well.
Perhaps you should add that the lack of a return value is because your main function isn't setup to return anything? :wink:

A construction which, if I recall correctly, only works in C and not in C++ as with that you -must- specifiy a return value, even for your main.

A good tip my old coding doc tought me: read assignments from the right to the left. Example:

Code:
static int * value;
value is a reference to an integer which is static.

This especially helped me cope with pointers and suchlike :icon6:
 
do we save this as .exe? or a *Special* program for this

.php?? come on say dudes

Oviosly Username1 knows He/She knows EVERYTHING when i go around the forum looking at her piss taking posts its so funny!

{This is a dumb question but ive never done C++}

Username1 is so badly my fucking idol!!! :D
 
Code written in C / C++ or pretty much any other reall language requires you to link and compile it before it can be made into an executable. This, in turn, requires a compiler. There are a lot of compilers out there, a good small free windows IDE with integrated compiler is dev-cpp, that'll let you compile and run this code with the press of a button.
 
i done this and then compiled it..

the program with the little .exe icon opens and closes streight away

[Idea] Look for coders - to Frag Frog..

The title ment to be Coders have a look or what ever

and [Idea] dose say most of it..

Its an idea for coders if they want to build there own game.. and who ever was that fucking prick said ill take all cred sorry dude but i aint like that...it was an idea to others cause i think it will be a big hit..

{anyone kno why Max Studios gives me a processor error im useing my laptop atm cause im going to have my com fixed to make my Mu server ded :P}
 
[Idea] Look for coders - to Frag Frog..

The title ment to be Coders have a look or what ever

and [Idea] dose say most of it..

Its an idea for coders if they want to build there own game.. and who ever was that fucking prick said ill take all cred sorry dude but i aint like that...it was an idea to others cause i think it will be a big hit..
Not nice hijacking other people's threads, but still - basically what you wanted was a team to make a game (for you or with you - no difference), and not too much an original one. We get posts like that every week and they should go to team recruitment section.
 
I would also add that the cin.get(); part really depends on what compiler/studio/etc you're using. I'm on NetBeans 5.5 right now and it isn't needed for basic programs, the pause is included.

Code:
#include <iostream>

main(int argc, char** argv) {
    
    std::cout << "Hello World!\n";
    
    return 0;
}

Outputs:

Code:
Hello World!
[Press Enter to close window]

I'm still learning, so bash this post if necessary.
 
I would also add that the cin.get(); part really depends on what compiler/studio/etc you're using. I'm on NetBeans 5.5 right now and it isn't needed for basic programs, the pause is included.

Code:
#include <iostream>

main(int argc, char** argv) {
    
    std::cout << "Hello World!\n";
    
    return 0;
}

Outputs:

Code:
Hello World!
[Press Enter to close window]

I'm still learning, so bash this post if necessary.
I use NETBEANS IDE for Java. I like the widgets system implimented. However, never used it for C/C++. Always coded in Dev-C++. Maybe an odd few times in VS2005.
 
Back