Loading...

C++ Input

This page lets the program read a word from the keyboard. The variables page left pine.cpp with two names, first and second, and both names were filled by the program itself. The screen was pine, then cone. That two-line result is the step this page builds on. You keep the first name and the word pine. The second name changes its source: it no longer holds cone from the file, it holds whatever you type. Getting Started still keeps pine.cpp and the lowercase word pine, so the cast and the file stay the same.
The compiler is already on the machine from the install page, and the build command does not change. The comment // the word stays pine stays above the declarations, where the compiler still skips it. Only the source of the second word changes on this page.

std::cin is the standard input stream, which reads from the keyboard. The >> operator moves the next word from the keyboard into a name. The line std::cin >> second; waits until you type something and press Enter. Then it stores the first word you typed in second. The name second must exist before the read, so its declaration sits above the read line. std::cin lives in the same header as std::cout, so the existing #include line already covers it, and no new include is needed for the read.
std::cin is a library object, and you did not write it. This page does not add a class of your own, and main stays a free function with its braces. The read is one statement, and it ends with a semicolon like the writing lines before it. The read also does not need a second cin line for the next word. One read takes one word, and a second word needs its own read, which this page does not add. Reading from the keyboard is the one new action on this page. Everything else in the file is already known from the variables page. The two names still use the same std::string type, and the two writing lines still end with std::endl, so the screen keeps the two-line shape you already know.

The declaration std::string second; has no value after the name. That means the box exists and starts empty. The read fills the box. Nothing needs to go between the two lines. Put the first writing line above the read, so the screen shows pine before the program stops and waits. The writing line that prints second goes after the read, because it needs the word you typed. The file keeps the name on disk as pine.cpp, and it still has one declaration for each name.
Type the file in this order, or change only the lines that differ if the variables file is already on disk. Keep #include . Keep the first declaration with pine. Replace the cone declaration with the empty declaration for second. Put the lines in the order shown below. The checked file is below.

pine.cpp reads one word from the keyboard

cpp
#include <iostream>
#include <string>

int main()
{
    // the word stays pine
    std::string first = "pine";
    std::string second;
    std::cout << first << std::endl;
    std::cin >> second;
    std::cout << second << std::endl;
    return 0;
}

Rebuild from the folder that holds pine.cpp. The command is g++ -std=c++17 pine.cpp -o pine and then ./pine. If g++ is missing and clang++ is the compiler you installed, swap only the tool name: clang++ -std=c++17 pine.cpp -o pine. Run ./pine. The screen first shows pine on the first line. Then the program stops at the read and waits. No prompt appears, so the cursor simply sits there. Type cone and press Enter. The second line shows cone, so the screen reads pine, then cone, the same two lines as the variables page.
Notice that the program does not print anything for the typing itself. The letters you type appear on screen only because your terminal echoes them, and the program does not write them. The second line comes from the writing line after the read, and that line prints the box, not the keys you pressed. If you type a word with a typo, such as cnoe, the screen shows cnoe on the second line, because the box holds exactly what the read stored. The program has no idea that cone was meant.

Watch the read work. Run the program again and type cone extra, then press Enter. The screen shows pine, then cone only. The word extra is not printed, because >> stops at the space and this program does not read the rest. The leftover text stays in the input buffer until the program ends, and the program throws it away at the end. Run it once more and press Enter with nothing typed first. The read skips the empty line and keeps waiting until you type a word. Type cone to finish the run.
The table lists what each part of the run shows. Read it once, then check your run against it. The read takes the first word only. Anything after a space stays in the input buffer, and this program never reads it.

📊 What each part of the run shows

Piece What you see
First pine, printed before the program waits
Typed cone, the word you enter
Extra not printed, because the read stops at the space
Screen pine then cone

Now remove the read line and rebuild. The declaration of second still sits in the file, and second is still empty. The program never waits, so it finishes at once. The second writing line prints an empty line, and the screen shows pine, then a blank line. Put the read back. The result returns to pine, then the word you typed. Each change you make to the file gives a result you can explain from its lines. Read the file from the top, then follow the program in order: the box for first is filled, the writing line prints it, the box for second is created empty, the read waits and fills it, and the last writing line prints it. The order of those steps is the order of the lines, and the compiler runs them in that order too.
The next page is types. Its job is to say what kind of value each box holds, such as text, whole numbers, and decimal numbers, and to show that the type decides what the box can keep. This page stops before that choice. You leave with the read in the file, the screen showing pine and the word you typed, and no type choice made yet. The std::string on these lines stays as it is. Count the lines on the screen before you leave. There are two, and the second line is whatever you typed.

🧠 Test Your Knowledge

Ready to Start

Test Your Knowledge

Challenge yourself with this interactive quiz and see how well you understand the topic

❓
4
Questions
🎯
70%
To Pass
♾️
∞
Time
🔄
∞
Attempts

📝 Instructions

  • Read each question carefully
  • Select the best answer for each question
  • You can retake the quiz as many times as you want
  • Your progress will be shown at the top