为什么getline()不能在我的小实验中工作?

时间:2012-10-26 05:56:57

标签: c++ string input lambda getline

    //////////////////////////////////////////////////////////////////
    // Capture a lambda with a lambda and use it in the lambda it's //
    // captured in along with some code in the lambda that captures //
    // it.                                                          //
    //                                                              //
    // Add to that to capture a variable in the client and use that //
    // too.                                                         //
    //                                                              //
    // Then make a lambda that captures a class object and calls    //
    // some method or methods with it, optionally modifies the re-  //
    // sult...                                                      //
    //////////////////////////////////////////////////////////////////

    //////////////////////////////////////////////////////////////////
    // Note: std::function<type(type)> f;                           //
    // f = <define lambda here>                                     //
    // f() //calls lambda                                           //
    //////////////////////////////////////////////////////////////////

    #include <functional>
    #include <iostream>
    #include <string>
    using std::cin;
    using std::cout;
    using std::endl;
    using std::function;
    using std::getline;
    using std::string;

    class Experimental {
       private:
          int x;
          string s;
       public:
          Experimental() {}
          ~Experimental() {}
          void set_x(int new_x);
          int get_x();
          void set_s(string s_in);
          string get_s();
    };

    void Experimental::set_x(int new_x) {
       x = new_x;
    }

    int Experimental::get_x() {
       return (x);
    }

    void Experimental::set_s(string s_in) {
       s = s_in;
    }

    string Experimental::get_s() {
       return s;
    }

    int main() {
       double n;
       string input;
       Experimental* experiment = new Experimental();

       cout << "Enter a number: ";
       cin >> n;

       function<double(double)> f;
       f = [&f](double k) {
          return (k ? k * f(k-1) : 1);
       };

       function<double(double)> g;
       g = [&f,n](double m) {
          return (f(n)/n);
       };

       function<int()> T1; //capture a class and do stuff...
       T1 = [&experiment,n]() {
          experiment->set_x(13 + n);
          int m = experiment->get_x();
          return (m);
       };

       function<string(string)> T2; //capture a class and do stuff...
       T2 = [&experiment](const string in) {
          experiment->set_s(in);
          string s = experiment->get_s();
          return (s);
       };

       cout << "The factorial of " << n << " is: ";
       cout << f(n) << endl;

       cout << "The factorial of " << n << " divided by " << n << " is: ";
       cout << g(n) << endl;

       cout << "The new value of x in experiment is: ";
       cout << T1() << endl;

       cout << "Enter a string: ";
       getline(cin, input); //FIXME
    cout << "input is: " << input << "<-" << endl;

       cout << "The new string in experiment is: ";
       cout << T2(input) << endl;
       delete experiment;

       return (0);
    }

我知道这很难看。不是真的使用lambdas,因为我们最初在这里尝试时会使用它们。出于某种原因,我没有得到我的字符串变量的输入,我不知道为什么。有人可以帮助/指出问题所在吗?

2 个答案:

答案 0 :(得分:4)

问题是你的getline正在读取缓冲区中剩余的行尾:

cin >> n;

使用ignore修复此问题:

#include <limits>
...
  cin >> n;
  ...
  cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
  getline(cin, input);

答案 1 :(得分:1)

这是一个非常常见的错误,基本上你有

cin >> number;
getline(cin, str);

现在想想这个(你似乎是一个优秀的程序员,所以我只给你一个线索)。一个数字中包含多少个换行符? cin >> number;实际读取的换行数是多少?