Share your repls and programming experiences

← Back to all posts
Bug?
Quillbert (0)

The getline doesn't seem to work. May just be incompetent programming, so I wanted to see if this was a bug.

Commentshotnewtop
mwilki7 (181)

cin and getline, if I recall correctly, don't handle newline characters '\n' the same. I recommend sticking with just one or the other.
cin still works for strings

int main() 
{
    int age = -1;
    string name;
  
    cout << "Enter your age: ";
    cin >> age;
    cout << age << endl;
  
    cout << "Enter you name: ";
    cin >> name;
    cout << name << endl;
    
    cout << "Enter age again: ";
    cin >> age;
    cout << age << endl;
    
    return 0;
}
SPQR (374)

Don't use cin and getline(cin) together, they aren't really compatible. Use only getline(cin) or only cin for all of the vars and it should fix your problem :)