実現したいこと
現在try throw catchとクラスを用いてパスワード認識のプログラムをC++で作っています。
下記のようにエラーが発生します。
私のコードのどこがおかしいか教えていただけますでしょうか?
発生している問題・エラーメッセージ
Enter the password: buvuTGB3547& Your password is not valid. libc++abi.dylib: terminating with uncaught exception of type double Process finished with exit code 6
該当のソースコード
C++
1#include <iostream> 2#include <cstring> 3 4using namespace std; 5 6class PasswordError{ 7private: 8 bool aUpper = false, 9 aLower = false, 10 aDigit = false, 11 aPunct = false; 12 13public: 14 bool testNum(char pass[]) { 15 16 for (int i = 0; pass[i]; ++i) 17 if (isupper(pass[i])) 18 aUpper = true; 19 else if (islower(pass[i])) 20 aLower = true; 21 else if (isdigit(pass[i])) 22 aDigit = true; 23 else if (!ispunct(pass[i])) 24 aPunct = true; 25 if (aUpper && aLower && aDigit && aPunct) 26 return true; 27 else 28 return false; 29 } 30 31 void exception() { 32 try { 33 if (!aUpper) 34 throw 2; 35 } 36 37 catch (int a) { 38 cout << "Your password should contain at least one upper case" << endl; 39 } 40 41 try { 42 if (!aLower) 43 throw 4.5; 44 } 45 46 catch (double b) { 47 cout << "Your password should contain at least one lower case" << endl; 48 } 49 50 try { 51 if (!aDigit) 52 throw 'c'; 53 } 54 55 catch (char c) { 56 cout << "Your password should contain at least one numeric digit" << endl; 57 } 58 59 try { 60 if (!aPunct) 61 throw 1.5f; 62 } 63 64 catch (float d) { 65 cout << "Your password should contain at least one of these special characters: !@#$%&*:;" << endl; 66 } 67 } 68}; 69 70int main() { 71 PasswordError passw; 72 73 const int SIZE = 100; 74 char password[SIZE]; 75 76 cout << "Enter the password: " << endl; 77 cin.getline(password, SIZE); 78 79 80 try { 81 if (strlen(password) < 8) { 82 throw 4; 83 } 84 } 85 86 catch (int i) { 87 cout << " Your password must be at least 8 characters long." << endl; 88 } 89 90 91 if (passw.testNum(password)) 92 cout << "Your password is valid." << endl; 93 else { 94 cout << "Your password is not valid. " << endl; 95 passw.exception(); 96 97 return 0; 98 } 99}
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/11/09 09:05
2018/11/09 09:08
2018/11/09 09:24
2018/11/09 09:30
2018/11/09 09:54