To store a function pointer and object type for input.
Overloaded input manipulator for int type.
istream &imanip_name(istream &stream, type param) {
// body of code
return stream;
}
Overloaded output manipulator for int type.
ostream &omanip_name(ostream &stream, type param){
// body of code
return stream;
}
For other input/output types
smanip<type> mainip_name(type param) {
return smanip<type> (manip_name, param);
}
Use an overloaded manipulator to provide special and unique input handling characteristics for your class.
Returns a pointer to stream object.
#include <iostream> #include <cstring> #include <cstdlib> #include <cctype> char buffer[80]; char *Password = "CodeWarrior"; struct verify { explicit verify(char* check) : check_(check) {} char* check_; }; char *StrUpr(char * str); std::istream& operator >> (std::istream& stream, const verify& v); int main() { using namespace std; cin >> verify(StrUpr(Password)); cout << "Log in was Completed ! \n"; return 0; } std::istream& operator >> (std::istream& stream, const verify& v) { using namespace std; short attempts = 3; do { cout << "Enter password: "; stream >> buffer; StrUpr(buffer); if (! strcmp(v.check_, buffer)) return stream; cout << "\a\a"; attempts--; } while(attempts > 0); cout << "All Tries failed \n"; exit(1); return stream; } char *StrUpr(char * str) { char *p = str; // dupe string while(*p) *p++ = static_cast<char>(std::toupper(*p)); return str; }
Result:
Enter password: <codewarrior> Enter password: <mw> Enter password: <CodeWarrior> Log in was Completed !