0

I just started learning c++, and as I'm learning the basics, I noticed that when I asked for two inputs (cin for age and getline() for a name) the program would ask the user for the age (the first input) but then shows all of the cout(s) without asking for the second input (getline()). Is there a c++ concept about inputs and outputs that I'm unaware of? because, as far as I'm aware, my code doesn't seem to have a problem.

here's a snippet of a very simple code:

#include <iostream>

using namespace std;

int main()
{
    int age;
    cout << "Enter your age: ";
    cin >> age;
    cout << "You are " << age << " years old." << endl;

    string name;
    cout << "Enter your name: ";
    getline(cin, name);
    cout << "Your name is registered as " << name << endl;


    return 0;
}
  • Tactical note: Spend some time getting familiar with the debugging tool that should have come with your development software (and get different development software if there is no debugger). With a debugger you can step through the program a line at a time and see exactly what happened on that line. If something happens that you didn't expect, you found a bug or you have bad expectations. Either one needs to be fixed. If the line is too complicated for you to figure out where things went wrong, break the line up into a different line for each operation so you can see where things went wrong. – user4581301 Jan 26 '22 at 22:32

0 Answers0