![]() |
How to reset a stringstream object?
I am trying to write code that will take a string of 4 numbers separated by 3 spaces and put them into two integer arrays representing the x and y values of two coordinates. ie:
INPUT: 0 0 4 7 coordinate_1[X] = 0 coordinate_1[Y] = 0 coordinate_2[X] = 4 coordinate_2[Y] = 7 Here is the code: :
#include <iostream>I figured that I could add x to the stream, read x from the stream, add y to the stream, read y from the stream etc. The output for the program is incorrect though, the only correct value ends up being cor_1[0]. All the others seem to be random numbers of great length. What is it about the above code or my understanding of stringstreams that is wrong? |
Your first transaction exausts the stream and sets the eof bit. That's a sort of fail condition. The stream won't work again for you until you clear that. Here are two good suggestions: 1) read the documentation for the things you use; 2) always test for success when you use those things. Failure is not an uncommon occurrence, particularly when a user is involved.
|
I forgot to mention that I had tried StrToInt.clear(), then StrToInt.str("") after it was read. It did not change the result :confused: .
I am not very experienced in c++ and many guides on the STL are confusing. Unfortunately I have a programming competition this Tuesday and I have to focus on concepts, not semantics, although I recognize that normally its good to focus on both. I'll keep looking for some info, and as always thanks for the quick reply DaWei. |
Unfortunately, the value of a concept is sometimes -858993460 ;).
|
I tried messing around with the code again and it turns out using StrToInt.clear() works after all. Must have been a typo somewhere in the code.
What do you mean by "documentation"? Is there something like unix 'man' pages for c++ libraries? The library, google and programmingfourms are the only "documentation" I use right now. |
Man pages are good. I sometimes use them. I also use MSDN on line, and have an ancient set of MSDN CDs tied into my help system. In particular, research the >> operator.
Realize that any function that is asked to convert user input to numeric values is wide open for trouble. %x*zh is hard to convert. Users may be inattentive, malicious, unknowing, or merely fall asleep with their forehead on the keyboard. Failing to test operations for success is the hallmark of a novice, at best. It could represent actionable negligence. |
As far as C++ documentation goes, the SGI Standard Template Library Programmer's Guide is about as good as I have found for free online. It is similar to unix man pages for each item in the library.
|
| All times are GMT -5. The time now is 1:59 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC