Thread: istringstream error

  1. #1
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244

    istringstream error

    What is wrong with my code? The program does not print the second phrase!
    Code:
    #include <iostream>
    #include <sstream>
    
    int main(){
        string buffer(¨This_is_number: 56¨);
        string anotherBuffer(¨Initial content¨);
        istringstream input(buffer);
    
        input >> buffer;
        cout << buffer << endl;
    
        int number;
        input >> number;
        cout << number << endl;
    
        input.str(¨Another_string¨);
        input >> anotherBuffer;
    
        cout << anotherBuffer << endl;
    
        return 0;
    }
    Thanks for any help.
    Nothing more to tell about me...
    Happy day =)

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Apparently the stream doesn't like the end of the string directly after the number and goes into fail state.
    string buffer("This_is_number: 56\n");
    makes it work.

    BTW, where do those freaky quotes come from?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >What is wrong with my code?
    You read input into an EOF state, you cannot read from the stream again until you clear the eof flag:
    Code:
    #include <iostream>
    #include <sstream>
    
    using namespace std;
    
    int main(){
        string buffer("This_is_number: 56");
        string anotherBuffer("Initial content");
        istringstream input(buffer);
    
        input >> buffer;
        cout << buffer << endl;
    
        int number;
        input >> number;
        cout << number << endl;
    
        input.clear();
        input.str("Another_string");
        input >> anotherBuffer;
    
        cout << anotherBuffer << endl;
    
        return 0;
    }
    My best code is written with the delete key.

  4. #4
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244
    Thanks everyone!!! I was trying to use ignore, but didn't work.

    CornedBee, I'm not sure what quotes are, but if you are talking about the ¨ in my strings, this is what happens when you use netscape with Linux :P.

    Again, thanks to both of you.
    Last edited by gustavosserra; 12-02-2003 at 09:00 AM.
    Nothing more to tell about me...
    Happy day =)

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    The eof, of course. Stupid me...
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  3. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM