在我输入这个C ++代码后,为什么我得到0

时间:2016-03-25 04:32:44

标签: c++

我正在尝试解决此问题http://a2oj.com/p?ID=24。但是,当我尝试接受测试用例时,在输入测试用例编号并按下enter后,控制台窗口显示0。 我的代码:

#include <cstring>

using namespace std;

int main ()
{
  int t; //test cases
  string str;
  cin >> t;
  while(t--){
    std::getline (cin,str);
    int len = str.size();
    cout << len;
  }
  cin.get();
  return 0;
}

1 个答案:

答案 0 :(得分:1)

您需要使用std::getline(string)功能。请参阅此示例代码:

// extract to string
#include <iostream>
#include <string>

    int main ()
    {
      std::string name;

      std::cout << "Please, enter your full name: ";
      std::getline (std::cin,name);
      std::cout << "Hello, " << name << "!\n";

      return 0;
    }
相关问题