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++?
✗ Incorrect
std::getline reads the entire line including spaces until Enter is pressed.
What happens if you use
std::cin >> str; to read a string with spaces?✗ Incorrect
std::cin >> str; stops reading at the first space.
How do you print a string variable
name followed by a new line?✗ Incorrect
std::cout << name << std::endl; prints the string and moves to a new line.
Which header file must you include to use
std::string?✗ Incorrect
std::string is defined in the string header.
What does
std::cin.ignore() do before using std::getline?✗ Incorrect
std::cin.ignore() discards leftover characters so std::getline works correctly.
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.