1.3a — A first look at cout, cin, and endl


As noted in previous sections, the std::cout object (in the iostream library) can be used to output text to the console. As a reminder, here’s our Hello world program:

To print more than one thing on the same line, the output operator (<<) can be used multiple times. For example:

This program prints:

x is equal to: 4

What would you expect this program to print?

You might be surprised at the result:

Hi!My name is Alex.


If we want to print things to more than one line, we can do that by using std::endl. When used with std::cout, std::endl inserts a newline character (causing the cursor to go to the start of the next line).

For example:

This prints:

My name is Alex.


std::cin is the opposite of std::cout -- whereas std::cout prints data to the console using the output operator (<<), std::cin reads input from the user at the console using the input operator (>>). Now that you have a basic understanding of variables, we can use std::cin to get input from the user and store it in a variable.

Try compiling this program and running it for yourself. When you run the program, it will print “Enter a number: ” and then wait for you to enter one. Once you enter a number (and press enter), it will print “You entered ” followed by the number you just entered.

For example (I entered 4):

Enter a number: 4
You entered 4

This is an easy way to get input from the user, and we will use it in many of our examples going forward.

If your screen closes immediately after entering a number, please see section 0.7 -- a few common cpp problems for a solution.

(As an interesting side note, if you enter a really big number, the results may surprise you. Try it! This happens because x can only hold numbers up to a certain size. After that, it “overflows”. We’ll discuss overflow in a future section.)

std::cin, std::cout, <<, and >>

New programmers often mix up std::cin, std::cout, << and >>. Here’s an easy way to remember:

  • std::cin and cout always go on the left-hand side of the statement.
  • std::cout is used to output a value (cout = character output)
  • std::cin is used to get an input value (cin = character input)
  • << is used with std::cout, and shows the direction that data is moving from the r-value to the console. std::cout << 4 moves the value of 4 to the console
  • >> is used with std::cin, and shows the direction that data is moving from the console into the variable. std::cin >> x moves the value from the console into x

(Admin note: Discussion of the std::namespace and using statements has been moved to lesson 1.8a -- Naming conflicts and the std namespace)

1.4 -- A first look at functions and return values
1.3 -- A first look at variables, initialization, and assignment

163 comments to 1.3a — A first look at cout, cin, and endl

  • Southern

    Hello everyone, can you explain for me what is "#include <stdafx.h>??

    • nascardriver

      Hi Southern!

      See Euro Micelli and Mark Amery's answer over at stackoverflow ( ).
      Basically, it's meant to speed up compilation. If it's causing you any trouble disable precompiled headers in your project settings. I generally don't recommend using them, because they prevent cross-compilation.

  • Reyansh

    thank you man!!
    for such a page,before checking out your page i was quitting computer science branch but after this i kindaa felt i am loving coding and this made me tell people about this page and help people like me to learn c++ easily
    a great admirer of your page

  • ehsan rezai

    hi, i actually started to work with programs and i had 0 background knowledge like i had no idea how programs even work, but i saw your website and this article and it sounds amazing,so i downloaded the visual studio and started working, well now i think i found the love of my life! now i dont waste my time on playing games and im just working with visual studio! thanks for writing this and providing it for free,well im kind of new but i will try harder, just wanted to say thank you ,and if there is kind of donation or sth i can do to return the favor , i would be glad to do so. <3

  • Micah Cary

    Hello. to whoever maintains this, I really want to thank you for putting your time into it and making it free to use.

  • Nguyen

    Hi Alex,

    In the output of std::cin

    For example (I entered 4):

    Enter a number: 4
    You entered 4

    I am wondering why there are two lines instead of one (
    Enter a number: 4You entered 4)?

    Thanks, Have a great day.

    • nascardriver

      Hi Nguyen!

      When the user presses return a linefeed will be inserted.

      • Nguyen

        Thank you....then what if I'd like to see everything printed on the same line (Enter a number: 4.  You entered: 4)?

        • nascardriver

          As far as I know there is no standard way, because the line feed is inserted by your console/terminal, not by your program. You'd need to use terminal/OS-specific solutions.

          • Alex

            > You'd need to use terminal/OS-specific solutions.

            Or better, a cross-platform library that abstracts these into a common interface, such as the curses I/O library (e.g.

  • Arntzen

    Hey, I have written the "input your number code" exactly like you have in the example and I click on the "Local Windows Debugger" button with the green play button/arrow next to it in Visual Studios. The program runs as it should, but when I put in a number and hit enter, the console window closes and in the output window in Visual Studios it shows those "bla bla... Cannot find or open the PDB file.

    Did the program run as it should or what? I hope I am running the program from the right thing (the one you find under "Debug > Start Debugging" or F5) or am I totally lost?

    Thanks in advance 😀

    • Alex

      Yes, that is the expected behavior. The debugger probably ran, but it likely didn't stop because you didn't set any breakpoints.

      All of those missing PDB files are probably windows system files that don't have debugging symbols available. You can ignore those notifications.

  • Ammaar

    Hi Alex,
    Thanks for these tutorials. Using what I learned I made my own calculator. It can add, subtract, multiply, and divide. Thanks a lot Alex.

  • brunda

    why we have to use "stdafx.h"

  • PatrickC808

    Hi Alex,

    Very helpful tutorials and very good at easing me into the world of c++. i have been having an issue that i hope you can solve, i have the 'hello world' program written up, it says the build succeeded, but the console command box never shows up saying hello. I've tried a lot of fixes, its not that the box appears and quickly closes after running the program, it never runs the program. I decided to continue reading and gave the std::cin tutorial a try. Unfortunately, i never get the chance to enter a number for the program to receive, resulting in Visual Studio telling me it succeeded, when nothing has happened. I'm a noob so i apologize if this is an easy fix and beneath you. please help ?

    • Alex

      It may be obvious, but are you actually running your program after compiling it?

      If so, then try temporarily disabling your virus scanner or malware detector, and see if that fixes it. Sometimes these interfere with things they shouldn't.

  • Cosmin

    Hello! I have a question: what is the default type cout prints? For example:

    I wonder if the type of the argument cout takes affects the value printed. For example, in the first case, I printed an int, but the value was too high and therefore, it overflowed. In the second case, it is the same value, but LL converted it to a long long, and cout printed correctly.

    • nascardriver

      Hi Cosmin!

      This has nothing to do with std::cout. The compiler will decide what type the number is before it gets passed to std::cout. std::cout only prints whatever is passed to it.
      Your compiler decided to make 1 an int and the 9th a long long. You explicitly stated that the other 1 is a long long so there's nothing for the compiler to do about it.

      The bitshift operator doesn't affect the type.

      Here's an example of what gcc 5.4.1 did


      As you see, here the 9th are a long, not a long long.

  • sonia

    hey!this tutorial is really of great help to me as it covered all the basics in the easiest way.

  • shubham

    hye alex this code
    //#include "stdafx.h" // Uncomment this line if using Visual Studio
    #include <iostream>

    int main()
        std::cout << "Enter a number: "; // ask user for a number
        int x; // no need to initialize x since we're going to overwrite that value on the very next line
        std::cin >> x; // read number from console and store it in x
        std::cout << "You entered " << x << std::endl;
        return 0;
    is showing submission limit reached in codechef ide.

  • Daniel

    What is the advantage to using std::cout instead of placing 'using namespace std;' after the include statements and just using 'cout' 'cin' 'endl' etc...?

    • dolx

      I've heard that using the standard namespace (using namespace std) is bad practice because it can cause syntax related errors. I guess you could google that though.

      • Daniel

        Thanks for the reply. I looked it up and found this answer on stackoverflow:

    • Alex

      There's less chance for naming collisions. I talk a lot more about using statements (and why to avoid them) in chapter 4.

  • Anonymous

    Hey Alex, novice programmer here, why wouldn't you just utilize \n within the string to gain begin a new line? As opposed to std::endl?

    Also, great tutorial.

    • MikeL

      On different operating systems, a new line might consist of something other than '\n'. By using the library definition for the OS you are building on, you are guaranteed that it will be correct. In addition, using appropriately named variables makes programs more readable. 😉

    • nascardriver

      std::endl flushes the output buffer (The text will be displayed immediately) whereas '\n' does not and the text might take a moment until it's printed to the console.
      '\n' is useful when you want to print multiple lines at the same time, you'll have '\n' in every but the last line and only the last line will use std::endl, because std::endl is slower than '\n'.

  • Abdo

    Hello Alex. I've been playing around with what I know from this tutorial so far, and surprisingly there's never ending ways to practice what I know despite that I'm still this early in the tutorial. One thing i noticed about std::cin is that it ignores everything after a space. so if in the console the user inputs:

    How was your day? :awesome and cool

    And then i print the variable in which I stored the input in, i get only:


    and it ignores the rest of the input. Do you know if it's possible to get around this somehow while still using std::cin. And if not then is there something else that can replace std::cin that doesn't ignore spaces?

Leave a Comment

Put all code inside code tags: [code]your code here[/code]