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

3

2

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.

Parkins answered 1/11, 2010 at 7:32 Comment(0)
P
3
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

Proglottis answered 1/11, 2010 at 7:35 Comment(1)
Special ignoreline and "blankline extractor" functions can make this easier.Leatherwood
C
3

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.

Coster answered 1/11, 2010 at 7:38 Comment(0)
N
1

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
Novanovaculite answered 1/11, 2010 at 8:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.