Why is it that whenever I use the getline function I get an error during execution. To clarify...
I have this code;
cout<<"Enter your string: ";

getline(cin, myString); //line 1
cout<<"Enter your name: ";
getline(cin, myName);   //line 2
cout<<"Age: ";
cin>>age;               //line 3

output:
____________________________
Enter your string: gfgfkjhjkhgg
Enter your name:
Age: 25
____________________________

As you can see, when you use the getline function the second time, the extraction stream fails and skips to the next non-String variable. Is that not weird? The result is that you can't enter any succeeding string variables anymore.
What is the solution to this?

Thanks in advance.

Hello.

Your buffer is probably full of garbage.

Use cin.sync() between asking for inputs and you should be good to go.

Like this:

cout << "Enter your string:";
getline(cin, myString); //line 1
cout<<"Enter your name: ";
getline(cin, myName);   //line 2
cin.sync();
cout<<"Age: ";
cin>>age;
cin.sync();

  return 0;

Kind regards,
Petcheco

@Petcheco,

wrt your suggestion ... please note added comments below:

cout << "Enter your string:";
getline(cin, myString); // this will read the whole line
cout<<"Enter your name: ";
getline(cin, myName);   // this will read the whole line
// cin.sync(); // SO this is *NOT needed* here //

/*
cout<<"Age: ";
cin>>age;
cin.sync(); // may fail here if cin >> age failed
*/
while( true )
{
    cout << "Age: ";
    if( cin >> age && cin.get() == '\n' )
        break; // since good data was entered followed by '\n' char //

    // else if reach here ...

    cin.clear(); // clear any error flags
    while( cin.get() != '\n' ) ; // flush cin stream //
    cout << "\nPlease enter ONLY a valid integer here ...\n";
}

Your problem may be compiler/system specific. When I run your code on 2 different compilers(VS, Cygwin) it works fine.

One possibility which might explain what you're seeing, if your system uses \n\r for each newline, then getline will only read to the \n and leave \r in the buffer, which the next getline will read and move one.

One workaround for this could be to use cin.ignore(1); after each getline.

1.getline(cin, myString); 2.cout<<"Enter your name: "; cin.ignore(); 3.getline(cin, myName); 4.cout<<"Age: "; 5.cin>>age

what cin.sync() do

This little demo may help ...

// demo_cin.sync.cpp //

#include <iostream>
#include <string>
#include <cctype> // re. tolower, toupper


int takeInChr( const std::string& msg )
{
    std::cout << msg << std::flush;
    std::string reply;
    getline( std::cin, reply );
    if( reply.size() )
        return reply[0];
    // else ...
    return 0;
}
int takeInChr2(  const std::string& msg )
{
    std::cout << msg << std::flush;
    int c = std::cin.get();
    // std::cin.sync(); // or could use instead ... //
    if( c != '\n' ) while( std::cin.get() != '\n' ) ;
    return c;
}

bool more( const std::string& text = "" )
{
    if( tolower( takeInChr2( "More " + text +
                 "(y/n) ? " ) ) == 'n' )
        return false;
    // else ...
    return true;
}



int main()
{
    do
    {
        char c1 = toupper( takeInChr( "Enter the first letter of your 1st name: " ));
        char c2 = toupper( takeInChr2( "Enter the first letter of your 2nd name: " ));

        std::cout << "Hi " << c1 << '.' << c2 << ". How are you today?\n";
    }
    while(  more() ) ;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.