user2988694
user2988694

Reputation: 89

VS Code :: C++ :: Error with giving inputs to the program

I have just setup the VS Code to run C++ using the YouTube video

Now when I write a simple code

#include <iostream>

using namespace std;

int main()
{

    cout << "Enter your first name:";
    cin << first_name;
    cout << "Your name is" + first_name;

    return 0;
}

I keep getting the error

PS C:\Users\raman\OneDrive\Documents\C++_Projects> cd "c:\Users\raman\OneDrive\Documents\C++_Projects\" ; if ($?) { g++ Input.cpp -o Input } ; if ($?) { .\Input }Input.cpp: In function 'int main()':

Input.cpp:8:18: error: 'first_name' was not declared in this scope
    
8 | cin <<  first_name;

Upvotes: 0

Views: 65

Answers (2)

ksohan
ksohan

Reputation: 1203

Your code has two problems:

  1. First first_name is not declared
  2. cin uses >>, not <<
#include <iostream>

using namespace std;

int main()
{
    string first_name;

    cout << "Enter your first name:";
    cin >> first_name;
    cout << "Your name is" + first_name;

    return 0;
}

Upvotes: 3

Haunui
Haunui

Reputation: 68

You have to declare 'first_name' before using it.

string first_name;

Upvotes: 2

Related Questions