Advertisement
ZiGGi

parse digit

Nov 8th, 2015
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.87 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. int main () {
  8.     string line, element;
  9.     ifstream myfile("input.txt");
  10.     size_t old_index, new_index;
  11.     bool is_digit;
  12.  
  13.     if (myfile.is_open()) {
  14.         while ( getline(myfile, line) ) {
  15.             new_index = -1;
  16.  
  17.             do {
  18.                 old_index = new_index + 1;
  19.                 new_index = line.find_first_of(" ", new_index + 1);
  20.  
  21.                 element = line.substr(old_index, new_index - old_index);
  22.  
  23.                 is_digit = true;
  24.                 for (int i = element.length(); i != 0; i--) {
  25.                     if (!isdigit(element[i])) {
  26.                         is_digit = false;
  27.                     }
  28.                 }
  29.  
  30.                 if (is_digit) {
  31.                     cout << "'" << element << "' is digit" << endl;
  32.                 } else {
  33.                     cout << "'" << element << "' is not digit" << endl;
  34.                 }
  35.             } while (new_index != string::npos);
  36.         }
  37.  
  38.         myfile.close();
  39.     } else {
  40.         cout << "Unable to open file";
  41.     }
  42.  
  43.     return 0;
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement