Create a list of numbers

Discussion in 'C++' started by Eric Lilja, Oct 27, 2004.

  1. Eric Lilja

    Eric Lilja Guest

    Hello, I'm working on my C++ homework and I have a question about the
    following function.
    It's part of my code the handles command line arguments. Here's the code:

    static void
    extract_numbers(const char* optarg, vector<string>& numbers)
    {
    const char* str = optarg;

    while(true)
    {
    string s(str);
    string::size_type index = s.find_first_of(',');

    if(index == string::npos)
    {
    string temp(s.c_str(), s.length());

    numbers.push_back(temp);

    break;
    }
    else
    {
    string temp(s.c_str(), index);

    numbers.push_back(temp);

    str += (index + 1);
    }
    }
    }

    optarg points to a null-terminated string of the following form:
    somenumber1,somenumber2,somenumber3
    where somenumber* is a, *drumroll*, number. It needs to extract these
    numbers and put them in the vector. Note that
    optarg may point to anything, I haven't made any sanity checks on the input
    at this point and it may also
    contain only one number. My way seems to work for "friendly" input, is it ok
    to hand in as it is (I need to perform better on
    this assignment) or can it be improved/bug fixed?

    Thanks for any help!

    / Eric
    Eric Lilja, Oct 27, 2004
    #1
    1. Advertising

  2. Eric Lilja wrote:
    > Hello, I'm working on my C++ homework and I have a question about the
    > following function.
    > It's part of my code the handles command line arguments. Here's the code:
    >
    > static void
    > extract_numbers(const char* optarg, vector<string>& numbers)
    > {
    > const char* str = optarg;
    >
    > while(true)
    > {
    > string s(str);
    > string::size_type index = s.find_first_of(',');
    >
    > if(index == string::npos)
    > {
    > string temp(s.c_str(), s.length());
    >
    > numbers.push_back(temp);
    >
    > break;
    > }
    > else
    > {
    > string temp(s.c_str(), index);
    >
    > numbers.push_back(temp);
    >
    > str += (index + 1);
    > }
    > }
    > }
    >
    > optarg points to a null-terminated string of the following form:
    > somenumber1,somenumber2,somenumber3
    > where somenumber* is a, *drumroll*, number. It needs to extract these
    > numbers and put them in the vector. Note that
    > optarg may point to anything, I haven't made any sanity checks on the input
    > at this point and it may also
    > contain only one number. My way seems to work for "friendly" input, is it ok
    > to hand in as it is (I need to perform better on
    > this assignment) or can it be improved/bug fixed?


    Try it with different inputs, like

    ,something,somethingelse
    or
    ,,something
    or
    something,,
    or
    something,,,lastthing

    If you get it all there, you're fine.

    Since it's only part of the program, you are still in need to fix the
    conversion later and detect any errors during that. It would be nice
    if your program while detecting the error in the input knew where the
    erroneous input is in the original string. If you have spare time, you
    could work on that.

    V
    Victor Bazarov, Oct 27, 2004
    #2
    1. Advertising

  3. Eric Lilja

    Michael Kurz Guest

    "Eric Lilja" <> schrieb im Newsbeitrag
    news:cloact$cvk$...
    > Hello, I'm working on my C++ homework and I have a question about the
    > following function.
    > It's part of my code the handles command line arguments. Here's the code:
    >
    > static void
    > extract_numbers(const char* optarg, vector<string>& numbers)
    > {
    > const char* str = optarg;
    >
    > while(true)
    > {
    > string s(str);
    > string::size_type index = s.find_first_of(',');
    >
    > if(index == string::npos)
    > {
    > string temp(s.c_str(), s.length());
    >
    > numbers.push_back(temp);
    >
    > break;
    > }
    > else
    > {
    > string temp(s.c_str(), index);
    >
    > numbers.push_back(temp);
    >
    > str += (index + 1);
    > }
    > }
    > }
    >



    Remark:
    I would create a class, which is able to parse a string and seperate it into
    parts.
    As IMHO this task is needed very very often. Something like the split()
    function in perl. We inhouse have a class named LineParser, which does this.
    Whenever you want to split up strings with some seperation character using
    such a spliiter class is much easier and with less probability for errors,
    if the class is working properly once.


    Regards
    Michael
    Michael Kurz, Oct 27, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Subra
    Replies:
    25
    Views:
    1,185
    user923005
    Mar 8, 2007
  2. Andrew Tatum

    Fibonacci Numbers and Lucas Numbers

    Andrew Tatum, May 26, 2007, in forum: C++
    Replies:
    6
    Views:
    555
    Howard
    May 27, 2007
  3. Lance Hoffmeyer
    Replies:
    2
    Views:
    520
    Lance Hoffmeyer
    Jul 26, 2007
  4. jko170
    Replies:
    9
    Views:
    125
    jko170
    Jan 21, 2009
  5. GIMME
    Replies:
    5
    Views:
    185
    Thomas 'PointedEars' Lahn
    Jul 26, 2004
Loading...

Share This Page