Login Class

+2 Aja Enyi · January 9, 2015
The Program runs but the password is always wrong... help?
#include<iostream>
#include<vector>
#include<string>
#include<conio.h>
#include<cstdlib>

using namespace std;

class password{
private:
string password;
public:
bool verifyPass(){
if(strcmp(password,"password")){
return false;
}
else
return true;
}
void getPass(){
char ch = _getch();
while(ch != 13){ //character 13 is enter
password.push_back(ch);
cout << "*";
ch = _getch();
}
}
int login(){
int trials=3;
getPass();
while(verifyPass()==0){
trials--;
cout<<"\aIncorrect Password!!!";
if(trials>0){
cout<<"\nYou have "<<trials<<" attempt(s) left\n";
cout<<"Enter password:  ";
getPass();
}
else{
System("CLS");
cout<<"\aLogin attempts exceeded!!!"
return 0;
}
}
}

};

int main(){
password session;
cout<<"Enter password:  ";
session.getPass();
if(verifyPass()==0){

}
return 0;
}

Post a Reply

Replies

Oldest  Newest  Rating
+1 c student · January 9, 2015
im not a leaner of c++ so the following information might be incorrect for your implementation, however, i have made it work as far as you have coded the program.  here is what i have analysed.

you have an incorrect string header file for strcmp.
#include <cstring>

despite fixing this, it will not work due to a conversion error from std::string to a const char* and might need to use str1.compare(str2), in your case, something of the likes of:
if (password.compare("password") == 0) { /* your code here */ }

your login function also might be missing a return type of int after the while loop
+1 Laura Lee · January 9, 2015
What c student pointed out is correct; however, In C++ string is a class with its own set of methods for handling manipulation, streams and comparisons. The cstring header would be the C++ implementation of C-style zero terminated strings. Generally when you are programming in C++ you should make full use of the language and features provided for you rather than resorting to functions like "strcmp".

You should be using the string class and for comparison, you should be using the compare method from the string class as c student stated. 

Aside from why your code isn't working, the design of your classes is very bad practice. The methods inside the class shouldn't be interacting with the user directly. Rather the user should be interacting with the main function, the main will then call methods of the class dependant on what the user wants. 

I wrote up a quick very basic login class just to show how it could be done alternatively to yours.
#include <iostream>
#include <string>
using std::getline;
using std::cin;
using std::cout;
using std::endl;
using std::string;



class Login
{
private:
    string ourPassword;
public:
    Login()
    {
        //Realistically we should load the password from a file
        //For now, we will just set our password
        ourPassword = "letmein";
    }
    ~Login()
    {
        //clean up any memory we allocated on the heap
    }
    bool verify(string testPass)
    {
        return ourPassword.compare(testPass);
    }
};

int main()
{
    Login login;
    string testPass;
    bool verified = 1;
    do
    {
        cout << "Enter a password:";
        getline(cin,testPass);
        if( (verified = login.verify(testPass)) == 1)
            cout << "Incorrect password!" << endl;
        else
            cout << "Correct password!" << endl << "Welcome." << endl;
    }
    while(verified == 1);
    return 0;
}


When you involve security measures, a login class shouldn't be done that way. This is just for the purpose of example.
  • 1

C++

106,932 followers
About

Used in many types of software including music players, video games, and many large scale applications.

Links
Moderators
Bucky Roberts Administrator