Inferno Web Development
  • Home
  • Forums
  • About
  • Links
  • Contact

Tutorials List

  • C++ Tutorials

Articles

  • Java
  • PHP
  • Photoshop
  • C#
  • C++ Qt GUI
  • C++ Win32 API
  • C++
  • MASM32
  • General News
  • JavaScript
  • Web Development
  • Windows Tweaks

Popular Tutorials

All time:

  • Website Header with 3D Fold Up / Lift Effect
  • C++ Win32 API Tutorial
  • Perfect C++ String Explode Split
  • Simple C++ DLL Loading a Message Box
  • Simple C++ Pointers and References

Related Articles

  • Singleton C++
  • C++ Volatile Keyword
  • Perfect C++ String Explode Split
  • C/C++ Basic Structures of a Simple Program
  • Simple C++ Pointers and References

Programming Tutorials

Home

Beginner C++ Cout Cin Integer

Submitted by Dan Killam on Fri, 11/07/2008 - 06:46.
  • C++

Table of Contents

  1. What You Will Need
  2. Working with Variables

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.

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;
}

For further discussion please visit our Programming Forums.


5
Average: 5 (2 votes)
»
  • Share Tutorial

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <img> <ul> <ol> <li> <dl> <dt> <dd> <h3> <h4> <h2>
  • Lines and paragraphs break automatically.
  • Image links with 'rel="lightbox"' in the <a> tag will appear in a Lightbox when clicked on.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>. Beside the tag style "<foo>" it is also possible to use "[foo]".
  • You can use BBCode tags in the text. URLs will automatically be converted to links.
  • Table of contents based on the <h*> tags

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
    oooo      o8o             oooooo     oooo  oooooo     oooo              
`888 `"' `888. .8' `888. .8'
888 oooo .oooo. `888. .8' `888. .8' oooo oooo
888 `888 `P )88b `888. .8' `888. .8' `888 `888
888 888 .oP"888 `888.8' `888.8' 888 888
888 888 d8( 888 `888' `888' 888 888
.o. 88P 888 `Y888""8o `8' `8' `V88V"V8P'
`Y888P 888
.o. 88P
`Y888P
Enter the code depicted in ASCII art style.

Navigation

  • Home
  • Forums
  • Image Gallery
  • Links
  • About
  • Contact

Why Register? Contribute articles and tutorials to ID, earn titles, learn from ID programming projects, and advertise your blog in our forums.

  • Forum Community
  • Register Now
  • Write an Article


Copyright © Inferno Development 2008-2009. All Rights Reserved.