LearnFrenzy
Sign in Take a free test

Input/Output with cin and cout · Find the Error · Question 3

Error: getline into an int variable Answer, explanation & discussion

  • Find the error
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 3 Back to Exercise

The program below does not compile. Which statement correctly describes the problem?

main.cpp
#include <iostream>
#include <string>

int main() {
    int pincode;
    std::getline(std::cin, pincode);
    std::cout << pincode << '\n';
    return 0;
}
  • A pincode must be long
  • B std::getline reads into a std::string only; read the line into a string and convert it with std::stoi Correct
  • C getline needs a delimiter
  • D cin must be cin.getline
Correct Answer: Option B

Either use std::cin >> pincode, or std::string line; std::getline(std::cin, line); int pincode = std::stoi(line);.

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