Bird
Raised Fist0
C++programming~10 mins

String input and output in C++ - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to read a string from the user using std::cin.

C++
#include <iostream>
#include <string>

int main() {
    std::string name;
    std::cout << "Enter your name: ";
    std::cin [1] name;
    std::cout << "Hello, " << name << "!\n";
    return 0;
}
Drag options to blanks, or click blank then click option'
A>>
B<<
C==
D>>=
Attempts:
3 left
💡 Hint
Common Mistakes
Using << instead of >> with std::cin causes a compilation error.
Using == is a comparison operator, not for input.
2fill in blank
medium

Complete the code to read a full line of text (including spaces) into a string variable.

C++
#include <iostream>
#include <string>

int main() {
    std::string sentence;
    std::cout << "Enter a sentence: ";
    std::getline(std::cin, [1]);
    std::cout << "You wrote: " << sentence << "\n";
    return 0;
}
Drag options to blanks, or click blank then click option'
Ainput
Bsentence
Cline
Dtext
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a variable name that is not declared causes a compilation error.
Using std::cin >> variable reads only until the first space.
3fill in blank
hard

Fix the error in the code to correctly output the string variable message.

C++
#include <iostream>
#include <string>

int main() {
    std::string message = "Welcome!";
    std::cout [1] message [2] std::endl;
    return 0;
}
Drag options to blanks, or click blank then click option'
A+
B>>
C<<
D=
Attempts:
3 left
💡 Hint
Common Mistakes
Using >> instead of << causes compilation errors.
Using + does not work with std::cout for output chaining.
4fill in blank
hard

Fill both blanks to create a dictionary (map) comprehension that stores word lengths for words longer than 3 characters.

C++
#include <iostream>
#include <string>
#include <map>
#include <vector>

int main() {
    std::vector<std::string> words = {"apple", "cat", "banana", "dog"};
    std::map<std::string, int> lengths;
    for (const auto& word : words) {
        if (word.[1]() > 3) {
            lengths[word] = word.[2]();
        }
    }
    for (const auto& [w, len] : lengths) {
        std::cout << w << ": " << len << "\n";
    }
    return 0;
}
Drag options to blanks, or click blank then click option'
Asize
Blength
Ccount
Dsize_t
Attempts:
3 left
💡 Hint
Common Mistakes
Using count() is incorrect because it does not exist for strings.
Using size_t is a type, not a method.
5fill in blank
hard

Fill all three blanks to create a map of uppercase words to their lengths for words longer than 4 characters.

C++
#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <algorithm>

int main() {
    std::vector<std::string> words = {"apple", "cat", "banana", "dog", "elephant"};
    std::map<std::string, int> result;
    for (auto word : words) {
        if (word.[1]() > 4) {
            std::transform(word.begin(), word.end(), word.begin(), ::[2]);
            result[[3]] = word.size();
        }
    }
    for (const auto& [w, len] : result) {
        std::cout << w << ": " << len << "\n";
    }
    return 0;
}
Drag options to blanks, or click blank then click option'
Alength
Btoupper
Cword
Dsize
Attempts:
3 left
💡 Hint
Common Mistakes
Using size() instead of length() in the condition is acceptable but here only length() is correct.
Using tolower instead of toupper changes to lowercase.

Practice

(1/5)
1. Which C++ statement correctly reads a full line of text including spaces into a string variable named name?
easy
A. std::cin >> name;
B. std::getline(std::cin, name);
C. std::cin.get(name);
D. getline(name, std::cin);

Solution

  1. Step 1: Understand how to read full lines with spaces

    The function std::getline reads an entire line including spaces into a string.
  2. Step 2: Identify correct syntax for reading from standard input

    The correct syntax is std::getline(std::cin, variable), so std::getline(std::cin, name); is correct.
  3. Final Answer:

    std::getline(std::cin, name); -> Option B
  4. Quick Check:

    Full line input = std::getline(std::cin, name) [OK]
Hint: Use std::getline to read full lines with spaces [OK]
Common Mistakes:
  • Using std::cin >> name; reads only until first space
  • Swapping arguments in getline function
  • Using std::cin.get() incorrectly for strings
2. Which of the following is the correct way to print the string variable message to the console in C++?
easy
A. std::cout << message << std::endl;
B. print(message);
C. echo message;
D. console.log(message);

Solution

  1. Step 1: Identify the standard output method in C++

    C++ uses std::cout to print output to the console.
  2. Step 2: Use the insertion operator to print strings

    The correct syntax is std::cout << message << std::endl; to print the string and move to the next line.
  3. Final Answer:

    std::cout << message << std::endl; -> Option A
  4. Quick Check:

    Print string = std::cout << message << std::endl; [OK]
Hint: Use std::cout << variable << std::endl; to print strings [OK]
Common Mistakes:
  • Using print() which is not a C++ function
  • Using echo or console.log which are from other languages
  • Forgetting std:: or << operator
3. What will be the output of the following C++ code?
std::string text;
std::getline(std::cin, text);
std::cout << "You entered: " << text << std::endl;

If the user inputs: Hello World
medium
A. You entered: Hello
B. You entered:
C. Compilation error
D. You entered: Hello World

Solution

  1. Step 1: Understand std::getline input behavior

    std::getline(std::cin, text); reads the entire line including spaces, so it captures "Hello World".
  2. Step 2: Output the stored string

    The output prints "You entered: " followed by the full input string, so it prints "You entered: Hello World".
  3. Final Answer:

    You entered: Hello World -> Option D
  4. Quick Check:

    getline reads full line = You entered: Hello World [OK]
Hint: std::getline reads full line including spaces [OK]
Common Mistakes:
  • Assuming std::cin >> text reads full line
  • Expecting only first word output
  • Thinking code causes compilation error
4. Identify the error in this C++ code snippet that tries to read a string with spaces:
std::string input;
std::cin >> input;
std::cout << input << std::endl;
medium
A. std::string is not a valid type
B. Missing semicolon after std::string input
C. std::cin >> input; reads only until first space, not full line
D. std::cout cannot print strings directly

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    std::cin >> input; reads only until first space, not full line -> Option C
  4. 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

  1. 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.
  2. Step 2: Print greeting with the name

    std::cout << "Hello, " << name << "!" << std::endl; correctly prints the greeting with the name.
  3. Final Answer:

    std::string name; std::getline(std::cin, name); std::cout << "Hello, " << name << "!" << std::endl; -> Option A
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard