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 2 2656
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
"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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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.
...
|
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...
|
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...
|
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...
|
by: tron.thomas |
last post by:
The following code:
numbers =
for value in numbers:
value *= 2
print numbers
results in the following output:
|
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...
|
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...
|
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:
...
|
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...
|
by: tammygombez |
last post by:
Hey fellow JavaFX developers,
I'm currently working on a project that involves using a ComboBox in JavaFX, and I've run into a bit of an issue....
|
by: tammygombez |
last post by:
Hey everyone!
I've been researching gaming laptops lately, and I must say, they can get pretty expensive. However, I've come across some great...
|
by: teenabhardwaj |
last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
|
by: CD Tom |
last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
|
by: CD Tom |
last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
|
by: Naresh1 |
last post by:
What is WebLogic Admin Training?
WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
|
by: jalbright99669 |
last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
|
by: antdb |
last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine
In the overall architecture, a new "hyper-convergence" concept was...
|
by: Matthew3360 |
last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function.
Here is my code.
...
| |