Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   How to reset a stringstream object? (http://www.programmingforums.org/showthread.php?t=12655)

Mcoy Feb 24th, 2007 3:37 PM

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?

DaWei Feb 24th, 2007 4:51 PM

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.

Mcoy Feb 24th, 2007 5:33 PM

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.

DaWei Feb 24th, 2007 7:03 PM

Unfortunately, the value of a concept is sometimes -858993460 ;).

Mcoy Feb 24th, 2007 10:23 PM

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.

DaWei Feb 24th, 2007 10:38 PM

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.

jubitzu Feb 26th, 2007 6:44 AM

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