![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
PFO God In Training
![]() Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 549
Rep Power: 4
![]() |
stringstream [solved]
How do you delete what's in a stringstream so that is can be used again for something else? I've googled for stringstream and saw a few suggestions, but none of them work. For example
string FormatNumber(string& str)
{
int n;
stringstream s(str);
s >> n;
s << setfill(' ') << setw(5) << n;
return s.str();
}In the above, if " 123.45" is passed to FormatNumber(), the result will be " 123123.45", obviously not what I want. I want the result to be " 123". But how to do this without using a second stringstream object? Thanks for any suggestions. Last edited by Ancient Dragon; Jun 1st, 2005 at 6:36 PM. |
|
|
|
|
|
#2 |
|
Newbie
Join Date: May 2005
Location: Colorful Colorado
Posts: 25
Rep Power: 0
![]() |
s.str("");that should do it...
__________________
How to ask questions the smart way |
|
|
|
|
|
#3 |
|
PFO God In Training
![]() Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 549
Rep Power: 4
![]() |
stringstream <solved>
Thanks a lot. That solved the proglem.
![]() |
|
|
|
|
|
#4 |
|
Newbie
Join Date: May 2005
Location: Colorful Colorado
Posts: 25
Rep Power: 0
![]() |
I want to recommend a book to you: C++ in a Nutshell ~$40
It has proved invaluable in my programming work and will answer almost any question you could ever conceive about C++ and the STL.
__________________
How to ask questions the smart way |
|
|
|
|
|
#5 |
|
PFO God In Training
![]() Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 549
Rep Power: 4
![]() |
stringstream (not finished afterall)
This function is not consistent -- when the input string contains a decimal value (such as "123.45" the function works as expected. But if the input string does not contain a decimal (e.g. "123") the result streamstring object is "". Can anyone explain that behavior? I use VC++ 6.0, and used its debugger to step through the code. The value of n is correct in all cases. Its only the final line that is failing.
string FormatNumber(string& str, int width)
// Converts str to an integer (truncating any deaimals that
// it might contain, then reformats the int to (width)-digit string,
// right justified and left-padded with spaces.
//
// For example, the input string str might be " 123.45". The
// final result of this function will be " 123".
{
// static char iobuf[40];
// sprintf(iobuf,"%*d",width,atoi(str.c_str()));
// return iobuf;
int n = 0;
stringstream s(str);
s >> n;
s.str("");
s << setfill(' ') << setw(width) << n;
#if defined(_DEBUG)
cout << "s.str() = '" << s.str() << "'";
if(s.str() == "")
cout << "\tBAD";
cout << endl;
#endif
return s.str();
}This is really a simple problem in C -- and NEVER fails. Of course the danger here is that a buffer overflow may occur for large values of width. But since I'm in total control of the program, that will never happen. That stringstream class really sucks :mad: string FormatNumber(string& str, int width)
{
static char iobuf[20];
sprintf(iobuf,"%*d",width, atoi(str.c_str());
return iobuf;
}Last edited by Ancient Dragon; Jun 3rd, 2005 at 9:31 AM. |
|
|
|
|
|
#6 |
|
Programmer
Join Date: Jun 2005
Location: Maryland, USA
Posts: 59
Rep Power: 4
![]() |
What are you trying to did anyway? Just convert an int to a string?
__________________
Free code: http://sol-biotech.com/code/. It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it. --Mitakeet The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man. --George Bernard Shaw |
|
|
|
|
|
#7 | |
|
PFO God In Training
![]() Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 549
Rep Power: 4
![]() |
Quote:
Example: input strijng: "123.0" output string to 5 places: " 123" |
|
|
|
|
|
|
#8 |
|
Programmer
Join Date: Jun 2005
Location: Maryland, USA
Posts: 59
Rep Power: 4
![]() |
I have never developed a fondness for C++ io, so I would probably do exactly what you are doing in your FormatNumber routine, sorry.
__________________
Free code: http://sol-biotech.com/code/. It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it. --Mitakeet The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man. --George Bernard Shaw |
|
|
|
|
|
#9 |
|
Programmer
Join Date: Jun 2005
Posts: 99
Rep Power: 4
![]() |
The stringstream is failing because you are reaching the end of the stream and then trying to use it again. After you take out an int the stream is empty so the badbit is set and any operations on the stream after that will fail.
You can use setstate() to change the current state of the stream back to good, e.g.: string FormatNumber(string const & str, int width)
// Converts str to an integer (truncating any deaimals that
// it might contain, then reformats the int to (width)-digit string,
// right justified and left-padded with spaces.
//
// For example, the input string str might be " 123.45". The
// final result of this function will be " 123".
{
// static char iobuf[40];
// sprintf(iobuf,"%*d",width,atoi(str.c_str()));
// return iobuf;
int n = 0;
stringstream s(str);
s >> n;
s.str("");
s.setstate( stringstream::goodbit );
s << setfill(' ') << setw(width) << n;
#if defined(_DEBUG)
cout << "s.str() = '" << s.str() << "'";
if(s.str() == "")
cout << "\tBAD";
cout << endl;
#endif
return s.str();
} |
|
|
|
|
|
#10 | |
|
PFO God In Training
![]() Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 549
Rep Power: 4
![]() |
Quote:
|
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|