C++ cin whitespace question

后端 未结 1 1738
醉话见心
醉话见心 2020-12-20 10:13

Programming novice here. I\'m trying to allow a user to enter their name, firstName middleName lastName on one line in the console (ex. \"John Jane Doe\"). I want to make th

相关标签:
1条回答
  • 2020-12-20 10:51

    Use getline and then parse using a stringstream.

    #include <sstream>
    
    string line;
    getline( cin, line );
    istringstream parse( line );
    
    string first, middle, last;
    parse >> first >> middle >> last;
    if ( last.empty() ) swap( middle, last );
    
    0 讨论(0)
提交回复
热议问题