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.
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.
Day 1: Data Types
You are viewing a single comment's thread. Return to all 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:
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.