Programming Forums
User Name Password Register
 

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

Reply
 
Thread Tools Display Modes
Old Feb 24th, 2007, 2:37 PM   #1
Mcoy
Newbie
 
Mcoy's Avatar
 
Join Date: Mar 2006
Posts: 20
Rep Power: 0 Mcoy is on a distinguished road
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?
Mcoy is offline   Reply With Quote
Old Feb 24th, 2007, 3:51 PM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Feb 24th, 2007, 4:33 PM   #3
Mcoy
Newbie
 
Mcoy's Avatar
 
Join Date: Mar 2006
Posts: 20
Rep Power: 0 Mcoy is on a distinguished road
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.
Mcoy is offline   Reply With Quote
Old Feb 24th, 2007, 6:03 PM   #4
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Feb 24th, 2007, 9:23 PM   #5
Mcoy
Newbie
 
Mcoy's Avatar
 
Join Date: Mar 2006
Posts: 20
Rep Power: 0 Mcoy is on a distinguished road
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.
Mcoy is offline   Reply With Quote
Old Feb 24th, 2007, 9:38 PM   #6
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Feb 26th, 2007, 5:44 AM   #7
jubitzu
Newbie
 
Join Date: May 2005
Location: Colorful Colorado
Posts: 25
Rep Power: 0 jubitzu is on a distinguished road
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.
jubitzu 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

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




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

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