Challenge - 5 Problems
String Input and Output Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of string input with getline and cin
What is the output of this C++ code when the input is:
John Doe
C++
#include <iostream> #include <string> int main() { std::string firstName, lastName; std::cin >> firstName; std::getline(std::cin, lastName); std::cout << "First: " << firstName << ", Last:" << lastName << std::endl; return 0; }
Attempts:
2 left
💡 Hint
Remember that std::cin >> reads until space, getline reads until newline including leading spaces.
✗ Incorrect
The std::cin >> firstName reads 'John' stopping at space. getline reads the rest of the line including the leading space before 'Doe'. So lastName contains ' Doe' with a leading space.
❓ Predict Output
intermediate2:00remaining
Output of string concatenation and output formatting
What is the output of this C++ code?
C++
#include <iostream> #include <string> int main() { std::string s1 = "Hello"; std::string s2 = "World"; std::cout << s1 + ", " + s2 + "!" << std::endl; return 0; }
Attempts:
2 left
💡 Hint
Look carefully at the commas and spaces in the concatenation.
✗ Incorrect
The code concatenates s1, then a comma and space, then s2, then an exclamation mark. So the output is 'Hello, World!'.
❓ Predict Output
advanced2:00remaining
Output of reading multiple words with getline and cin
What is the output of this code if the input is:
Mary Jane Watson
C++
#include <iostream> #include <string> int main() { std::string first, last; std::getline(std::cin, first, ' '); std::getline(std::cin, last); std::cout << "First: " << first << ", Last: " << last << std::endl; return 0; }
Attempts:
2 left
💡 Hint
getline with a delimiter reads until that delimiter, not the whole line.
✗ Incorrect
The first getline reads until the first space, so first = 'Mary'. The second getline reads the rest of the line, so last = 'Jane Watson'.
❓ Predict Output
advanced2:00remaining
Output of mixing cin and getline with leftover newline
What is the output of this code if the input is:
42 Hello World
C++
#include <iostream> #include <string> int main() { int number; std::string text; std::cin >> number; std::getline(std::cin, text); std::cout << "Number: " << number << ", Text: '" << text << "'" << std::endl; return 0; }
Attempts:
2 left
💡 Hint
After reading number with >>, the newline remains in the input buffer.
✗ Incorrect
std::cin >> number reads 42 but leaves the newline. getline reads until newline, so it reads an empty string. So text is empty.
🧠 Conceptual
expert2:00remaining
Why does mixing cin >> and getline cause issues?
Why does mixing std::cin >> and std::getline often cause unexpected empty string inputs?
Attempts:
2 left
💡 Hint
Think about what happens to the newline character after std::cin >> reads input.
✗ Incorrect
std::cin >> reads input but stops before newline, leaving it in the buffer. std::getline reads until newline, so it reads the leftover newline as an empty string.