Troubles with String type

Posted on 2003-03-01
Medium Priority
Last Modified: 2010-04-17
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
Question by:Hoogie
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Accepted Solution

PlanetCpp earned 100 total points
ID: 8049872
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;

Expert Comment

ID: 8050839
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.


Assisted Solution

Exceter earned 100 total points
ID: 8053219
>> 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!


Expert Comment

ID: 9447420
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.

Expert Comment

ID: 9450927
Point split: PlanetCpp and Exceter.

Expert Comment

ID: 9472474
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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
The SignAloud Glove is capable of translating American Sign Language signs into text and audio.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question