Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 151
  • Last Modified:

Troubles with String type

I am just trying to learn C++ and I have just been frustrated beyond belief.  I'm working on a simple little program from a text book that is just supposed to ask for your name and greet you and no matter what I try to do I cannot get it to work.  My program looks something like this.

/*String type demo program*/  

#include <iostream.h>
#include <string.h>

int main()

  string Name;

  cout<<"Enter your name:";
  cout<<"!Nice to meet you!"<<endl;

and these are the 3 errors I recieve:
1. String type Demo.cpp(9) : error C2065: 'string' : undeclared identifier
2. String type Demo.cpp(9) : error C2146: syntax error : missing ';' before identifier 'Name'
3.String type Demo.cpp(9) : error C2065: 'Name' : undeclared identifier

If someone can help me out it would be greatly appreciated, thanks a lot
2 Solutions
is that example right from the book????
throw that book away if that's so
change your includes to:
#include <iostream>
using std::cout;
using std::cin;
using std::endl;

#include <string>
using std::string;

#include <iostream>
#include <string>
using namespace std;
Some compilers do not include the string CLASS in the main headers location, perhaps the book describes how to create a class and uses as an example a CUSTOM string class, so go some pages back, and read again, there may be explained how to create such class, and then use something like this:

#include "C:\compiler\mysrc\string.h"

    string NAME


instead of

#include <string.h>

of course this is on windows or MS-DOS based compilers on unix based the path may be slighty different.

remeber that the path on include statement, should point to the place where your custom string class resides.

Hope it helps.

>> return(0)

This is a syntax error right here. Where is the semicolon? This line should look like,



return 0;

Note that the braces are not neccessary because return is a keyword and not a function.


This is perfectly legal. Return simply returns control of the program, and a return value dpending on whether the function was declared void, to the function that called it. Therefore, braces are only needed in a return statement to indicate the order of operations in an expression. Since this simply passes back a numeric value they are not necessary.

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
Post your closing recommendations!  No comment means you don't care.
Point split: PlanetCpp and Exceter.
Hoogie, if you create a class for example:

class test

you can then create instances of that class:

test X;
test Y;

the class DECLARATION is stored in a ".h" file

so if you don;t include this file and then try to create instances of that class you will
recieve that you are getting after compiling.

look at you string.h file and see if thereis something like:

class string

if not, you are including the incorrect file, or even string.h does not exists
string.h is a CLASS declaration, you are getting the error because of this.

The error message is clear..

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now