0

I'm new to C++ and I am trying to get an int and then a string just like in the code below:

If I switch the order - get the string first and then the int - it works fine, but doing it in this order doesn't work.

When I enter the age it just skips the input for the name. Why does this happen? And how can I fix it?

#include <iostream>
#include <string>

using namespace std;

int main() {
    int age;
    string name;

    cout << "Enter age : ";
    cin >> age;
    cout << "Age : " << age << endl;

    cout << "Enter name : ";
    getline(cin, name);
    cout << "Name : " << name << endl;

    return 0;
}
Mark Rotteveel
  • 82,132
  • 136
  • 114
  • 158

0 Answers0