Created
May 7, 2015 10:48
-
-
Save RIscRIpt/1290e092ac9e5d1f1fa5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
#include <conio.h> | |
using namespace std; | |
template<typename T> | |
class password { | |
public: | |
template<typename T> | |
friend ostream& operator<<(ostream &os, password<T> &in); | |
friend istream& operator>>(istream &is, password<T> &out); | |
protected: | |
T value; | |
}; | |
template<typename T> | |
ostream& operator<<(ostream &os, password<T> &in) { | |
return os << in.value; | |
} | |
istream& operator>>(istream &is, password<int> &out) { | |
string str; | |
while(true) { | |
int c = getch(); | |
if(c >= '0' && c <= '9') { | |
str.push_back(c); | |
cout << '*'; | |
} else if(c == '\b') { | |
if(str.length() > 0) { | |
str.pop_back(); | |
cout << "\b \b"; | |
} | |
} else if(c == '\r') { | |
cout << endl; | |
break; | |
} | |
} | |
out.value = atoi(str.c_str()); | |
return is; | |
} | |
istream& operator>>(istream &is, password<string> &out) { | |
out.value.clear(); | |
while(true) { | |
int c = getch(); | |
if(c >= ' ' && c <= '~') { | |
out.value.push_back(c); | |
cout << '*'; | |
} else if(c == '\b') { | |
if(out.value.length() > 0) { | |
out.value.pop_back(); | |
cout << "\b \b"; | |
} | |
} else if(c == '\r') { | |
cout << endl; | |
break; | |
} | |
} | |
return is; | |
} | |
int main() { | |
password<int> pw_int; | |
password<string> pw_str; | |
cout << "Enter integer password: "; | |
cin >> pw_int; | |
cout << "Enter string password: "; | |
cin >> pw_str; | |
cout << "pw_int = " << pw_int << endl | |
<< "pw_str = " << pw_str << endl; | |
getch(); | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment