0
0
C++programming~5 mins

String input and output in C++ - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the simplest way to read a whole line of text from the user in C++?
Use std::getline(std::cin, variableName); to read a full line including spaces until the user presses Enter.
Click to reveal answer
beginner
How do you print a string variable named name to the console in C++?
Use std::cout << name << std::endl; to display the string followed by a new line.
Click to reveal answer
intermediate
What is the difference between std::cin >> str; and std::getline(std::cin, str);?
std::cin >> str; reads input only until the first space, while std::getline(std::cin, str); reads the entire line including spaces.
Click to reveal answer
intermediate
How can you clear the input buffer before using std::getline after std::cin >>?
Use std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); to discard leftover characters including the newline.
Click to reveal answer
beginner
Why is it important to include #include <string> when working with strings in C++?
Because the <code>std::string</code> class is defined in the <code>string</code> header, so including it allows you to use string variables and functions.
Click to reveal answer
Which function reads a full line of text including spaces in C++?
Astd::getline
Bstd::cin >>
Cstd::cout
Dstd::printf
What happens if you use std::cin >> str; to read a string with spaces?
AIt reads the whole line including spaces
BIt causes a compile error
CIt clears the input buffer
DIt reads only up to the first space
How do you print a string variable name followed by a new line?
Astd::getline(std::cin, name);
Bstd::cin >> name;
Cstd::cout << name << std::endl;
Dprintf(name);
Which header file must you include to use std::string?
A#include &lt;string&gt;
B#include &lt;iostream&gt;
C#include &lt;vector&gt;
D#include &lt;cmath&gt;
What does std::cin.ignore() do before using std::getline?
APrints output
BClears leftover input including newline
CReads a string
DEnds the program
Explain how to read a full line of text from the user and then print it in C++.
Think about reading input with spaces and printing it out.
You got /5 concepts.
    Describe the difference between using std::cin >> and std::getline for string input.
    Consider how spaces affect input reading.
    You got /4 concepts.