Default User said:
Then you need to be more specific about what you already have,
The original code looked like the following unholy mess (which I did
not write):
// unsigned int typedef'ed as uint
// assume appropriate #includes
char sTemp[64];
uint uDeptTime, uArrvTime;
if( argc>=2 ) {
if( sameas(argv[1], "") ) { // sameas ~ strcmp() with flavor
// error
}
strncpy( sTemp, argv[1], sizeof(sTemp) );
cp=strchr(sTemp, '.');
if( cp == NULL ) {
// error
}
uint const lene=strlen(cp);
uint const lenb=strlen(sTemp);
uint const lenr=lenb-lene;
sTemp[lenr]='\0';
uDeptTime=(uint)atoi(sTemp);
if( cp+1 ) {
uArrvTime=(uint)atoi(cp+1);
}
}
I wrote the following as a first approximation to a decent solution:
char *cp;
vector<uint> v;
char sTemp[64];
uint uDeptTime, uArrvTime, uMaxGT; // new variable
for( cp=argv[1] ; (cp=strchr(cp,'.')) != NULL ; ) {
v.push_back( atoi(cp++) ); // atoi() wraps the "standard" atoi, but
// I don't know the details
}
if( v.size() < 3 ) {
// error
}
uDeptTime=v[0];
uArrvTime=v[1];
uMaxGT=v[2];
and what your requirements are.
Straightline code only. (no additional class declarations)
Do you object to converting the C-style strings to std::strings or
std::stringstreams?
I'd love to use std::strings and/or std::stringstreams if they offer a
cleaner (not necessarily more "efficient") solution.