0

It requires to explain how the std::string and std::getline handle the whitespace characters separately. And I run the code that solution provides but didn't get the result expected.

Here's the code:

#include "stdafx.h"

#include <iostream>
#include <string>
using namespace std;

int main()
{
    string word, line;
    cout << "choose:1 string,2 getline" << endl;
    char ch;
    cin >> ch;
    if (ch == '1') {
        cout << "Please input:Welcome to C++family!" << endl;
        cin >> word;
        cout << "Effective string:" << word << endl;
        return 0;
    }
    cin.clear();
    cin.sync();
    if (ch == '2') {
        cout << "Please input:Welcome to C++family!" << endl;
        getline(cin, line);
        cout << "Effective string:" << endl;
        cout << line << endl;
        return 0;
    }
    cout << "Error!" << endl;
    return -1;
}

When I input "1" the result is no problem.But when I input "2", it does not let me input and show like the under line:

2
Please input :Welcome to C++family!
Effective string:
Enter any key to continue......

I can't understand why it runs like this and what is the mistake.
Please help me, thank you.

Chen
  • 21
  • 5
  • 2
    If that's the solution provided by the book itself, then you should either loop for an errata, or throw it away. Because the solution is faulty. It doesn't discard the newline after you input the `2`, which leads to `std::getline` reading it as an empty line. – Some programmer dude Jan 18 '18 at 07:56
  • 2
    Is the book perhaps [C++ Primer *Plus*](https://www.amazon.com/dp/0672326973/?tag=stackoverflow17-20) by Stephen Prata? Then you *should* throw it away! See e.g. [this review](https://accu.org/index.php?module=bookreviews&func=search&rid=1744) for a few reasons why. [C++ Primer](https://www.amazon.com/dp/0321714113/?tag=stackoverflow17-20) by Stanley B. Lippman et. al. on the other hand is an *excellent* beginners book. Also see [here for a list of good beginners (and other) books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282). – Some programmer dude Jan 18 '18 at 07:59
  • Thank you very much!I understand what the problem is because of your answer!And my book is C++ Primer by Stanley B. Lippman not C++ Primer Plus by Stephen Prata.But i want to ask more:Aren't the cin of ch and the cin of line different? – Chen Jan 18 '18 at 08:30
  • `std::cin` is `std::cin`, no matter how you read the input from it. Using e.g. `cin >> ch` you read a single character from the input, with `std::getline` you read a line *one character at a time*. – Some programmer dude Jan 18 '18 at 08:33
  • The use for `cin.sync()` is wrong. The intent may be ignoring the leaving `\n` character, but `cin.sync()` [is not guaranteed to do so](http://en.cppreference.com/w/cpp/io/basic_istream/sync#Notes). – xskxzr Jan 18 '18 at 08:49
  • @xskxzr 2Thank you very much.I change 'cin.sync()' to 'cin.ignore()' and put them in if(ch == '2') {..}.It can be run normal but I'm not sure it's right. – Chen Jan 18 '18 at 09:08
  • @Some programmer dude I understand:). Thank you! – Chen Jan 18 '18 at 09:12
  • I am looking at the C++ primer by Lippman 5th edition and this is surely not the excercise 3.4. Solving 99% of the exercises I don't recall this one, certainly not cin.sync() part. – atru Jan 18 '18 at 09:34
  • @atru I am using the Chinese version of this book.Maybe the number of page are different.And i check my English version of this book it appears at 3.2.2Exercises."Explain how whitespace characters are handled in the string input operator and in the getline function." – Chen Jan 18 '18 at 09:34
  • Makes sense then, they're probably different a bit. Yes, in my book that's 3.3 and 3.4 is checking strings for equality and some related tasks. 3.5 is reading strings, concatenating, and printing. Make sure you explain most of it ;) – atru Jan 18 '18 at 09:36
  • @atru I'm sorry it's my fault.it's 3.3 not 3.4.I have rectified it.Thank you:). – Chen Jan 18 '18 at 09:42
  • Oh, ok, so you were coding this to prove your explanation? I do it too, good luck :) – atru Jan 18 '18 at 09:57

0 Answers0