Advertisement
irmantas_radavicius

Untitled

Mar 7th, 2024
539
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.45 KB | None | 0 0
  1. // Validation functions for the course of OOP'2024
  2. // (c) irmantas.radavicius@mif.vu.lt
  3. // 2025-03-07 first draft
  4.  
  5. #include <iostream>
  6. #include <fstream>
  7. #include <sstream>
  8. #include <cctype>
  9. #include <vector>
  10.  
  11. using namespace std;
  12.  
  13. // gets valid input (single value per line) from cin
  14. // msgInput: message to ask for data
  15. // msgError: additional details after invalid input
  16. // returns a value of type T
  17. template <typename T>
  18. T getValue(const string &msgInput, const string &msgError = ""){
  19.     T x;                            // value to be returned
  20.     while(1){                       // loops until input becomes valid
  21.         cout << msgInput;
  22.         cin >> x;                   // try to read value
  23.         char c = cin.get();         // and one char after
  24.         if(cin.fail() || (c != '\n'))
  25.         {   // if reading failed or additional chars found
  26.             cout << "Bad input! " << msgError << endl;
  27.             cin.clear();            // clear error flags in cin
  28.             cin.ignore(1024, '\n'); // clean input buffer
  29.         } else {
  30.             break;                  // input is valid
  31.         }
  32.     }
  33.     return x;
  34. }
  35.  
  36.  
  37. // gets valid input (single value per line) from cin
  38. // msgInput: message to ask for data
  39. // msgError: additional details after invalid input
  40. // returns a value of type T
  41. template <typename T>
  42. vector<T> getList(const string &msgInput, const string &msgError = ""){
  43.     vector<T> v;                    // list to be returned
  44.     while(1){                       // this loops until input becomes valid
  45.         string line;
  46.         cout << "Enter a list of numbers: ";
  47.         getline(cin, line);         // read the whole line
  48.         stringstream ss;            // now process it internally
  49.         ss << line;
  50.         int hasErrors = 0;          // is zero for valid input
  51.         while(1){                   // process every value in the line
  52.             T x;
  53.             ss >> x;
  54.             if(!ss.fail()){         // if the value is valid
  55.                 v.push_back(x);     // add it to the list
  56.             } else {                // we failed
  57.                 if(!ss.eof())       // check the reason why
  58.                     hasErrors = 1;  // garbage stopped us and not the end
  59.                 break;              // finish processing current line
  60.             }
  61.         }
  62.         if(hasErrors == 0)
  63.             break;                  // the line is valid
  64.         else {                      // there was garbage
  65.             cout << "Bad input!" << msgError << endl;
  66.             v.clear();              // clear error flags in cin
  67.         }
  68.     }
  69.     return v;
  70. }
  71.  
  72. // prints the values in a single line
  73. template <typename T>
  74. void printList(vector<T> v){
  75.     for(int i = 0; i < v.size(); i++)
  76.         cout << v[i] << " ";
  77.     cout << endl;
  78. }
  79.  
  80. int main(){
  81.  
  82.     // compute the sum of two numbers - example on how to use getValue
  83.     int x = getValue<int>("Enter first number: ", "Integer value required.");
  84.     int y = getValue<int>("Enter second number: ", "Integer value required.");
  85.     int z = x + y;
  86.     cout << "The sum is " << z << endl << endl << endl;
  87.  
  88.     // compute the sum of all values in a list - example of how to use getList
  89.     vector<int> values = getList<int>("Enter the list of integers: ");
  90.     printList<int>(values);
  91.     int sum = 0;
  92.     for(int i = 0; i < values.size(); ++i){
  93.         sum += values[i];
  94.     }
  95.     cout << "The sum is " << sum << endl;
  96.  
  97.     return 0;
  98.  
  99. }
  100.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement