c++ - getting a string to work in password function -
hi apologies poor explanation i'm new this. working on password function i'm having problems. have set account name string "john" , account password int 1111. password works fine string causing error. when change "const string name = "john"" random integer code works fine.
i hoping spot i'm going wrong?
bool login() { const int password = 1111; int passwordattempt; const string name = "john"; string nameattempt; int attempts = 0; std::cout << "please enter password:" << std::endl; //first attempt @ account number & password std::cin >> passwordattempt; std::cout << "enter name:"<< std::endl; std::cin >> nameattempt; if (passwordattempt == password && nameattempt == name) { return true; } else while (passwordattempt!=password || nameattempt!=name) { if(attempts++ ==2)//.. loop 2 more attempts { std::cout << "you have entered wrong details 3 times. application terminated now" << std::endl; return false; break; } std::cout<<"incorrect password. try again"<<std::endl; std::cout<< "" <<std::endl; std::cout << "please enter password:" << std::endl; std::cin >> passwordattempt; std::cout << "enter name:"<< std::endl; std::cin >>nameattempt; } } using namespace std; int main() { bool loggedin = login(); if (loggedin){ cout << "logged in" << endl; } else if (!loggedin){ cout << "not logged" << endl; } system("pause"); return 0; }
- add
std::
string declarations (so havestd::string
). - after while loop there's end of function
login
without value return. addreturn true
there. build warnings enabled!
Comments
Post a Comment