程序无法正确找到名称“John Fitzgerald Kennedy”的每个部分的子字符串,并且无法在单独的行上输出每个名称。程序输出一个超出范围的异常,甚至不显示第二个名字,只显示第一个。我将如何在每一行输出每个名称?
#include <iostream>
#include <string>
using namespace std;
int main()
{
string fullName="",
firstName="",
middleName="",
lastName="";
cout<<"Enter your full name: ";
cin>>fullName;
firstName=fullName.substr(0,4);
middleName=fullName.substr(4,14);
lastName=fullName.substr(14,19);
cout<<firstName<<endl;
cout<<middleName<<endl;
cout<<lastName;
cin.get();
cin.get();
return 0;
}