0

I am a mere beginner, who is currently learing C++ from freecodecamp.org's Youtube channel. The following is my code

#include <iostream>
#include <string>

using namespace std;

int main()
{
    int age; 
    cout << "What is your age? ";
    cin >> age;
    cout << "You are " << age << " years old." << endl;

    string name;
    cout << "What is your name? ";
    getline(cin, name);
    cout << "Oh, so you are " << name << " .";
    return 0;

}

The code does ask me for the age input, but is not promting name input. Instead, it directly skips that step and prints the last cout line. The following is the output

What is your age? 12
You are 12 years old.
What is your name? Oh, so you are  .

Another thing that beats me is that the instructor in the youtube video did not include the header "string", but still he was able to use the getline() method. In visual studio I am unable to do so. I have to include the #include "string". And as if that wasn't confusing enough, I tried to go without #include "string" in Clion and it worked! I am not quite getting it now. Can someone please help me?

  • Your header confusion is because headers can include other headers depending on the implementation. Evidently your implementation differs from the instructor's. Don't rely on implicitly included headers and you'll be fine. – chris Oct 01 '20 at 03:14

0 Answers0