Dear Sir,
I am trying to print the string with the following code,
but I am getting errors like
Undefined symbol 'string'
Undefined symbol 'str'.

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

void main()
{
string str = "My name is shridhar";
cout<<"What is your name"<<str;
getch();
}

I think the problem is with C++ version.

Can you please tell me latest C++ version where One uses #include<string> instead of
#include<string.h> and using namespace std; instead of #include<conio.h>
Plase help for above queiries Thanks

Edited 5 Years Ago by Nick Evan: Added [CODE]-tags

First use code tags.

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

void main()
{
string str = "My name is shridhar";
cout<<"What is your name"<<str;
getch();
}

Yes
But I am using getch(). Ia there any problem.
I used return 0; also but i gives same prblem.

You are using depreciated header files, take out the .h. And int main is in these days. And you need to either use fully qualified name or use the using declaration.

#include<string>
#include<iostream>

int main(){
 using namespace std;

  string str = "My name is shridhar";
  cout<<"What is your name"<<str;

  char pause = 0;
  cout << "Press enter to continue...";
  cin.get(pause);
}

I agree with the above post (which works), but I would do it slightly different.

First of all as firstPerson stated, you are using depreciated headers. C++ now uses a standard "include <>" without ".h" extension (in most cases, not all).

Second, you were using objects that the compiler didn't have a library for, this is what the "using namespace std;" is for, you are giving it the std library. Otherwise you would have to manually link it to the library like so:
std::cout<<"What is your name"<<str; ) and so on...

Thirdly (and also stated above), there is NO such thing as "void main()" main has to return an int, and you can't return using a void function (main).

Here is the sample I corrected for you (it compiles and runs on my system):

#include <string>
#include <iostream>

using namespace std;
 
int main()
{ 
string str = "My name is Shridhar\n";
cout << "What is your name? " << str;

system("pause");
return 0;
}

Edited 5 Years Ago by kernel>panic: n/a

Include <stdio.h> function in your program, I think this is problem with this program.

Standard input/output is required in many applications, but it is not what will solve his problem. He had many issues, and the stdio library will not fix them.

Dear Sir,
If I put iostream instead of iostream.h , It is giving error as " Unable to include file 'iostream'.
Actually I am using TC C++ version 3.0 on windows XP, whether This version is previous one. If yes please let me know the latest good version of C++.

With #include<iostream.h> same problem as Unable to find string function as above mentioed is coming.

If I put std::cin, then it is giving error like "Type qualifier 'std' must be struct or class.

Besides It is giving same error like " Function 'sytem' should have prototype for using pause("system"). It is also giving same problem as Undefined symbol 'string'.
I fill the problem is with either Operating system (XP) or C++ version ( TC version 3.0) or as you mentioned it is not getting standard libarary.

Or what esle can you suggest.

Thnks

somshridhar,
If the code I gave you above compiled with all those errors, it most likely is your compiler.
I do not have much experience with Turbo C++, but I do know that version 3.0 was released 2008 I believe. Borland now uses one called C++ Builder XE.

I don't want to cause you a hassle, but If it were me, I would switch to a new IDE and compiler.
A simple one to use, yet full of features would be Bloodshed Dev-C++, which uses GCC as a compiler and has an intergrated IDE.
You can obtain a copy here: http://www.bloodshed.net/dev/devcpp.html
Download the Beta 5 version with Mingw/GCC 3.4.2.

I have tested that compiler with your code, and it runs without trouble.
Dev-C++ is updated frequently and includes all of the library's you will probably ever use (using the new standards)

Edited 5 Years Ago by kernel>panic: Bad link

Dear Sir,
Thank you for your support.
I am trying the following program with Dev C++.

// istream get
#include <iostream>
#include <fstream>
#include<string>
#include<cstring>
//#include<sstream>
using namespace std;

int main () {
char c, str[256];
ifstream is;

cout << "Enter the name of an existing text file: ";
cin.get (str,256);

is.open (str); // open file

ofstream was;
was.open("output.mvw");

string st ("There are two needles in this haystack with needles.");
string str2 ("needle");
size_t found;

// different member versions of find in the same order as above:
found=st.find(str2);
if (found!=string::npos)
cout << "first 'needle' found at: " << int(found) << endl;
// let's replace the first needle:
st.replace(st.find(str2),str2.length(),"preposition");
cout << st << endl;
while (is.good()) // loop while extraction from file is possible
{
c = is.get(); // get character from file
if (is.good())
was << c;
}

is.close(); // close file
was.close();
return 0;
}


But I am not getting output. Means actually even if I am trying with simple program also

like 

#include<iostream>
using namespace std;

int main()
{
int i;
cout<<"Enter a number";
cin>>i;
cout<<"The number is "<<i;
return 0;
}

For this program. A dos prompt window asks

Enter a number

When I put number 23. And entering then the DOS window is getting vanished.
So no output is displaying.

Please help as I am delying my work due to such problems.

Thank you

Edited 5 Years Ago by ~s.o.s~: added code tags; learn to use them.

somshridhar,

I hate to say it, but I am astonished you could write such an advanced program without it being obvious what your problem is.
That being said, the problem is an easy fix; just add this line of code before the 'return 0;' statement:

system("PAUSE");

Your problem is, you are seeing output (if you had cd into the program through command prompt you would have seen it)
but you had it close before you could read it. In the future always add that line of code before any statement that can exit the program to see your output.

Let me know if you run into further trouble, I can not test the program any further due to me not having the text file it requires by user.

P.S. I have to correct you on one thing as it bugs me, you were not seeing a 'DOS' window, you were seeing a 'Command Prompt' there is a difference.

Edited 5 Years Ago by kernel>panic: Text formating after viewing

Comments
NEVER suggest system("pause");

cin.get() buys you a nice portable solution to the "pause" issue. Sometimes a cin.ignore() is required beforehand to clean up junk in the stream.

cin.get() buys you a nice portable solution to the "pause" issue. Sometimes a cin.ignore() is required beforehand to clean up junk in the stream.

I agree with that, except that 'cin.get()' doesn't always work in every case.
Try running the simple version of his program with cin.get(), it does not pause it.
Unless I am missing something, cin.get() alone will not solve his problem.

Yeah, sometimes you need the cin.ignore() (or the overload that will accept multiple characters up to a sentinel which can be '\n') (see http://www.cplusplus.com/reference/iostream/istream/ignore/)

#include<iostream>
using namespace std;

int main()
{
   int i;
   cout<<"Enter a number";
   cin>>i; //user enters an integer + presses Enter => '\n' left in the stream
   cout<<"The number is "<<i;
   cin.ignore();  //picks up '\n'
   cin.get();   //accepts keypress
   return 0;
}

Okay, I see. By adding cin.ignore() you get around that.
Smart, especially if you were to go portable, like you said,
This would work on other platforms as well.
But if you are strictly sticking to windows, I would still use system("PAUSE").

System("pause") is still a bad idea (as you can see from some of the rep comments above) because all one have to do is substitute another program to do just about anything, call it "pause," and every run of your software could be contributing to the delinquency of minors.

Anyway, not trying to give you personally a hassle about it, just stuff I've picked up on here and trying to spread good information.

Yes, I admit to it being an improper programming practice to use that function.
somshridhar when you see this post, please take note of it. jonsca is correct.

I should stop using the system("PAUSE)" function so much, but those transitions are hard for us "lazy" programmers,
we hate going the extra mile (good example, Bill Gates, heh heh, no offense meant)

Edited 5 Years Ago by kernel>panic: n/a

Comments
It's all good

'string' is not a datatype, so str naturally becomes a invalid variable. try this...

#include<conio.h>
#include<string.h>
#include<iostream.h>
 
void main()
{
char str[] = "My name is shridhar";
cout<<"What is your name"<<str;
getch();
}

Edited 5 Years Ago by Nick Evan: Added [CODE] tags

Comments
Worst advice in this entire thread
Very unorganized!
Prestandard, over three years experience on the site and no code tags

Thank a lot.
Now my program is woking nicely.
My string function is also working properly.(With Dev C++...Again thanks)
As per my above program now actullay I am trying to replace a word from a line and write the sentence again with the replaced word.
I am successful in in above task.
Now I am reading a input text file. like suppose

My name is Shridhar
My wife's name is Radha

Now suppose I want to replace second sentence with
My brother's name is somnath.

Means I want to replace wife by brother and Radha by Somnath.
and put both the sentences in output.txt.

Actually, As per above program Separately I can replace the words and Separartely I can read file and write it in output file.

But How can I read file, Replace some content from the file and write it in Output file. Means how can I combine both Read Write and Find Replace function.

Can you please help me.

I don't quite understand what you are trying to do. Are you saying you want to integrate all those functions into one program?
If so I can help you accomplish that, give me the code you have written already and I will show you how to combine it.

This article has been dead for over six months. Start a new discussion instead.