Welcome!

Join our community of MMO enthusiasts and game developers! By registering, you'll gain access to discussions on the latest developments in MMO server files and collaborate with like-minded individuals. Join us today and unlock the potential of MMO server development!

Join Today!

C++ Tutorial 1 (Basics, input + output)

Initiate Mage
Joined
Jul 29, 2008
Messages
83
Reaction score
10
You are coming hear to learn some c++ and I am here to offer you some help.

First we will learn about the basics to get a program start and input + output.

Code:
using namespace std;
#include<iostream>

int main()
{
   string name;
   cout << "Hello what is your name?\n";
   cin >> name;
   cout << name;
   cin.get();
}





Code:
using namespace std;
is basically declaring all the standard libraries in your program which includes iostream.The ";" is added to the end to declare that it is the end of the line.



Code:
#include<iostream>
I told you above that the using namespace std; declares the standard library. However once declared you will have to include it in your project so you can use the functions it contains like "cout and cin". You use "#include <name of include>" for any file you want to include which may be like #include <vector> etc.



Code:
int main()
{
  //Code
}

Now we will get to the main program and you specify it by
Code:
int main()
{
}
Any code inside the curly brackets will be executed by your compiler. ALL YOUR PROGRAMS NEED AN int main(), its the starting point for all programs.



Code:
//
in C++ is known as a comment, anything after the "//" will not be executed.



Code:
string name;
This is declaring "name" as a string. Whenever you try to change "name" from the program, it can range from single words to numbers etc.



Code:
cout << "Hello what is your name?\n";
Remember when we included <iostream> ? Will this is a function of iostream. cout << will basically display anything that is in the " ". The << is included after cout to show that it is being pushed out. cout is a abbreviation for console output.



Code:
cin >> name;
Remember when we defined name as a string? Well its coming to use now. cin is also a function of <iostream> which means console input. the purpose of cin is to wait for user input. When you specify cin >> name, it will show up a space in your program to type in, one you type in a string since we declared name as string, it will push it to the name variable.



After the cin, if you ever wanted to see if your input actually got put into your variable name, then you can go
Code:
cout << name;
which will basically output what you have in for your variable name.



Pretend that you changed string name; to int name;, this will cause an error if you try to define letters with it. Only numbers will be applicable then.



Code:
cin.get();
this is pretty much used to create a input with no value. If your program did not include cin.get();, then after your input is put into the variable name, the program will quickly shut down and you wont be able to see the outcome.
Alternatives of cin.get() is also system('PAUSE') which will pause the program as well, but the problem with this is that it only works with windows machines, and as you know c++ can be used to run on Linux machines as well. This is a portability issue.

Code:
cout << name;
As we used the cout function before to display text between " ", we can also use the cout function to display what is in a variable. We dont use the " " becuase if we were to put "name" then it would output the words 'name'. For variables, you just put the variable name then thats it.
 
Last edited:
Experienced Elementalist
Joined
Oct 18, 2008
Messages
206
Reaction score
19
PHP:
#include<iostream>
#include <string>
using namespace std;

int main()
{
   string name;
   cout << "Hello what is your name?\n";
   cin >> name;
   cout << name;
   cin.get();
   return 0;
}
this is how your code should be :) if you wrote int main()
than your program needs to return a value
 
Last edited:
Initiate Mage
Joined
Sep 11, 2006
Messages
11
Reaction score
1
this is how your code should be :) if you wrote int main()
than your program needs to return a value

Most compilers don't require you to have a return statement in int main but you can add a return in if you want to

int main() returns a value, however, in some programs, you can use void main().

void main() is a no-no by C++ standards
 
  • Like
Reactions: emi
Joined
Jun 8, 2007
Messages
1,985
Reaction score
490
system("PAUSE"); is also resource heavy

and only works in windows... Not even really C++.

Besides, you don't even comment your code. Documenting code as you go is the earliest trait anyone should learn, as well as pseudo code.

Nice try though. Please help improve existing tutorials in this section. It's okay to post on older threads in the tutorials section of Coder's Paradise, you know. There are many beginner C++ tutorials which can use improving, please refer to the rules.
 
Initiate Mage
Joined
Nov 28, 2011
Messages
69
Reaction score
7
Umm no , Cin can't hold sentences. Meaning if someone put in their full name the program would fail. A better intercepter would be..

getline (cin , *then yourstringnamehere*);
 

Jax

C# Programmer
Joined
Dec 11, 2009
Messages
881
Reaction score
431
Thanks for this tutorial,
Im trying to learn C++!
 
Initiate Mage
Joined
Apr 29, 2012
Messages
2
Reaction score
0
Umm no , Cin can't hold sentences. Meaning if someone put in their full name the program would fail. A better intercepter would be..

getline (cin , *then yourstringnamehere*);

Thanks! I was actually trying to find the right code for holding whole phrases :) :thumbup:
 
Back
Top