Advertisement
drankinatty

Reading & Checking Arrays C++

Feb 25th, 2020
353
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.62 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. #define NELEM 6     /* constant for correct number of elements */
  5.  
  6. int main (void) {
  7.    
  8.     int i, last = 0;                        /* int read and last seen */
  9.     std::vector<std::vector<int>> v {};     /* vector of vector<int> */
  10.     std::vector<int> tmp {};                /* temporary vector<int> to fill */
  11.    
  12.     while (std::cin >> i) {                 /* read each integer in file */
  13.         last = i;                           /* save last i to test on loop exit */
  14.         if (i == -1) {                      /* if -1 array ended with last int */
  15.             if (tmp.size() < NELEM)         /* is size less than NELEM? */
  16.                 std::cerr << "\nthe array does not have enough integers.\n";
  17.             else if (tmp.size() > NELEM)    /* otherwise if greater than NELEM */
  18.                 std::cerr << "\nyou have too many numbers in this array.\n";
  19.             else {
  20.                 v.push_back(tmp);           /* add tmp to vector<vector<int>> */
  21.                 std::cout << "\nthe size of the array is correct.\n";
  22.             }
  23.             tmp.clear();                    /* clear temporary vector<int> */
  24.             continue;
  25.         }
  26.         std::cout << " " << i;              /* output element */
  27.         if (i < 0) {                        /* check if number is negative */
  28.             std::cerr << "\nthis array has negative element.\n";
  29.             while (std::cin >> i && (last = i) != -1) {}/* read/discard rest */
  30.             tmp.clear();                    /* clear temp array */
  31.             continue;
  32.         }
  33.         tmp.push_back(i);                   /* add int to temporary vector */
  34.     }
  35.     if (last != -1)                         /* validate last read is -1 */
  36.         std::cerr << "error: last input not -1.\n";
  37.    
  38.     std::cout << "\ncorrect arrays:\n";     /* output good arrays */
  39.     for (auto r : v) {                      /* loop over good vectors */
  40.         for (auto c : r)                    /* loop over integers */
  41.             std::cout << " " << c;          /* output values */
  42.         std::cout << '\n';                  /* tidy up with newline */
  43.     }
  44. }
  45.  
  46. /*
  47.  
  48.     echo "1 2 3 4 5 6 -1 1 2 3 4 -1 1 2 -3 4 5 6 7 8 -1 1 2 3 4 5 6 7 -1 2 3 4 5 6 7 -1" | ./bin/readmultiarray
  49.      1 2 3 4 5 6
  50.     the size of the array is correct.
  51.      1 2 3 4
  52.     the array does not have enough integers.
  53.      1 2 -3
  54.     this array has negative element.
  55.      1 2 3 4 5 6 7
  56.     you have too many numbers in this array.
  57.      2 3 4 5 6 7
  58.     the size of the array is correct.
  59.    
  60.     correct arrays:
  61.      1 2 3 4 5 6
  62.      2 3 4 5 6 7
  63.  
  64.  
  65. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement