Vlearns
asked on
atoi question
Hi
I am using atoi function in the the code and want to handle tis particular error case.
any garbage input to atoi will result in 0 getting returned which is a success code.
Is there a function i can use that returns an error code for garbage in case, or throw an exception.
I can use STL probably not use boost, but still would like to know what the options are with
either.
thanks
I am using atoi function in the the code and want to handle tis particular error case.
any garbage input to atoi will result in 0 getting returned which is a success code.
Is there a function i can use that returns an error code for garbage in case, or throw an exception.
I can use STL probably not use boost, but still would like to know what the options are with
either.
thanks
ASKER
Thanks for the responses.
here is the exact problem i am trying to solve
here is the string
response = (int)atoi(responsestring.c _str());
The responsestring is suppossed to be numerical codes like
1111
1222
3333
except in one case
where it is 4444\r\nurl=<redirect URL>.
this is what my code does
response = (int)atoi(responsestring.c _str());
idx = responsestring.find("url=" );
if (response == User::ERR && idx != string::npos)
{
custom_error = "Please verify " + responsestring.substr(idx + strlen("url="));
}
here is the exact problem i am trying to solve
here is the string
response = (int)atoi(responsestring.c
The responsestring is suppossed to be numerical codes like
1111
1222
3333
except in one case
where it is 4444\r\nurl=<redirect URL>.
this is what my code does
response = (int)atoi(responsestring.c
idx = responsestring.find("url="
if (response == User::ERR && idx != string::npos)
{
custom_error = "Please verify " + responsestring.substr(idx + strlen("url="));
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
what happens if we get garbage?
ASKER
more specifically
what is the difference between 4444\r\nurl=<redirect URL>.
and 2222hellogarbage
garbage22222
0garbage8888
what is the difference between 4444\r\nurl=<redirect URL>.
and 2222hellogarbage
garbage22222
0garbage8888
You mean *only* garbage as input? That condition could be detected like
response = (int)strtol(responsestring.c_str(),&pCnvEnd,10);
if (responsestring.c_str() == pCnvEnd) {
// nothing numeric at all
}
.
2222hellogarbage - results in 2222 being returned and pCnvEnd pointing to hellogarbage (offset 4 to string start)
garbage22222 - results in 0 being returned and pCnvEnd pointing to garbage (offset 0 to string start)
0garbage8888 - results in 0 being returned and pCnvEnd pointing to garbage (offset 1 to string start)
garbage22222 - results in 0 being returned and pCnvEnd pointing to garbage (offset 0 to string start)
0garbage8888 - results in 0 being returned and pCnvEnd pointing to garbage (offset 1 to string start)
if the string always (and trustable) begins with a number, atoi is sufficient. it would stop converting digits at the fist non-digit (periods and minus excluded). the strtol is necessary if you want refuse bad user-input.
Sara
Sara
what do you want to do with
4444\r\nurl=<redirect URL>.
?
what do you want to do with
2222hellogarbage
or
garbage22222
or
0garbage8888
or whatever else you consider to be garbage?
4444\r\nurl=<redirect URL>.
?
what do you want to do with
2222hellogarbage
or
garbage22222
or
0garbage8888
or whatever else you consider to be garbage?
Open in new window
Or you could use a std::stringstream like
Open in new window