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.
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.
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.
Now we will get to the main program and you specify it by
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.
in C++ is known as a comment, anything after the "//" will not be executed.
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.
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.
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
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.
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.
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.
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;
Code:
#include<iostream>
Code:
int main()
{
//Code
}
Now we will get to the main program and you specify it by
Code:
int main()
{
}
Code:
//
Code:
string name;
Code:
cout << "Hello what is your name?\n";
Code:
cin >> name;
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;
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();
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;
Last edited: