0
#include <iostream>
#include "stdafx.h"
#include <string>

using namespace std;

int main()
{
    //Variables in which student info is stored
    string name = "";

    //asks user for their name
    cout << "/nHello, my name is Hal, what is your name? ";

    cin >> name;

    cout << "/nWelcome to C++ ";
    cout << name;
    cout << ", let the adventure begin!";

    return 0;
}

Really basic code that I can't seem to get to work. Everywhere I have cin or cout the compiler says they're an undeclared identifier. I've looked at all of the answers on here and other forums and it none of the solutions seem to fix it. Thanks in advance.

Lance P
  • 19
  • 1
  • 6
  • How are you compiling? – matt Jun 30 '16 at 06:59
  • Visual studio 2015 – Lance P Jun 30 '16 at 07:01
  • Are you sure you're using c++? The program compiles fine with g++ on this end. sans `include stdafx.h` of course. – matt Jun 30 '16 at 07:03
  • Im using a visual c++ win32 console application from visual studio – Lance P Jun 30 '16 at 07:05
  • You should probably add a tag for visual studio, this looks like a Visual Studio problem. You might try uninstalling VS and re-installing it. – matt Jun 30 '16 at 07:07
  • 1
    http://stackoverflow.com/questions/4726155/whats-the-use-for-stdafx-h-in-visual-studio "Simply list all your big huge headers for your APIs in your stdafx.h file, in the appropriate order, and then start each of your CPP files at the very top with an #include "stdafx.h", before any meaningful content (just about the only thing allowed before is comments)." – Peter Jun 30 '16 at 07:08
  • Try to define cout and cin as its the main problem : using std::cout; using std::endl; – Peter Jun 30 '16 at 07:12
  • When i had cout and sin defined directly and didnt have the using namespace std it threw even more errors – Lance P Jun 30 '16 at 07:15
  • 2
    What happens when you move the `#include stdafx.h` to the top of the file? – matt Jun 30 '16 at 07:16
  • 1
    This is basically the same problem as here: http://stackoverflow.com/questions/1435804/why-am-i-unable-to-ifdef-stdafx-h , even though the code before stdafx.h is different. The accepted answer applies though. – Sebastian Redl Jun 30 '16 at 07:57
  • Sebastian fixed it. Thank You! – Lance P Jun 30 '16 at 17:54

1 Answers1

3

Hope you have the "stdafx.h" file that you are trying to include in the right path. The code works fine without the file being included.

Vaibhav N Naik
  • 350
  • 1
  • 8