Click here to Skip to main content
15,894,539 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
my program only allow userdata from "userdatabase.txt" to enter when ask for login and password. but now I anyhow type also can login.
can anyone tell me where is the problem.

C++
void authUser()        // Function that prompts for user to login and matches input with userdatabase.txt
{
    string accountID = "";        // initialize temp accountID to null
    string password = "";        // initialize temp password to null
    int invalidCount = 0;

    char c = ' ';

    bool validUser = false;
    bool failedLogin = false;

    while (validUser != true) // to check for invalid login
    {
        system("clear");
        using std :: cout;
        using std :: endl;
        void Encrypt( char [ ] ); // prototypes of functions used in the code
        void Decrypt( char * ePtr );

        cout << logo_MainMenu << endl;
        if (failedLogin == true)                // Displays error before asking for username/password
        {
            if (invalidCount == 1)              // tests for invalid login, 1 / 3 chance used up
            {
                cout << "\E[1;32mInvalid Account ID / Password. Please try again!\E[0m" << endl;
                cout << "You have used up 1 / 3 chances to login." << endl;
                cout << "If you fail to login after 3 tries, the application will lock down and exit!" << endl;
            }
            else if (invalidCount == 2)         // tests for invalid login, 2 / 3 chance used up
            {
                cout << "\E[1;32mInvalid Account ID / Password. Please try again!\E[0m" << endl;
                cout << "You have used up 2 / 3 chances to login." << endl;
                cout << "If you fail to login after 3 tries, the application will lock down and exit!" << endl;
            }
            else                                // tests for invalid login, 3 / 3 chance used up
            {
                cout << "\E[1;32mInvalid Account ID / Password.\E[0m" << endl;
                cout << "You have used up 3 / 3 chances to login." << endl << endl;
                cout << "The application will now lock down and exit!" << endl;
                exit (0);                       // exit application
            }
        }
        cout << endl << "To login, please enter your Account ID and Password" << endl;
        cout << "(type \"exit\" to terminate the application)" << endl << endl;
        cout << "Account ID: ";
        getline (cin, accountID);

        if (accountID == "exit")                // user can exit anytime
            exit (0);
        // by typing "exit" at username or password
        if (password == "exit")
            exit (0);

        char clear[200] = {0};
        char cipher[200] = {0};  // trying to to encrypting and decrypt 
        char filename[80];
        int x,i;
        ofstream outputFile;
        outputFile.open("yxy.txt"); // the text file that my encrypted password goes into

        ifstream userDatabaseIN("userdatabase.txt", ios::in); // the text file that userdata is stored at

        cout << "Enter Password:" ;   // asking for password
        cin.getline(clear,sizeof(clear));

        x = strlen(clear);
        // Encryption
        for(i=0; i<=x-1; i++)
        {
            if (isspace(clear[i]))
            {
                cipher[i] = clear[i];
            }
            {
                cipher[i] = clear[i]+3;
            }
        }

        // Decryption
        for(i=0; i<=x-1; i++)
        {
            if (isspace(cipher[i]))
            {
                clear[i] = cipher[i];
            }
            else
            {
                clear[i] = cipher[i]-3;
            }
        }


        outputFile<<" Encrypted:" << cipher << endl;
        outputFile<<" Decrypted:" << clear << endl;    //output 
        cout<<"Encrypted and Decrypted password transfer to yxy.txt\n"; 


        return ;
    }
Posted
Updated 23-Oct-13 22:45pm
v4
Comments
Richard MacCutchan 24-Oct-13 4:17am    
Your code does not seem to match your comments (much like your other questions). Please explain exactly what this code is trying to do and where it is going wrong.
Member 10334475 24-Oct-13 4:20am    
erm this code asks for username and password which its database is under "userdatabase.txt"
now, even if I enter invalid username and password it still can enter the system.
Richard MacCutchan 24-Oct-13 4:49am    
If you remove all the extraneous code you should be able to see that nowhere do you check the entered userid or password to see if they match anything in your input file. You also have a return statement inside your while loop so you break out at the end of the first loop. As I stated in my comment to your other question, you need to break your code down into simple manageable parts and test each part before adding new features. Get yourself a good book on writing C++ code and work through it.
Member 10334475 24-Oct-13 5:00am    
can show me how simple can this go? need to hand this up in 2 hours
Richard MacCutchan 24-Oct-13 5:02am    
I think you left it a bit late.

1 solution

Well, according to your code, everything what you've entered will be passed.
At first, this is wrong.
while (validUser != true)

In your code, there is no statement to set validUser = true so it always break out when you input password and userid at once.

Second, there is no matching statement and reflect matching result to failedLogin variable.
You only read password and encrypt it, but there is no matching statement so it's reflection result failedLogin is no longer usable.

It's my opinion.
 
Share this answer
 
Comments
WuRunZhe 26-Oct-13 2:10am    
Could accept solution with blue button.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900