Having problem using strings - Compiling Error

Having problem using strings - Compiling Error

I am using a book that teaches C++. Here is the program I am having a problem with:


#include ;
#include 
int main ( )
{
string  name;
cout << "What is your name?" << endl;
cin >> name;
cout << "Your name is "<< name << " .";
return 0;
}

The error message in the compiler says:


string  name;
           ^
question.cpp(5) : Error: undefined identifier 'string'
--- errorlevel 1

Every time I try to use a string, I get that message. Shouldn't #include cover it? If you still don't know, the source code is attatched, if you need to see it.

Thanks, Pike

P.S. The file is in zip format

4 Beiträge / 0 neu
Letzter Beitrag
Nähere Informationen zur Compiler-Optimierung finden Sie in unserem Optimierungshinweis.

Why not from the std namespace?
as in
#include
#include
using namespace std;

The answer given by the user above is correct but I would get in the habit of using char arrays. Becaus strings aren't null terminated and you will run into more problems as your learning progresses (i.e. file I/O if you pass a string object to the open member of a fstream object it will not work. To fix this you have to eigth get in the habit of NULL terminating your strings (for the string class .data() NULL terminatines your string)or use char arrays). Also you have a extra semi colon after your iostream declaration.

#include ;

int main ( ){
char name[255];
cout << "What is your name?" << endl;cin >> name;
cout << "Your name is "<< name << " .";
return 0;
}

This should be working
#include <iostream>
using namespace std;
int main ( ){

string  name;
cout << "What is your name?" << endl;
cin >> name;
cout << "Your name is "<< name << " .";
return 0;

}

Kommentar hinterlassen

Bitte anmelden, um einen Kommentar hinzuzufügen. Sie sind noch nicht Mitglied? Jetzt teilnehmen