Home > Enterprise >  VS Code :: C :: Error with giving inputs to the program
VS Code :: C :: Error with giving inputs to the program

Time:04-05

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;

CodePudding user response:

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;
}

CodePudding user response:

You have to declare 'first_name' before using it.

string first_name;
  •  Tags:  
  • c
  • Related