Advertisement
maxim_shlyahtin

str to int

Nov 24th, 2021
929
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.65 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <utility>
  4. #include <string>
  5. #include <fstream>
  6.  
  7. using namespace std;
  8.  
  9.  
  10. int main()
  11. {  
  12.     vector <int> a;
  13.     vector <string> v;
  14.     string path = "noice.txt";
  15.     ifstream fin;
  16.     fin.open(path);
  17.  
  18.     if (!fin.is_open())
  19.     {
  20.         cout << "error" << endl;
  21.     }
  22.     else
  23.     {
  24.         string str;
  25.         while (!fin.eof())
  26.         {
  27.             getline(fin, str);
  28.             v.push_back(str);
  29.         }
  30.     }
  31.     fin.close();
  32.     for (int i = 0; i < (int)v.size() - 1; i++)
  33.     {  
  34.         try {
  35.             int res = stoi(v[i]);
  36.             a.push_back(res);
  37.         }
  38.         catch (std::invalid_argument e) {
  39.             cout << "caught invalid argument exception\n";
  40.             cout << i;
  41.         }
  42.     }
  43.     return 0;
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement