1atoi(str) is unsafe
2
3This is the prefered method:
4(int)strtol(str, (char **)NULL, 10)
1int isNumber(char s[])
2{
3 for (int i = 0; s[i]!= '\0'; i++)
4 {
5 if (isdigit(s[i]) == 0)
6 return 0;
7 }
8 return 1;
9}
10
1#include<string>
2string str1 = "45";
3string str2 = "3.14159";
4string str3 = "31337 geek";
5
6int myint1 = stoi(str1);
7std::cout<<stoi(str1);
1const char *number = "10";
2char *end;
3long int value = strtol(number, &end, 10);
4if (end == number || *end != '\0' || errno == ERANGE)
5 printf("Not a number");
6else
7 printf("Value: %ld", value);