-1

Writing my first program, and my using namespace std; statement won't work. When I build the program I'm getting thrown this error:

C:\\Users\\p6735a\\Desktop\\Project\\game.cpp: In function `int main(int, char *)':
C:\\Users\\p6735a\\Desktop\\Project\\game.cpp:6: `string' undeclared (first use this         function)
C:\\Users\\p6735a\\Desktop\\Project\\game.cpp:6: (Each undeclared identifier is reported only once
C:\\Users\\p6735a\\Desktop\\Project\\game.cpp:6: for each function it appears in.)
C:\\Users\\p6735a\\Desktop\\Project\\game.cpp:6: parse error before `;'
[Finished in 0.1s with exit code 1]

Here's the code:

#include <iostream>
using namespace std;

int main(int argc, char *argv[])
{
    string input;
    int hp;

    cout << "You see a man. Would you like to kill him?\n1. Yes\n2. No" << endl;
    //cin >> input;
}
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
amartin94
  • 505
  • 3
  • 19
  • 35

3 Answers3

8

You need to include the string header:

#include <string>

But why have a using namespace std declaration at all? Simply use the objects individually:

using std::cout;
using std::string;
David G
  • 94,763
  • 41
  • 167
  • 253
  • 1
    @amartin94, It's unspecified what headers other headers include, so you should always include what you need. I would be somewhat wary of a tutorial that doesn't do that. [Good books](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) won't. – chris Apr 30 '13 at 01:16
6

Add

#include <string>

to your includes since string is from <string> header.

Meanwhile, it is bad practice to use using namespace std (see Why using namespace std is considered a bad practice), you'd better use:

std::string input
std::cout, std::endl

instead.

Community
  • 1
  • 1
taocp
  • 23,276
  • 10
  • 49
  • 62
-1

In Visual Studio you need to have the new #include "pch.h".

double-beep
  • 5,031
  • 17
  • 33
  • 41
SilverFang
  • 23
  • 7