We use string input and output to read words or sentences from the user and show text on the screen.
When you want the user to type their name.
When you need to get a sentence or phrase from the user.
When you want to display messages or results as text.
When you want to ask questions and get answers in words.
When you want to show instructions or information on the screen.
Syntax
C++
#include <iostream>
#include <string>
int main() {
std::string text;
std::getline(std::cin, text); // to read a full line including spaces
std::cout << text << std::endl; // to print the stringreturn0;
}
Use std::getline(std::cin, variable) to read a whole line including spaces.
Use std::cin >> variable to read a single word (stops at space).
Examples
This reads one word (no spaces) and greets the user.
C. std::cin >> input; reads only until first space, not full line
D. std::cout cannot print strings directly
Solution
Step 1: Understand how std::cin >> works with strings
std::cin >> input; reads input only until the first whitespace, so it cannot read full lines with spaces.
Step 2: Identify the correct method to read full lines
To read full lines including spaces, std::getline(std::cin, input); should be used instead.
Final Answer:
std::cin >> input; reads only until first space, not full line -> Option C
Quick Check:
std::cin >> string reads until space [OK]
Hint: Use std::getline to read strings with spaces, not std::cin >> [OK]
Common Mistakes:
Thinking std::cin >> reads full line
Confusing syntax errors with input behavior
Believing std::cout cannot print strings
5. You want to read a user's full name (which may contain spaces) and then print a greeting like Hello, [name]!. Which code snippet correctly does this in C++?
hard
A. std::string name;
std::getline(std::cin, name);
std::cout << "Hello, " << name << "!" << std::endl;
B. std::string name;
std::cin >> name;
std::cout << "Hello, " << name << "!" << std::endl;
C. char name[50];
std::cin >> name;
std::cout << "Hello, " << name << "!" << std::endl;
D. std::string name;
getline(name, std::cin);
std::cout << "Hello, " << name << "!" << std::endl;
Solution
Step 1: Choose method to read full name with spaces
Using std::getline(std::cin, name); reads the entire line including spaces, perfect for full names.
Step 2: Print greeting with the name
std::cout << "Hello, " << name << "!" << std::endl; correctly prints the greeting with the name.
Final Answer:
std::string name;
std::getline(std::cin, name);
std::cout << "Hello, " << name << "!" << std::endl; -> Option A
Quick Check:
Read full line + print greeting = std::string name;
std::getline(std::cin, name);
std::cout << "Hello, " << name << "!" << std::endl; [OK]
Hint: Use std::getline for full name input, then std::cout to print [OK]
Common Mistakes:
Using std::cin >> which stops at first space
Swapping arguments in getline function
Using char arrays without proper input handling
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.