I'm doing a problem on HR and cant figure out how to check for error without using conditional statements. How is this done in C++?
// if string is int output it, else output "Bad string"
// need to do this without any loops/conditionals
int main(){
string S;
char *end;
long x;
cin >> S;
const char *cstr = S.c_str();
x = strtol(cstr,&end,10);
if (*end == '\0')
cout << x;
else
cout << "Bad string";
return 0;
}
Should I be using something besides strtol?