0

何も入力せずにEnterキーを押すと、getline()関数も空白の入力を受け取ります。空白入力 (文字および/または数字および/または記号を含む) を許可しないように修正するには?

string Keyboard::getInput() const
{
    string input;

    getline(cin, input);

    return input;
}    
4

3 に答える 3

3

入力が空白である限り、getline をやり直すことができます。例えば:

string Keyboard::getInput() const
{
    string input;

    do {
      getline(cin, input);    //First, gets a line and stores in input
    } while(input == "")  //Checks if input is empty. If so, loop is repeated. if not, exits from the loop

    return input;
}
于 2013-07-28T09:20:33.000 に答える
2

これを試して:

while(getline(cin, input))
{
   if (input == "")
       continue;
}
于 2013-07-28T09:19:47.293 に答える
2
string Keyboard::getInput() const
{
    string input;
    while (getline(cin, input))
    {
        if (input.empty())
        {
            cout << "Empty line." << endl;
        }
        else
        {
            /* Some Stuffs */
        }
    }
}
于 2013-07-28T09:21:34.330 に答える