0
0
C++programming~20 mins

String input and output in C++ - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
String Input and Output Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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;
}
AFirst: John Doe, Last:
BFirst: John, Last: Doe
CFirst: John, Last:
DFirst: John, Last: Doe
Attempts:
2 left
💡 Hint
Remember that std::cin >> reads until space, getline reads until newline including leading spaces.
Predict Output
intermediate
2: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;
}
AHello,World!
BHelloWorld!
CHello World!
DHello, World!
Attempts:
2 left
💡 Hint
Look carefully at the commas and spaces in the concatenation.
Predict Output
advanced
2: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;
}
AFirst: Mary, Last: Jane Watson
BFirst: Mary Jane, Last: Watson
CFirst: Mary, Last: Jane
DFirst: Mary Jane Watson, Last:
Attempts:
2 left
💡 Hint
getline with a delimiter reads until that delimiter, not the whole line.
Predict Output
advanced
2: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;
}
ANumber: 42, Text: ''
BNumber: 42, Text: ' Hello World'
CNumber: 42, Text: 'Hello World'
DNumber: 42, Text: '\nHello World'
Attempts:
2 left
💡 Hint
After reading number with >>, the newline remains in the input buffer.
🧠 Conceptual
expert
2:00remaining
Why does mixing cin >> and getline cause issues?
Why does mixing std::cin >> and std::getline often cause unexpected empty string inputs?
ABecause std::cin >> converts input to uppercase, confusing getline.
BBecause std::getline cannot read strings after std::cin >> is used once.
CBecause std::cin >> leaves the newline character in the input buffer, which std::getline reads as an empty line.
DBecause std::getline automatically skips whitespace, causing it to miss input.
Attempts:
2 left
💡 Hint
Think about what happens to the newline character after std::cin >> reads input.