0

How can I fix this code?

this is the code: #include <iostream> using namespace std; class myClass { public: myClass(string nm) { setName(nm); } void setName(string x) { name = x; } string getName() { return name; } private: string name; }; int main() { myClass ob1("David"); myClass ob2("Amy"); int a; cout << "enter your password"; cin >> a; if( a == 33){ cout << "ok, enter another one"; cin >> a; cout << ob1.getName(); } else{ cout << "oh well"; } } 1. How do I fix the if statement? 2. How can I make it so that if a certain digit is not entered, this( cout << "enter your password"; cin >> a;) will not be output onto the screen. For example, if the person was suppised to enter 5, but entered 6, how can I erase cout << "enter your password"; cin >> a; so that it wont be output?

28th Jul 2018, 4:12 AM
Coder333
3 Réponses
0
try with while loop and setstate.... something like below: cout << "Enter password" << endl; cin >> a; while(a!=5) { cout.setstate(ios_base::failbit); cout << "Invalid password... Renter" <<endl; cout.clear(); cin >> a; } Cout<< "Successful login" << endl; This might help...
28th Jul 2018, 7:32 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 1
fail bit is set when there is logical error in I/o... We are forcing to ignore output till the time it is cleared.. for more information on setstate : http://www.cplusplus.com/reference/ios/ios/setstate/
29th Jul 2018, 4:05 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
@Ketan Lalcheta What does this part mean? cout.setstate(ios_base::failbit);
28th Jul 2018, 9:57 PM
Coder333