![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Mar 2006
Posts: 20
Rep Power: 0
![]() |
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>
#include <string>
#include <sstream>
#include <stdlib.h>
using namespace std;
int main()
{
int cor_1[2];
int cor_2[2];
string input;
string temp;
stringstream StrToInt;
getline(cin, input);
StrToInt << input[0];
StrToInt >> cor_1[0];
StrToInt << input[2];
StrToInt >> cor_1[1];
StrToInt << input[4];
StrToInt >> cor_2[0];
StrToInt << input[6];
StrToInt >> cor_1[1];
cout << cor_1[0] << cor_1[1] << cor_2[0] << cor_2[1] << "\n";
system("PAUSE");
return 0;
}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? |
|
|
|
|
|
#2 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Mar 2006
Posts: 20
Rep Power: 0
![]() |
I forgot to mention that I had tried StrToInt.clear(), then StrToInt.str("") after it was read. It did not change the result
. 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. |
|
|
|
|
|
#4 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Unfortunately, the value of a concept is sometimes -858993460
.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#5 |
|
Newbie
Join Date: Mar 2006
Posts: 20
Rep Power: 0
![]() |
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. |
|
|
|
|
|
#6 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#7 |
|
Newbie
Join Date: May 2005
Location: Colorful Colorado
Posts: 25
Rep Power: 0
![]() |
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.
__________________
How to ask questions the smart way |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Level Editor | frankish | Show Off Your Open Source Projects | 47 | Jul 10th, 2006 6:57 PM |
| SMTPlib - Connection reset by peer | Sane | Python | 2 | Jun 20th, 2006 5:40 PM |
| Library problem creating Direct3d Object | Kilo | C++ | 9 | May 30th, 2006 8:48 AM |
| Object Pascal Q: object instance as parameter | KodeKid | Delphi | 1 | May 16th, 2006 12:06 PM |
| stringstream | Ancient Dragon | C++ | 12 | Jun 3rd, 2005 4:50 PM |