Beginner C++ Cout Cin Integer

The objective of this basic C++ tutorial is to provide beginners with a simple understanding of the cout and cin functions of C++, while simultaneously providing a guide to one of their first programs. This C++ tutorial will also show you how to set up your environment and your example programs.

toc_collapse=0;
Contents 

What You Will Need

  • A compiler and IDE (I'm using Bloodshed Dev-C++; this is a free IDE that comes with the MinGW compiler)
  • Some creativity

Install DevC++ by going to their website, downloading their exe package with the mingw compiler. Most default options will be fine. To begin, create a project, right click your project on the left side and add new source files. Press F9 to compile.

To start the program off, we need to include the "preprocessor." A preprocessor will run as soon as the program opens, before any additional data is entered.

#include <iostream>

"iostream" is a header that gives you access to many predefined functions such as cout and cin which is what will be the focus of this tutorial. Now hit enter twice and type "using namespace std;". The statement we just used is to alert the compiler we will be using functions inside the "std" library - "std" is abbreviated for standard. Make sure you include the semicolon (;) or your program will not have the proper syntax. In C++ (and C) programming, you need to include a semicolon to tell the compiler that you have ended the command.

#include <iostream>

using namespace std;

Now for one of the most important parts of the code! Hit enter twice again, and type:

int main()
{

There is no semicolon at the end of int main() because that is where you will be telling the program what to do. If you were to type int main(); your program would not do anything because you've informed the compiler that you are at the end of the command. However, we're at the beginning! The reason for typing int before main is because the function will return an integer.

#include <iostream>

using namespace std;

int main()
{

OK we've reached an exciting part! We're actually going to do something that we can see a result for! Hit enter once (your compiler should automatically indent for you) and type:
"cout<<"Sup world!\n";
cin.get();
}"
Cout is similar to print, it displays text in the program. << are insertion operators that indicate what to output. The quotes make the compiler output the literal string, and /n moves the cursor down to the next line. Notice there is a semicolon at the end of the command. Cin.get(); tells the program to expect the user to hit the enter key. Try the running the program without this line. The program flashes for a very brief moment. The aforementioned function keeps the program open until you hit enter. The } ends the main function.

#include <iostream>

using namespace std;

int main()
{
    cout<<"Sup world!\n";
    cin.get();
 }

Now comment out the code you just wrote in main. You have two options here. Option 1: Use "//" at the start of the line. This will cause the compiler to ignore the line of code. However, you have to do this at the start of each line you want to comment out. If you are trying to comment out a massive section of code this practice will consume a large amount of time. Thus, we have Option: Use /* at the beginning of the area to be commented out, and */ at the very end. Say you want to test a program you've written without a section of code. Just enclose that section of code with /* at the beginning and */ at the end. This option works for a single line, or a vast number of lines.

#include <iostream>

using namespace std;

int main()
{
    /*
    cout<<"Sup world!\n";
    cin.get();
    */

 }

Working with Variables

Variables allow you to store data, different types of variables allow you to store different types of data.

  • int - Int allows you to store integers (whole numbers)
  • float - Float allows you to store numbers with decimals
  • char - Char allows you to store a single character
In this tutorial we will be using int, but I strongly encourage you to test the different types of variables on your own. This will help improve your understanding of variables. Be creative, try different things as you go! You don't have to follow along exactly with me. In the main section type:
"int thisisanumber;

cout<<"Please enter a number: ";
cin>> thisisanumber;
cin.ignore();
cout<<"You entered: "<< thisisanumber <<"\n";
cin.get();"

You will notice a few new things here, the rest is knowledge we're adding onto. With int thisisanumber we are defining a variable and it is set as an integer. We ask the user to enter a number and with cin>> thisisanumber we store the number they enter. We use cin.ignore() to disregard the user hitting the enter key. Next, using cout we display the integer entered with << thisisanumber <<. The insertion operators allow us to enter the variable, but if we don't end it with them, we'll get an error, so make sure to enclose the variable inside of these. And finally, cin.get() keeps the program open until we hit enter.

#include <iostream>

using namespace std;

int main()
{
    /*
    cout << "Sup world!\n";
    cin.get();
    */

    int thisisanumber;

      cout << "Please enter a number: ";
      cin >> thisisanumber;
      cin.ignore();
      cout << "You entered: " << thisisanumber << "\n";
      cin.get();

 }

As previously mentioned I encourage you to experiment with this tutorial and try to work out ideas that you come up with. I try my best to not be a hypocrite and had done this myself. I was aware of the basic math functions the included library provided me (+, -, x, and /) so I wanted to test out multiplying variables together. This is what I typed:

"int thisisanumber, thisnumber2, a;

cout<<"Please enter a number: ";
cin>> thisisanumber;
cin.ignore();
cout<<"Please enter another number: ";
cin>> thisnumber2;
cin.ignore();
cout<<"You entered: "<< thisisanumber <<" & "<< thisnumber2 <<"\n";
a = thisisanumber * thisnumber2;
cout<<"a = "<< a << "";
cin.get();

return 0;"

It is important to notice that I have added extra integers, doing so allows me to do the multiplication without overwriting values. The rest of the code is similar to what has already been covered. Record a variable, then record another - note that the variable name changed. Then we see the numbers the user has entered. The program then multiplies the two variables together and records the answer in variable a. The user does not notice this because there was no cout function. After multiplying the two together and getting the answer, then the program provides the output of the multiplication problem. At the bottom of the code you'll notice I have entered return 0; which informs your pc whether the program succeeded or not.

#include <iostream>

using namespace std;

int main()
{
    /*
    cout<<"Sup world!\n";
    cin.get();
   
    return 1;
    */

   
    int thisisanumber, thisnumber2, a;  
   
    cout << "Please enter a number: ";
    cin >> thisisanumber;
    cin.ignore();
    cout << "Please enter another number: ";
    cin >> thisnumber2;
    cin.ignore();
    cout << "You entered: " << thisisanumber << " & " << thisnumber2 << "\n";
    a = thisisanumber * thisnumber2;
    cout << "a =  " << a << "";
    cin.get();

    return 0;
}

The code posted below is how my code looked after completing the tutorial I followed. I took some imagination to help further my understand of basic functions taught in the tutorial. Take a look:

#include <iostream>

using namespace std;

int main()
{
    /*
    cout<<"Sup world!\n";
    cin.get();
   
    return 1;
    */

   
    int thisisanumber, thisnumber2, a;  
   
    cout << "Please enter a number: ";
    cin >> thisisanumber;
    cin.ignore();
    cout << "Please enter another number: ";
    cin >> thisnumber2;
    cin.ignore();
    cout << "You entered: " << thisisanumber << " & " << thisnumber2 << "\n";
    a = thisisanumber * thisnumber2;
    cout << "a =  " << a << "";
    cin.get();
   
    return 0;
}

Author Bio: Dan Killam is a Graduate Student studying Digital Forensics at the University of Central Florida.

For further discussion please visit our Programming Forums.

Anonymous's picture

i love this tutorial

i love this tutorial

Anonymous's picture

I want the output to be

I want the output to be printed as
"C++ is better than C"
that is the double quotes should be printed along with the line as it is.
I have tried adding 2 double quotes along with the line in cout but it gives error.
Can you tell how will the line be printed along with the doule quotes.
Please write the entire line and show it to me. I am a new C++ Programmer.

Thank you

Anonymous's picture

omg this tutorial is good

omg this tutorial is good

Post new comment

The content of this field is kept private and will not be shown publicly. If you have a Gravatar account associated with the e-mail address you provide, it will be used to display your avatar.