Advertisement
kidino

C++ Array College Assignment

Jan 9th, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.48 KB | None | 0 0
  1. #include <iostream>;
  2. #include <fstream>;
  3. #include <string>;
  4. #include <regex>;
  5.  
  6. using namespace std;
  7.  
  8. int main() {
  9.  
  10.     string line;
  11.     string s;
  12.     ifstream myfile;
  13.     ofstream myfile2;
  14.     string data[2][2];
  15.     vector<string> str_result;
  16.     int a = 0;
  17.     int b = 0;
  18.  
  19.     myfile.open("D:\\Dev\\Projects\\Project1\\assignment2.txt");
  20.     if (myfile.is_open())
  21.     {
  22.         // read file
  23.         cout << "Reading from File" << endl;
  24.         while (getline(myfile, line))
  25.         {
  26.             cout << line << '\n';
  27.         }
  28.         myfile.close();
  29.  
  30.         // tokening with regex
  31.         regex reg("\\s+");
  32.         sregex_token_iterator iter(line.begin(), line.end(), reg, -1);
  33.         sregex_token_iterator end;
  34.  
  35.         vector<string> vec(iter, end);
  36.  
  37.         // assigning value to array
  38.         for (auto v : vec)
  39.         {
  40.             data[a][b] = v;
  41.             b++;
  42.             if (b > 1) {
  43.                 b = 0;
  44.                 a++;
  45.             }
  46.         }
  47.  
  48.         // first display
  49.         cout << endl;
  50.         cout << "First Display" << endl;
  51.         for (int x = 0; x < 2; x++) {
  52.             for (int y = 0; y < 2; y++) {
  53.                 cout << data[x][y] << ' ';
  54.                 if (y == 1) {
  55.                     cout << endl;
  56.                 }
  57.             }
  58.         }
  59.  
  60.         myfile2.open("D:\\Dev\\Projects\\Project1\\swap.txt");
  61.  
  62.         // second display
  63.         cout << endl;
  64.         cout << "Second Display" << endl;
  65.         for (int x = 0; x < 2; x++) {
  66.             for (int y = 1; y >= 0; y--) {
  67.                 cout << data[x][y] << ' ';
  68.                 myfile2 << data[x][y] << ' ';
  69.                 if (y == 0) {
  70.                     cout << endl;
  71.                     myfile2 << endl;
  72.  
  73.                 }
  74.             }
  75.         }
  76.  
  77.         myfile2.close();
  78.  
  79.     } else {
  80.         cout << "Unable to open file\n";
  81.     }
  82.  
  83.     cin.get();
  84.  
  85.     return 0;
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement