Queries about strstream.h

V

vishnu mahendra

hello to all,
#include<iostream.h>
#include<conio.h>
#include <strstream.h>
void main()
{
clrscr();
strstream str;
char *a = new char[50];
int i;
cin >> i;


ostrstream s;
s<<"\nvalue of i is "<<i<<ends;
cout<<"\nIn s we have"<<s.str();

str<<i<<ends;
cout<<"\nusing rdbuf "<<str.rdbuf();

getch();
}

correct me if i am wrong.
Is Strstream is just used for converting interger and formatting it into strings.

what is the difference between "s.str()" and "str.rdbuf()" ?
the output is the same.

Did strstream have anyother functions.
I checked c++ library , but i cannot understand a word.


thankyou in advance,
vishnu
 
B

Buster

vishnu mahendra wrote:

Your textbook is due for recycling. Please don't sell it on.
#include<iostream.h>

Wrong. The standard C++ header you want is called 'iostream',
without the ".h".
#include<conio.h>

Wrong. There is no standard C++ header corresponding to this one.
#include <strstream.h>

Wrong. This header and everything it provides is deprecated.
You should use the 'sstream' header and the 'stringstream'
class template instead.
void main()

Wrong. The main function always returns int.
{
clrscr();

Wrong. There is no way to clear the console's screen in standard C++.
strstream str;

Should be "std::stringstream str;", but why bother, if you're not going
to use it?
char *a = new char[50];

(i). You never delete [] the memory allocated here. That's bad.
(ii). You never use the variable a at all, so don't declare it.
(iii). Why 50?

Use std::string for strings.
int i;
cin >> i;

Should be "std::cin >> i;".
ostrstream s;

Should be "std::eek:stringstream s;"
s<<"\nvalue of i is "<<i<<ends;

You don't need the "ends" with std::stringstream,
since std::string doesn't need to be null-terminated.
cout<<"\nIn s we have"<<s.str();

OK, but qualify "cout" with "std::".
str<<i<<ends;

What was that for?
cout<<"\nusing rdbuf "<<str.rdbuf();

I daresay you don't need to know about std::basic_ios said:

Wrong. There is no way to wait for a single keypress in standard C++.
}

correct me if i am wrong.
Is Strstream is just used for converting interger and formatting it into strings.

You shouldn't be using it at all. stringstream, on the other hand, is
used for converting any built-in type into a string. It also works with
user-defined types if you provide the right overloaded << operator.
what is the difference between "s.str()" and "str.rdbuf()" ?
the output is the same.

std::stringstream::str () returns the string you want. std::basic_ios
<>::rdbuf () returns a pointer to the streambuf used by the stream
object on which it is called. The << operator is overloaded in the
basic_ostream class to allow the insertion of a streambuf into an output
stream. This is mostly of interest to people implementing stream, not
users.
Did strstream have anyother functions.

std::stringstream has lots of member functions.
I checked c++ library , but i cannot understand a word.

Get a better book. I recommend "The C++ Programming Language",
3rd/Special edition, by Bjarne Stroustrup, for starters.
 
V

vishnu mahendra

Hello sir,

Thank you for the help. your help is much appreciated.
By the way i forgot to tell you that i am using Turbo C++.
i am a newbie so i made lots of mistakes.

thankyou again,
vishnu
 
B

Buster

vishnu said:
Thank you for the help. your help is much appreciated.
By the way i forgot to tell you that i am using Turbo C++.
i am a newbie so i made lots of mistakes.

I'm glad you found it helpful. Sorry if I was rude.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top