Passing on ostringstream

V

vsuresh.cs

Hi,

My c++ program looks likes this

void api1() {
cin << info;
ostringstream ostr;
ostr << info;
api2(ostr.str().c_str()); // Getting string from ostreamstream and
getting char* from string
}

void api2(const char* ch) {
insert map(ch);
}

Sample input to api2 "a" after that when i checked the content of map
at the end. I am seeing some junk values.

Can some one explain what is going wrong in my program? Do i should
not do c_str() on ostringstream?.

Thanks,
Suresh
 
E

Erik Wikström

Hi,

My c++ program looks likes this

void api1() {
cin << info;
ostringstream ostr;
ostr << info;
api2(ostr.str().c_str()); // Getting string from ostreamstream and
getting char* from string
}

void api2(const char* ch) {
insert map(ch);
^^^^^^^^^^^^^^^^^ Syntax Error!
Sample input to api2 "a" after that when i checked the content of map
at the end. I am seeing some junk values.

Can some one explain what is going wrong in my program? Do i should
not do c_str() on ostringstream?.


I can not say for sure since I do not know what map is or how you insert
into it (please see section 5.8 in the FAQ). I would guess that you
insert ch in the map, and since ch is a pointer to a temporary (or at
least an object which will be destroyed at the end of api1()) you only
get garbage later on.

Instead of using c_str() to get a cont char* you should use the return-
value from str() as the key in the map, playing around with char arrays
unless you have to is bad.
 
V

vsuresh.cs

Hi Sam/Erick,

I am writing my program in detail

std::map<string,int> cache;

void api1() {

cin << key; // string
cin << value; // int
ostringstream ostr;
ostr << key;
api2(ostr.str().c_str(), value);

}

void api2(const char* info, int value) {
cache[info] = value;
}

So, here i am not storing the info* but copying the value of that
pointer to string and keeping it in my MAP.
But when i print this cache it refers to junk value.

+suresh
 
B

Ben Bacarisse

I am writing my program in detail

std::map<string,int> cache;

void api1() {

cin << key; // string
cin << value; // int

Do you really have << operators on cin?
ostringstream ostr;
ostr << key;
api2(ostr.str().c_str(), value);

}

void api2(const char* info, int value) {
cache[info] = value;
}
 
V

vsuresh.cs

I am writing my program in detail
std::map<string,int> cache;
void api1() {
  cin << key; // string
  cin << value; // int

Do you really have << operators on cin?
  ostringstream ostr;
  ostr << key;
  api2(ostr.str().c_str(), value);

void api2(const char* info, int value) {
  cache[info] = value;
}

Sorry Ben,

its cin >> key;
cin >> value;

+suresh
 
E

Erik Wikström

Hi Sam/Erick,

I am writing my program in detail

No you are not, please post a minimal but compilable and functional
program that demonstrates your problem.
std::map<string,int> cache;

void api1() {

cin << key; // string
cin << value; // int
ostringstream ostr;
ostr << key;

This does not make sense, if key is a string why use a stringstream to
convert it to a string?
api2(ostr.str().c_str(), value);

}

void api2(const char* info, int value) {
cache[info] = value;
}

Same here, since cache is of type map<string, int> why are you messing
around with char*? Just pas key as a string if that is what you want.
 
Z

Zeppe

Sorry Ben,

its cin >> key;
cin >> value;

so far, your biggest mistake is not to write a very small program that
compiles and that presents the error. For me, your mistake can be
anywhere else in parts of the program that are not shown.

Best wishes,

Zeppe
 
V

vsuresh.cs

Hi Zeppe,

I agree i did not want to create a stand alone program that gives the
error.

In simple words, is it advisable to pass ostringstream as a char* to
another interface ?

Thanks,
Suresh
 
V

vsuresh.cs

Then how do you expect people to help?
Hi Ian,

my subject is "passing on ostringstream" and its not related to cin/
cout or syntax error. But people are pointing to the syntax error and
other optimizations that can be done on my program. As i am new to
this group, i dont know that i want to write 100% error free code :
( will writing sample pgms.

will be clearer from next post.

Can i know the reason why its not advisable ?

+suresh
 
I

Ian Collins

Hi Ian,

my subject is "passing on ostringstream" and its not related to cin/
cout or syntax error. But people are pointing to the syntax error and
other optimizations that can be done on my program.

Part of the problem is the question was not clear. You are attempting
to pass the value of the contents of the stream as a const char* rather
than passing on the stream object.
Can i know the reason why its not advisable ?
As others have pointed out, you were passing a pointer to a temporary
object which will be destroyed when it goes out of scope. Erick also
pointed out the solution.
 
E

Erik Wikström

Hi Ian,

my subject is "passing on ostringstream" and its not related to cin/
cout or syntax error.

But none of your problems or questions were about passing an ostringstream.
But people are pointing to the syntax error and
other optimizations that can be done on my program.

I have not seen any advice about optimisations, but I have seen (and
given) some about good design and writing good code. While it is
possible to write error-free code in a bad way, it is much easier to
reduce the errors if the code quality is high.
As i am new to
this group, i dont know that i want to write 100% error free code :
( will writing sample pgms.

Since you are new to C++ and it is a complex language it might be hard
for you to figure out what the errors are, and (again since it is a
complex language) it might be hard for us too if we can not see all the
code. We do not require you to post 100% error free code, but we will
point out the errors we find, in fact we do not even require that you
post compiling code in some instances, but we do want you to post the
actual code you are having problems with.
Can i know the reason why its not advisable ?

It is never advisable to us char* when you want strings unless you
really have to, use std::string instead whenever possible. Especially as
in your case what you really want is a string (since that is what you
have in you map) and not a char*.
 

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
473,968
Messages
2,570,152
Members
46,697
Latest member
AugustNabo

Latest Threads

Top