LearnFrenzy
Sign in Take a free test

Input/Output with cin and cout · Predict the Output · Question 14

cin >> char vs cin.get with spaces Answer, explanation & discussion

  • Output
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 14 Back to Exercise

What is the output of the following C++ program when the input is a b?

main.cpp
#include <iostream>

int main() {
    char c1, c2, c3;
    std::cin >> c1;
    std::cin.get(c2);
    std::cin >> c3;
    std::cout << '[' << c1 << "][" << c2 << "][" << c3 << "]\n";
    return 0;
}
  • A [a][ ][b] Correct
  • B [a][b][b]
  • C [a][b][ ]
  • D [a][ ][ ]
Correct Answer: Option A

get reads the space after a; >> then skips whitespace (none left before b) and reads b.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links