string - Simple C++ Substring Issue -


the program not find substrings of each section of name "john fitzgerald kennedy", , cannot output each name on separate line. program outputs out of range exception , not show second name, first. how ouput each name on each separate line?

#include <iostream> #include <string>  using namespace std;  int main() {  string fullname="",         firstname="",         middlename="",         lastname="";  cout<<"enter 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 standard input when encounters first space. need command

getline(cin, fullname); 

to read entire line along spaces , chunk them different parts of name.


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -