Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //--------------
- //----user.h----
- //--------------
- #ifndef USERNAME_H
- #define USERNAME_H
- #include <iostream>
- #include <string>
- using namespace std;
- namespace authenticate
- {
- void inputUserName();
- string getUserName();
- }
- #endif
- //--------------
- //---user.cpp---
- //--------------
- #include <iostream>
- #include <string>
- using namespace std;
- namespace
- {
- string username;
- //Intent:判斷帳號是否符合規則
- //Pre:無
- //Post:如果符合則回傳true, 否則回傳false
- bool isValid()
- {
- //判斷帳號長度是否恰好為8個字且皆為letter
- if (username.length() == 8)
- {
- for (int i = 0; i < username.length(); i++)
- {
- if (!isalpha(username[i])) return false;
- }
- return true;
- }
- return false;
- }
- }
- namespace authenticate
- {
- //Intent:供使用者輸入帳號並且將使用者的帳號儲存起來
- //Pre:無
- //Post:無
- void inputUserName()
- {
- //供使用者輸入帳號
- do
- {
- cout << "Enter your username " <<
- "(8 letters only)" <<
- endl;
- cin >> username;
- } while (!isValid());
- }
- //Intent:得到使用者的帳號
- //Pre:無
- //Post:回傳使用者的帳號
- string getUserName()
- {
- return username;
- }
- }
- //------------------
- //----password.h----
- //------------------
- #ifndef PASSWORD_H
- #define PASSWORD_H
- #include <iostream>
- #include <string>
- using namespace std;
- namespace authenticate
- {
- void inputPassword();
- string getPassword();
- }
- #endif
- //------------------
- //---password.cpp---
- //------------------
- #include <iostream>
- #include <string>
- #include <cctype>
- using namespace std;
- namespace
- {
- string password;
- //Intent:判斷密碼是否符合規則
- //Pre:無
- //Post:如果符合則回傳true, 否則回傳false
- bool isValid()
- {
- //判斷密碼長度是否8個字以上且至少包含一個non-letter的字
- if (password.length() >= 8)
- {
- for (int i = 0; i < password.length(); i++)
- {
- if (!isalpha(password[i])) return true;
- }
- }
- return false;
- }
- }
- namespace authenticate
- {
- //Intent:供使用者輸入密碼並且將使用者的密碼儲存起來
- //Pre:無
- //Post:無
- void inputPassword()
- {
- //供使用者輸入密碼
- do
- {
- cout << "Enter your password (at least 8" <<
- " characters and at least one non-letter)" <<
- endl;
- cin >> password;
- } while (!isValid());
- }
- //Intent:得到使用者的密碼
- //Pre:無
- //Post:回傳使用者的密碼
- string getPassword()
- {
- return password;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment