How do I stop program from skipping over getline? [duplicate]

我是研究僧i 提交于 2019-12-13 15:55:45

问题


This is my main program,

int main () {

    string command;
    cin>>command;

    if(command == "keyword")
    {
        string str, str2, str3, str4;

        cout << "Enter first name: ";
        getline (cin,str);

        cout << "Enter last name: ";
        getline (cin,str2);

        cout << "Enter age: ";
        getline (cin,str3);

        cout<<"Enter country: ";
        getline (cin,str4);

        cout << "Thank you, " << str <<" "<<str2 <<" "<<str3<<" "<<str4<< ".\n";
    }
}

When keyword is entered, the program immediately outputs :

Enter first name: Enter last name:

completely bypassing the ability to enter the first name.


回答1:


string command;
cin>>command;

after this just eat the end of the line

string restOfLine;
getline(cin, restOfLine);

Otherwise the '\n' in the line where you input command is not consumed and the next readline reads just it. HTH




回答2:


cin >> command does not extract the newline character ('\n') from the input stream; it's still there when you call getline(). Therefore, you need an extra dummy call to getline() (or ignore()) to deal with this.




回答3:


As mentioned by others, the problem is that while reading the command you are leaving the end of line character in the buffer. Besides the alternative proposed by @Armen Tsirunyan, you can use two other approaches:

  • Use std::istream::ignore for that: cin.ignore( 1024, '\n' ); (assuming that lines will not be greater than 1024 characters in width.

  • Just replace cin >> command with getline( cin, command ).

Neither alternative requires creating an extra string, the first is weaker (in the event of very long lines), the second alternative modifies the semantics, as now the whole first line (not just the first word) is processed as the command, but this might be fine as it allows you to perform tighter input checking (the command is spelled as required in the first word, and there are no extra options in the command line.

If you have different set of commands and some might need an argument, you can read the command line in one pass, and then read the command and arguments from there:

std::string commandline;
std::vector<std::string> parsed_command;
getline( cin, commandline );
std::istringstream cmdin( commandline );
std::copy( std::istream_iterator<std::string>(cmdin), std::istream_iterator(),
           std::back_inserter( parsed_command ) );
// Here parsed_command is a vector of word tokens from the first line: 
// parsed_command[0] is the command, parsed_command[1] ... are the arguments


来源:https://stackoverflow.com/questions/4067256/how-do-i-stop-program-from-skipping-over-getline

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!