473,394 Members | 1,831 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Create a list of numbers

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
Jul 22 '05 #1
2 2726
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
Jul 22 '05 #2

"Eric Lilja" <er*******************@yahoo.com> schrieb im Newsbeitrag
news:cl**********@news.island.liu.se...
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
Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: massimo | last post by:
Hey, I wrote this program which should take the numbers entered and sort them out. It doesnąt matter what order, if decreasing or increasing. I guess I'm confused in the sorting part. Anyone...
19
by: Eduardo Bezerra | last post by:
Hi, I'm looking for an efficient way to create the oposite of a list of numbers. For example, suppose I have this list of numbers: 100 200 300
7
by: Wolfgang Kreuzer | last post by:
Hello all, I have two tables - Projects and ProjectStruct Table Projects contains master records of the projects, ProjectStruct allows to define a project herarchie and contains the fields...
24
by: flkeyman | last post by:
Work in legal office. Trying to create solid designed database structure. This is list of tables w/fields and primary keys. Any comments/advice greatly appreciated. tbl-Defendants CaseNumber...
4
by: tron.thomas | last post by:
The following code: numbers = for value in numbers: value *= 2 print numbers results in the following output:
3
by: Kasrav | last post by:
hey guys it me again i got stuck in the middle of this program. The program starts with a list declaration of these numbers , i have to make sure the program outputs the following 1 The number...
1
by: yaarnick | last post by:
Create a linked list data structure library to hold strings. Then library should support the following linked list operations. Create() – Creates the linked list Insert() – Insert a string into the...
1
by: jazon | last post by:
Let me start by saying this for an Operating Systems class. No, I don't expect the work to be done for me. The assignment is as follows: To be honest, I feel like a fish out of water, like...
1
by: vekka | last post by:
Hi! Programming language: C I wondered if any of you are able to see what is wrong with my code. What I want to do is to find the union of a set of numbers. I use a singly linked list. and my union...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.