Advertisement
irmantas_radavicius

Untitled

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