Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <string>
- #include <fstream>
- using namespace std;
- class Exception{
- public:
- Exception();
- Exception(string message);
- string getmessage();
- private:
- string message;
- };
- class RuntimeException : public Exception {
- public:
- RuntimeException();
- RuntimeException(string themessage);
- string getmessage();
- private:
- string message;
- };
- class NumberFormatException : public RuntimeException {
- public:
- NumberFormatException();
- NumberFormatException(string themessage);
- string getmessage();
- private:
- string message;
- };
- int toInt(string message) throw(Exception);
- int main()
- {
- ifstream input;
- ofstream output;
- input.open("textfile.txt");
- if(input.fail())
- {
- cout<<"Input failed brah.\n";
- exit(1);
- }
- string numbs;
- do{
- input >> numbs;
- cout << "The string that has been read is the following : ";
- cout << numbs << endl << endl;
- try{
- cout << toInt(numbs);
- }
- catch(NumberFormatException e){
- cout << e.getmessage() << endl << endl;
- }
- catch(RuntimeException e) {
- cout << e.getmessage() << endl << endl;
- }
- catch(Exception e){
- cout << e.getmessage() << endl << endl;
- }
- }while(input>>numbs);
- return (0);
- }
- Exception::Exception(){
- message = "";
- }
- Exception::Exception(string themessage){
- message = themessage;
- }
- int toInt(string message) throw(Exception){
- int fixedmess[20];
- int size;
- size = message.length();
- if(message.length() == 0)
- throw(Exception("The string is empty"));
- for(int i = 0; i <= size; i++){
- fixedmess[i] = message[i];
- if(i == 11)
- throw(RuntimeException("This string is more than 10 digits long"));
- if((fixedmess[i] < 48)||(fixedmess[i] > 57)){
- throw (NumberFormatException("There seems to be a character which is not a digit "));
- }
- cout << fixedmess[i] << endl;
- }
- return 0;
- }
- string Exception::getmessage(){
- return(message);
- }
- RuntimeException::RuntimeException(){
- message = "";
- }
- RuntimeException::RuntimeException(string themessage){
- message = themessage;
- }
- string RuntimeException::getmessage(){
- return(message);
- }
- NumberFormatException::NumberFormatException(){
- message = "";
- }
- NumberFormatException::NumberFormatException(string themessage){
- message = themessage;
- }
- string NumberFormatException::getmessage(){
- return(message);
- }
Add Comment
Please, Sign In to add comment