Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- файл MyException.h
- #pragma once
- #include <iostream>
- #include <string>
- const int indent = 11;
- using namespace std;
- class MyException
- {
- protected:
- string message;
- public:
- MyException(string message = "ERR")
- {
- this->message = message;
- }
- virtual ~MyException() {}
- string getMessage() { return this->message; }
- };
- файл NumberException.h
- #pragma once
- #include "MyException.h"
- class NumberException :
- public MyException
- {
- public:
- NumberException(string msg = "") :MyException() { this->message += 'N'; this->message += msg; }
- ~NumberException() {}
- };
- int input_int(int low, int high);
- double input_double(double low, double high);
- файл NumberException.cpp
- #include "NumberException.h"
- int input_int(int low, int high)
- {
- {
- int number;
- bool flag = true;
- while (flag)
- {
- flag = 0;
- try
- {
- cin >> number;
- if (cin.peek() != '\n' || cin.fail())
- throw NumberException("132: use only integer numbers!");
- if (number < low || number > high)
- throw NumberException("133: wrong number");
- }
- catch (MyException &a)
- {
- cout << a.getMessage() << endl;
- flag = 1;
- rewind(stdin);
- cin.clear();
- }
- }
- return number;
- }
- }
- double input_double(double low, double high)
- {
- double number;
- bool flag = true;
- while (flag)
- {
- flag = 0;
- try
- {
- cin >> number;
- if (cin.peek() != '\n' || cin.fail())
- throw NumberException("134: use only double numbers!");
- if (number < low || number > high)
- throw NumberException("135: wrong number");
- }
- catch (MyException &a)
- {
- cout << a.getMessage() << endl;
- flag = 1;
- rewind(stdin);
- cin.clear();
- }
- }
- return number;
- }
- файл StringException.h
- #pragma once
- #include "MyException.h"
- class StringException :
- public MyException
- {
- public:
- StringException(string msg = "") { this->message += 'S'; this->message += msg; }
- ~StringException() {}
- };
- string input_string_wihout_numbers(int size = indent - 1);
- string input_string(int size = indent - 1);
- файл StringException.cpp
- #include "StringException.h"
- string input_string_wihout_numbers(int size)
- {
- string str;
- bool flag = true;
- while (flag)
- {
- flag = 0;
- try
- {
- cin >> str;
- if (str.size() > size)
- throw StringException("123: too big string");
- for (int i = 0; i < str.size(); i++)
- if (str[i] >= 'a' && str[i] <= 'z' || str[i] >= 'A' && str[i] <= 'Z' || str[i] == '-');
- else throw StringException("124: use only english letters!");
- }
- catch (StringException &a)
- {
- cout << a.getMessage() << endl;
- flag = 1;
- rewind(stdin);
- cin.clear();
- }
- }
- return str;
- }
- string input_string(int size)
- {
- string str;
- bool flag = true;
- while (flag)
- {
- flag = 0;
- try
- {
- cin >> str;
- if (str.size() > size)
- throw StringException("125: too big string");
- }
- catch (StringException&a)
- {
- cout << a.getMessage() << endl;
- flag = 1;
- rewind(stdin);
- cin.clear();
- }
- }
- return str;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement