Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 1st, 2005, 3:59 PM   #1
Ancient Dragon
PFO God In Training
 
Ancient Dragon's Avatar
 
Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 549
Rep Power: 4 Ancient Dragon is on a distinguished road
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.
Ancient Dragon is offline   Reply With Quote
Old Jun 1st, 2005, 5:30 PM   #2
jubitzu
Newbie
 
Join Date: May 2005
Location: Colorful Colorado
Posts: 25
Rep Power: 0 jubitzu is on a distinguished road
s.str("");

that should do it...
jubitzu is offline   Reply With Quote
Old Jun 1st, 2005, 6:33 PM   #3
Ancient Dragon
PFO God In Training
 
Ancient Dragon's Avatar
 
Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 549
Rep Power: 4 Ancient Dragon is on a distinguished road
stringstream <solved>

Thanks a lot. That solved the proglem.
Ancient Dragon is offline   Reply With Quote
Old Jun 3rd, 2005, 5:49 AM   #4
jubitzu
Newbie
 
Join Date: May 2005
Location: Colorful Colorado
Posts: 25
Rep Power: 0 jubitzu is on a distinguished road
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.
jubitzu is offline   Reply With Quote
Old Jun 3rd, 2005, 9:25 AM   #5
Ancient Dragon
PFO God In Training
 
Ancient Dragon's Avatar
 
Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 549
Rep Power: 4 Ancient Dragon is on a distinguished road
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.
Ancient Dragon is offline   Reply With Quote
Old Jun 3rd, 2005, 9:31 AM   #6
mitakeet
Programmer
 
mitakeet's Avatar
 
Join Date: Jun 2005
Location: Maryland, USA
Posts: 59
Rep Power: 4 mitakeet is on a distinguished road
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
mitakeet is offline   Reply With Quote
Old Jun 3rd, 2005, 9:36 AM   #7
Ancient Dragon
PFO God In Training
 
Ancient Dragon's Avatar
 
Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 549
Rep Power: 4 Ancient Dragon is on a distinguished road
Quote:
Originally Posted by mitakeet
What are you trying to did anyway? Just convert an int to a string?
remove any decimals that may be in the string and format it to a fixed-width string for output to a lazer printer.

Example:

input strijng: "123.0"
output string to 5 places: " 123"
Ancient Dragon is offline   Reply With Quote
Old Jun 3rd, 2005, 9:40 AM   #8
mitakeet
Programmer
 
mitakeet's Avatar
 
Join Date: Jun 2005
Location: Maryland, USA
Posts: 59
Rep Power: 4 mitakeet is on a distinguished road
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
mitakeet is offline   Reply With Quote
Old Jun 3rd, 2005, 9:46 AM   #9
Animatronic
Programmer
 
Join Date: Jun 2005
Posts: 99
Rep Power: 4 Animatronic is on a distinguished road
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();
}
Animatronic is offline   Reply With Quote
Old Jun 3rd, 2005, 9:51 AM   #10
Ancient Dragon
PFO God In Training
 
Ancient Dragon's Avatar
 
Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 549
Rep Power: 4 Ancient Dragon is on a distinguished road
Quote:
Originally Posted by Animatronic
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.:
Good idea, but it doesn't work either.
Ancient Dragon is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:09 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC