Post: C++ Tutorial Basic Commands
03-22-2009, 01:08 AM #1
(adsbygoogle = window.adsbygoogle || []).push({}); TOTURIAL MADE, CREATED, AND TYPED BY GLoRY GuNz
Nooby tutorial but even those who know a lot may gain something from reading it

If you find this tutorial helpful please +rep me

TUTORIAL --
Explaining the basic commands of C++


Part 1 - Libraries and Namespaces
All libraries and namespaces will always go at the BEGINNING of a program. Their are many different types of Libraries, a Library is basically a command that will allow to use other types of commands throughout the program, which is why it's called a library.

The only library that you will need to know for now is the #include <iostream>

The only namespace command you will need to know is using namespace std;
Theirs is another simple one for visual basic but you need to upgrade it, and we don't need to know about that one yet

The #include <iostream> will allow you to use all of the basic commands
Some of these commands include

cout
endl
Only remember the one's bolded for right now
cin
system commands such as system("PAUSE") and system("CLEAR")

switch commands
if
else
do
while
and many others

This is how your beginning of your program should look for know
    #include <iostream>
using namespace std;


Part 2 - How to start up int main() (The very first function needed in all programs - You do not need to know this yet) and cout and endl commands

After you have typed in your libraries you will need to program in your int main()

The int main() is used to show the program where you are going to start, it's immediately where the computer looks first

Look at this coding
    #include <iostream>
using namespace std;

int main()
{
}


the int main() is put under your libraries, for basic programs it will always be the second part of your program, or the second thing you code

notice that after int main() you see two brackets--
this {
and this }

The first one is called an opening brace({), it is used to open up the int main() so that you can start programing
The second one is called a closing brace(}), it is used to close up the int main() so that your coding inside of the braces gets processed

So now lets try puting a message in the program so it comes up on the screen using the cout and endl command

Here's what it looks like with a message in it
    #include <iostream>
using namespace std;

int main()
{
cout << "Here's The Message The will show up in the program you create when you click debug" << endl;
}


The cout command as you see, tells the program that it wants a message outputed or shown on the screen
After you type cout you have to put the << keys in to be able to type more coding

So it would look like this cout <<
Now once you have that main cout command in you will be able to type your message
To type in a message for a cout command you will need to put in 2 quotation keys and your message will go in between these keys

Example of a message
    cout << ""

Here your message would be nothing because nothing is between the quotation quotes

So now we want our message to be like hi was sup
We would type
    cout << "Hi was sup"


Now that we have our message we have to end it so that we dont get an error when we debug it

To end it you can do two different things
You can type
    cout << "Hi was sup" << endl;

This will be the more neater of the two, because it will basically end a paragraph or a line, so that the program moves down a line

or
    cout << "Hi was sup";

This will just make the program end the sentence, but any other codes will be added directly on to the end of the sentence, The little end command ; is needed at the end of most codes in int main() (or any other function) and is just a way of ending a line and moving on to more coding

Part 3 - system("PAUSE")

Ok so far your coding should look like this
    #include <iostream>
using namespace std;

int main()
{
cout << "Hi was sup" << endl;
}


now in order for the window to stay open for you to see the message when you click debug, you will need to use a system("PAUSE") command

system("PAUSE") will pretty much freeze all coding before it until any key is pressed, pretty basic huh

So you would type the new code right after your message because that's what you want it to pause, it would look like this
    #include <iostream>
using namespace std;

int main()
{
cout << "Hi was sup" << endl;
system("PAUSE");
}


Now you have the messaged paused!
Now it's time to finish the program so that we get no errors when we hit debug

Part 4 - Return command (Nooby explanation so understanding is better)

Ok now after every other code in your int main() you will need to add a return command

So here's what we have so far
    #include <iostream>
using namespace std;

int main()
{
cout << "Hi was sup" << endl;
system("PAUSE");
}


So now the return command will have to go after system("PAUSE") since thats the last part of coding in int main()

So here's what the would look like
    #include <iostream>
using namespace std;

int main()
{
cout << "Hi was sup" << endl;
system("PAUSE");
return 0;
}


The return 0; is basically a command for ending a fuction, in this case that fuction is int main()
What it really does, and you don't need to know this yet and don't read it if you don't want to be confused is return a value

click debug on your program and wa la now you have learned why your first program works

END OF TUTORIAL

Any questions can be asked
03-22-2009, 01:50 AM #2
Smile Hello world!
03-22-2009, 02:10 AM #3
You can GoOgLe this!

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo