#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”。请帮我解决这个问题。
答案 0 :(得分:1)
您可能应该使用getline来实现此目的,以便捕获整个输入行并在使用string.find时进行验证
下面的注释示例。
#include <string>
#include <iostream>
int main()
{
std::string in_code; //String to store user input
std::getline(std::cin, in_code); //Get user input and store in in_code
int word = in_code.find(" "); //Get position of space (if one exists) - if no space exists, word will be set to std::string::npos
if (word != std::string::npos) //If space exists
{
in_code.erase(0, word + 1); //erase text before & including space
std::cout << in_code << std::endl;
}
else
{
std::cout << "The entered input did not contain a space." << std::endl;
}
return 0;
}