1

I am making a project in C++, in which I need to make a login system. I wrote the code below, but I am not able to figure out why the password doesn't match when we run case 2 in the login() function.

Core concept:
First, we need to register the user name and password, which is done in case 1 of the login() function. Then, the user gets access to the information through his username and password.

Problem faced:
When I try to use a username and password of an already-registered user, the password doesn't match. I checked the stored password and entered password by printing them to the console, and they are the same. But, when we compare both by using the == operator, it gives a false result. Can anyone explain why?

#include<iostream>
#include<fstream>
#include<cstdlib>
#include<conio.h>

using namespace std;

int login(void);

int main(void)
{
    cout<<"Login system\n";

    int d;
    d = login();

    if(d)
        cout<<"Access granted!!"<<endl;
    else
        cout<<"Access denied!!"<<endl;
}

int login(void)
{
    system("CLS");
    cout<<"\n\n\n";

    string username, password;

    fstream file;

    int choice;
    again:

    cout<<"1.Add new user\n2.login\n";
    cin>>choice;

    switch(choice)
    {
        case 1:
        {
            file.open("Login details.txt", ios :: app);
            cout<<"Enter new username and password\n";

            cin>>username;
            cin>>password;
            file<<username<<"\t\t";
            file<<password<<endl;
            return 1;
            break;
        }

        case 2:
        {
            file.open("Login details.txt", ios :: in);

            string tempname, temp_pass, k;
            char temp_pas[100];
            int p = 0;
            cout<<"Enter your username and password\n";
            cin>>tempname;
            do
            {
                temp_pas[p] = getch();
                if(temp_pas[p] != '\r')
                cout<<"*";
                p++;
            }while(temp_pas[p - 1] != '\r');
            
            cout<<endl;
            
            for(int i = 0; i < p; i++)
            {
                temp_pass += temp_pas[i];
            }

            while(file)
            {
                file>>username;
                file>>password;

                cout<<q<<endl<<w<<endl;
                if(tempname == username && temp_pass == password)
                {
                    cout<<"access granted\n";
                    return 1;
                }
            }
            cout<<"Invalid username or password\n";
            return 0;

            break;
        }
       default
             {
              cout<<"Choose a valid option"<<endl;
              goto again;
              }
    }
}
  • 1
    Your `do-while` loop doesn't seem to modify `p` at all? Why are you using a `char[]` at all, instead of `string`s? – cigien Jul 30 '20 at 17:15
  • Also note: [Why Should I Not #include ](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) – Geno C Jul 30 '20 at 17:18
  • Also, your `login` function doesn't return *anything* when the first `case` breaks out of the `switch`. You should enable compiler warnings and save yourself lots of time and trouble. – Adrian Mole Jul 30 '20 at 17:24
  • @cigien - Yep, he needs to increment `p`. He is using `getch()` so he can replace the input from the user with `*` – jiveturkey Jul 30 '20 at 17:25
  • While this is a great learning exercise, know actual passwords are NEVER stored. You create a hash from the password and the hash is stored so if your password file is compromised, the hacker doesn't get the actual passwords. To validate a login, you read the password entered, hash that and compare it with the stored hash. (just food for thought) – David C. Rankin Mar 05 '22 at 03:38

3 Answers3

0

you're making it all hard, just simply save id and password as string into file and later on read the file and later on to check use string.compare() function

  • Yes, this is a good suggestion but I wanted that when the user is entering the password it would display * (star) instead of his password. – fortnight learner Jul 31 '20 at 03:45
  • reprint the screen using system("cls") such that after you enter a charachter the charachter vanishes to become a * – Vacar Amin Jul 31 '20 at 10:21
0

Change the for loop from: for (int i = 0; i < p ; i++) to: for (int i = 0; i < p-1 ; i++)

The compiler is currently reading \r as being the last character of the entered password.

0
// For beginners, it will help 

#include <iostream>
#include <fstream>

using namespace std;

string username;
string password;


void reg(){
    
    fstream file("Data.txt", ios :: app);
    cout << "Enter the username" << endl;
    cin >> username;
    file << username << " ";
    cout << "Enter the password" << endl;
    cin >> pasword;
    file << pasword << endl;
    file.close();
    
}

int login(){
    string name, pass;
    while (1)
    {
        correct :
            fstream file("Data.txt", ios :: in);
            cout << "Enter the username you want to login" << endl;
            cin >> name;
            cout << "Enter the password" << endl;
            cin >> pass;
            while (file){
                file >> username;
                file >> password;
                if (name.compare(username) == 0){
                    if (pass.compare(password) == 0){
                        cout << "Loging successfully ........" << endl;
                        return 0;
                    }   
                }   
            } 
            cout << "Enter correct user name or pasword" << endl;
            goto correct;
    }
}

int main()
{
    int i;
    while(i != 3){
        cout << "Please choose between three" << endl;
        cout << "Press 1 to register a new account " << endl;
        cout << "Press 2 to login a existing account " << endl;
        cout << "Press 3 to exit" << endl;

        cout << "Enter your choice" << endl;
        cin >> i;

        switch (i)
        {
        case 1 :
            reg();
            break;

        case 2 :
            login();
            break;

        case 3 :
            return 1;
        
        default:
            cout << "Enter a valid input" << endl;
            break;
        }
    }   
    return 0;
}