The program does not properly find the substrings of each section of the name "John Fitzgerald Kennedy", and cannot output each name on a separate line. The program outputs a out of range exception and does not even show the second name, only the first. How would I ouput each name on each separate line?
#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;
}
cin>>fullName;
stops reading the standard input when it encounters the first space. What you need is a command like
getline(cin, fullName);
to read the entire line along with the spaces and then chunk them to get different parts of the name.