alien_fx_fiend

isNumber with auto

Oct 9th, 2020
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2.  
  3. bool toNumberConv(std::string str, auto &x)
  4. {
  5.     int dots = 0;
  6.     bool num = true;
  7.     unsigned int i = 0;
  8.     while(i < str.length() && num)
  9.     {
  10.         if ((str[i] == '.') && dots == 0)
  11.         {
  12.             dots = 1;
  13.         }
  14.         else if (!isdigit(str[i]))
  15.         {
  16.             num = false;          
  17.         }
  18.         i++;
  19.     }
  20.     if(num && dots == 0)
  21.     {
  22.         x = stoll(str);
  23.         if(x <= 2147483647)
  24.         {
  25.             x = stoi(str);
  26.             std::cout << "Ez egy int\n";
  27.         }
  28.         else
  29.         {
  30.             std::cout << "Ez egy long long\n";
  31.         }
  32.     }
  33.     else if(num && dots == 1)
  34.     {
  35.         x = stod(str);
  36.         std::cout << "Ez egy double\n";
  37.     }
  38.     else
  39.     {
  40.         x = -1;
  41.         std::cout << "Nem sikerult a konvertalas\n";
  42.     }
  43.     return num;
  44. }
  45. /*
  46. int             stoi        -2,147,483,648 to 2,147,483,647
  47. long long int   stoll       -(2^63) to (2^63)-1
  48. double          stod
  49. */
  50. int main()
  51. {
  52.     std::string str;
  53.     std::cin >> str;
  54.    
  55.     long double x;
  56.    
  57.     bool isNum = toNumberConv(str, x);
  58.    
  59.     if(isNum) std::cout << x << std::endl;
  60.     else std::cout << "Ez nem egy szam\n";
  61.    
  62.     return 0;
  63. }
Add Comment
Please, Sign In to add comment