Advertisement
193030

Working reading string to Numbers, custom stoi function

Mar 31st, 2020
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.86 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <string.h>
  4. #include <cstdlib>
  5. #include <sstream>      // std::stringstream
  6.  
  7. using namespace std;
  8.  
  9.  
  10.  
  11. string input = " ";
  12.  
  13.  
  14. long sstoi(const char *s) // custom stoi function
  15. {
  16.     long i;
  17.     i = 0;
  18.     while(*s >= '0' && *s <= '9')
  19.     {
  20.         i = i * 10 + (*s - '0');
  21.         s++;
  22.     }
  23.     return i;
  24. }
  25. int main() {
  26.  
  27.     int k = 0;
  28.     cin >> k;
  29.     string newString[k] = {};
  30.     long long numbers[k] = {};
  31.     int output[k] = {};
  32.     cin.sync();
  33.  
  34.     for(int i =0; i<k; i++)
  35.     {
  36.  
  37.         getline(cin, input);
  38.         newString[i] = input.erase(input.size() - 1);
  39.         //numbers [i] = atoi(newString.c_str()); // S tova ne raboti
  40.          numbers[i] = sstoi (input.c_str());
  41.  
  42.  
  43.  
  44.     }
  45.  
  46.     for( int i =0; i<k; i++)
  47.     {
  48.  
  49.         cout << numbers[i] << endl;
  50.  
  51.     }
  52.  
  53.  
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement