#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;
}
This program should return "is_a_good_boy" when I give input "Jhon is_a_good_boy". But it prints "Jhon". Help me solve this, please.
You probably should approach this with getline in order to capture the whole line entered as well as do validation when using string.find
Commented example below.
#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;
}