Guest User

Untitled

a guest
Apr 20th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.41 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <string>
  4. #include <fstream>
  5.  
  6.  
  7. using namespace std;
  8.  
  9. class Exception{
  10. public:
  11. Exception();
  12. Exception(string message);
  13. string getmessage();
  14. private:
  15. string message;
  16.  
  17. };
  18.  
  19. class RuntimeException : public Exception {
  20. public:
  21. RuntimeException();
  22. RuntimeException(string themessage);
  23. string getmessage();
  24.  
  25. private:
  26. string message;
  27. };
  28.  
  29. class NumberFormatException : public RuntimeException {
  30. public:
  31. NumberFormatException();
  32. NumberFormatException(string themessage);
  33. string getmessage();
  34. private:
  35. string message;
  36. };
  37.  
  38. int toInt(string message) throw(Exception);
  39.  
  40. int main()
  41. {
  42. ifstream input;
  43. ofstream output;
  44. input.open("textfile.txt");
  45. if(input.fail())
  46. {
  47. cout<<"Input failed brah.\n";
  48. exit(1);
  49. }
  50. string numbs;
  51. do{
  52. input >> numbs;
  53. cout << "The string that has been read is the following : ";
  54. cout << numbs << endl << endl;
  55.  
  56.  
  57. try{
  58. cout << toInt(numbs);
  59. }
  60. catch(NumberFormatException e){
  61. cout << e.getmessage() << endl << endl;
  62. }
  63. catch(RuntimeException e) {
  64. cout << e.getmessage() << endl << endl;
  65. }
  66. catch(Exception e){
  67. cout << e.getmessage() << endl << endl;
  68. }
  69. }while(input>>numbs);
  70.  
  71.  
  72. return (0);
  73.  
  74.  
  75. }
  76.  
  77.  
  78. Exception::Exception(){
  79.  
  80. message = "";
  81.  
  82. }
  83.  
  84. Exception::Exception(string themessage){
  85.  
  86. message = themessage;
  87.  
  88.  
  89.  
  90. }
  91.  
  92. int toInt(string message) throw(Exception){
  93. int fixedmess[20];
  94. int size;
  95.  
  96. size = message.length();
  97. if(message.length() == 0)
  98. throw(Exception("The string is empty"));
  99. for(int i = 0; i <= size; i++){
  100. fixedmess[i] = message[i];
  101.  
  102. if(i == 11)
  103. throw(RuntimeException("This string is more than 10 digits long"));
  104.  
  105. if((fixedmess[i] < 48)||(fixedmess[i] > 57)){
  106. throw (NumberFormatException("There seems to be a character which is not a digit "));
  107. }
  108. cout << fixedmess[i] << endl;
  109.  
  110.  
  111. }
  112. return 0;
  113.  
  114. }
  115.  
  116. string Exception::getmessage(){
  117.  
  118. return(message);
  119. }
  120.  
  121. RuntimeException::RuntimeException(){
  122. message = "";
  123. }
  124.  
  125. RuntimeException::RuntimeException(string themessage){
  126. message = themessage;
  127. }
  128.  
  129. string RuntimeException::getmessage(){
  130.  
  131. return(message);
  132. }
  133.  
  134. NumberFormatException::NumberFormatException(){
  135.  
  136. message = "";
  137.  
  138. }
  139.  
  140. NumberFormatException::NumberFormatException(string themessage){
  141.  
  142. message = themessage;
  143. }
  144.  
  145. string NumberFormatException::getmessage(){
  146.  
  147. return(message);
  148. }
Add Comment
Please, Sign In to add comment