We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • Hackerrank Home
  • Prepare
    NEW
  • Certify
  • Compete
  • Career Fair
  • Hiring developers?
  1. Prepare
  2. Tutorials
  3. 30 Days of Code
  4. Day 1: Data Types
  5. Discussions

Day 1: Data Types

Problem
Submissions
Leaderboard
Discussions
Editorial
Tutorial

    You are viewing a single comment's thread. Return to all comments →

  • AdamsLT
    6 years ago+ 124 comments

    People starting to learn C++ might suffer if they aren't familiar with cin >> and getline() and what happens if you use both of them together. Using cin leaves an end of line character which is then read by your getline(); It is best not to mix different types of input.

    The solution one could use to overcome this problem is to use a different input method. On the other hand, it is always nice to get familiar with problems one might come across while coding, so take a look. It is possible to overcome this problem while keeping all of your code and just adding a single line.

    Here is an excerpt from the input code:

    cin >> doubleNumber;
    cin.ignore();  //ignores an end of line character 
    getline(cin, stringName);
    

    This will read you string correctly, it will also read the whole line. It will just ignore a character and continue from that.

    I could be wrong, I'm still a complete novice myself but this is the way I understand it. Cheers.

    607|
    Permalink
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy
  • Request a Feature