CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    May 2009
    Posts
    3

    Retrieving numbers from an string.

    Basically i have started learning c++. My situation is that i have an array of strings that are read from a text file, which contains lines of text which are correctly formatted.

    for eg:

    AC/DC,Dirty Deeds Done Dirt Cheap,303,
    Alison Moyet,All Cried Out,410,
    Allan Browne Quintet,Cyclosporin,291,


    Now i put these in an array as i read them from the file. I want to read the numbers in the strings, these numbers are the lengths of the songs and i need to find the total length of all the songs in the file.

    My prob is finding a way to get just the numbers from the string array index. Basically just the 303 from the 1st string, 410 from the 2nd string and 291 from the 3rd string. If i can do just one i can just throw it in a loop and find the total length.

    I am looking for any suggestions, (i hope this is in the right forum section)
    Thanks.

  2. #2
    Join Date
    Apr 2009
    Location
    Netherlands
    Posts
    91

    Re: Retrieving numbers from an string.

    Best thing to do is to write out the algorithm in psuedocode first. Then use a textbook, online documentation or google to find the neccesary c++ functions to change the psuedocode into c++.

  3. #3
    Join Date
    Feb 2009
    Location
    India
    Posts
    444

    Re: Retrieving numbers from an string.

    You can use strtok or one of its variants to do this.
    Convert every 3rd field to numeric using atoi.
    «_Superman
    I love work. It gives me something to do between weekends.

    Microsoft MVP (Visual C++)

  4. #4
    Join Date
    May 2009
    Posts
    3

    Re: Retrieving numbers from an string.

    Thanx for the info i got it working. Now i have another problem may be unusual.

    Code:

    for(int k = 0; k <= numOfSongs; k++)
    {
    found = allText[k].find_first_of("0123456789");
    strTime = allText[k].substr(found);
    time = atoi(strTime.c_str());

    totalTime += time;
    }

    I have that for loop now where the k condition is (k <= numOfSongs) i get a funny error in the console:

    Error:

    This application has requested the Runtime to terminate it in an unusual way.
    Please contact the application's support team for more information.


    BUT when i change the condition to k < numOfSongs (without the equals) it works, but i miss out on the last array.

    Any1 can shed any light on this?

    Thanks.

  5. #5
    Join Date
    Feb 2009
    Location
    India
    Posts
    444

    Re: Retrieving numbers from an string.

    If a string is declared as string str[100], then its valid indexes are from 0 to 99. So the loop for(int k = 0; k < numOfSongs; k++) is correct without the = sign. You missing the last index could be some other problem.

    You the debugger to track it down.
    «_Superman
    I love work. It gives me something to do between weekends.

    Microsoft MVP (Visual C++)

  6. #6
    Join Date
    Nov 2006
    Location
    Australia
    Posts
    1,569

    Re: Retrieving numbers from an string.

    Are you sure you're missing the last element in the array?
    Good judgment is gained from experience. Experience is gained from bad judgment.
    Cosy Little Game | SDL | GM script | VLD | Syntax Hlt | Can you help me with my homework assignment?

  7. #7
    Join Date
    May 2009
    Posts
    3

    Re: Retrieving numbers from an string.

    Thanks for your help guys i found the problem, couldn't of done it without u guys!

    Thanks again.

  8. #8
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: Retrieving numbers from an string.

    Here's another alternative technique.

    Code:
    #include <iostream>
    #include <sstream>
    #include <string>
    #include <algorithm>
    
    using namespace std;
    
    int main()
    {
        // Demo string.
        string text("AC/DC,Dirty Deeds Done Dirt Cheap,303,");
    
        // Replace commas with newlines.
        replace(text.begin(), text.end(), ',', '\n');
    
        // Initialise a string stream.
        istringstream iss(text.c_str());
    
        string band;
        string title;
        string temp;
        int time;
    
        // Read the values.
        getline(iss, band);
        getline(iss, title);
        getline(iss, temp);
        time = atoi(temp.c_str());
    }
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  9. #9
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: Retrieving numbers from an string.

    If you want to go totally C++, replace the itoa with

    Code:
    istringstream(temp.c_str()) >> time;
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured