为什么该程序打印预期结果的反面?

问题描述 投票:-1回答:1
#include <bits/stdc++.h>
using namespace std;
int main()
{
     string in_code;
     std::cin >> in_code;
     int word;
     word = in_code.find(" ");
     in_code.erase(0, (word + 1));
     cout << in_code << endl;
}

当我输入“ Jhon is_a_good_boy”时,该程序应返回“ is_a_good_boy”。但是它会打印“ Jhon”。请帮我解决这个问题。

c++ function cin erase
1个回答
0
投票

您可能应该使用getline处理此问题,以便捕获输入的整个行以及在使用string.find时进行验证

© www.soinside.com 2019 - 2024. All rights reserved.