String to Int converstion using <sstream>

This is a discussion on String to Int converstion using <sstream> within the C++ Programming forums, part of the General Programming Boards category; Dev-C++ debugger stops at this section o' code.. I am wondering if my attempt at using the istringstream object is ...

  1. #1
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903

    Question String to Int converstion using <sstream>

    Dev-C++ debugger stops at this section o' code.. I am wondering if my attempt at using the istringstream object is anywhere close to correct.. and maybe a few pointers on how to use it correctly:

    Code:
    int tempint = 0,
        qnty = 0;             
         
         for(int i=0, end=lotarray.size(); i<end; i++)
         { 
            istringstream myStream(lotarray[i].numbers);  //lotarray[i].numbers is a string that would look like this:  "9,13,4,11,45,27"      
            
            for(int j=0; j<5; j++)
            {
               myStream >> tempint;
               game_nos.push_back(tempint);
            }
            
            myStream >> tempint;
            pballs.push_back(tempint);  
         }
    the game object:
    Code:
    struct game
    {
        string date;
        string numbers;
    
       friend istream& operator>> (istream& ins,  game& target);   
       friend ostream& operator<< (ostream& outs, game& target);
    };
    the lotarray object:
    Code:
    vector<game> lotarray;

    complete code is available upon request.. my program compiles, so I don't have any compiler errors.. just seems to stop working when it reaches the above code.
    Last edited by The Brain; 01-20-2008 at 09:25 PM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Maybe if you separated your numbers with whitespace?
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Yes, I suspect the myStream >> tempint is waiting for more data, thus the hang. The first >> operation is reading the whole string into the stream. Use your debugger and watch it.

    You could use getline(myStream,tempint,',') to do this without changing the existing lotarray format.

    Also, it appears there are 6 numbers, not 5, so your j loop is dropping one off.

    Todd

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    Quote Originally Posted by Todd Burch View Post
    Also, it appears there are 6 numbers, not 5, so your j loop is dropping one off.

    Todd
    There is additional read after the loop
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Quote Originally Posted by vart View Post
    There is additional read after the loop
    Oops - yep. And yuk.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to combine these working parts??
    By transgalactic2 in forum C Programming
    Replies: 0
    Last Post: 02-01-2009, 07:19 AM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. My graphics library
    By stupid_mutt in forum C Programming
    Replies: 3
    Last Post: 11-26-2001, 05:05 PM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 04:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21