0

I have to make a program that handles user register and login using text file as the place to store the user's data.

I finished the register and login function and the register is working just fine but the login function only works when I register in the same run , if I run it one more time and doesn't register and login with the data already in the text file , it shows the output of the username or password being incorrect.

There's also admin and status thing that is also needed but it's working fine.

Here's the register and login code :

#include <iostream>
#include <fstream>
#include <iomanip>
#include <conio.h>
#include <string>
using namespace std;

bool administrator = false;
bool login = false;

string username;
char admin;
string password;
char status;

string inputusername;
string inputpassword;


void createAcc()
{       
        cout << "\n\n";
        cout << "=================================" << endl;
        cout << setw(25) << "USER REGISTRATION" << endl;
        cout << "=================================" << endl;
        cout << "Enter Username :" << endl;
        cin >> username;

        cout << "Enter User Type : " << endl << "Administrator = [a]        Buyer = [b]" << endl << endl;
        cin >> admin;
        while(admin != 'a' && admin != 'b')
        {
            cout << "Enter a valid choice : " ;
            cin >> admin;
        }

        cout << "Enter Password : " << endl;
        cin >> password;

        status = 'a';

        ofstream userData("userdata.txt", ios::out | ios::app);

        if (!userData.is_open())
        {
            userData.open("userdata.txt");
        }

            userData << username << " " <<  admin << " " << password << " " << status << endl;

            userData.close();

        cout << endl << "Successfully Registered!" << endl;

}



void loginAcc()
{   
    cout << "\n\n";
    cout << "=================================" << endl;
    cout << setw(25) << "USER LOGIN" << endl; 
    cout << "=================================" << endl;
    cout << "Enter Username : " << endl;
    cin >> inputusername;

    cout << "Enter Password : " << endl;
    cin >> inputpassword;

    string userPass = inputusername + " " + admin + " " + inputpassword + " " + status;
    int offset;
    string line;

    ifstream userData;
    userData.open("userdata.txt");

    bool found = false;
    if(userData.is_open())
    {
        while(getline(userData,line) && !found)
        {
            if (line.compare(userPass) == 0)
            {
                found = true;
            }
        }
        userData.close();
        if(found){
            login = true;
            cout << "Successfully Logged In!" << endl;
        }
        else if(status == 'b')
        {
            cout << endl << "User deleted by admin" << endl;
        }
        else{
            cout << endl << "Username or Password is Incorrect" << endl;
        }

    }
    
    if (admin == 'a'){
        administrator = true;
    }
}

And the data in the text file is still there and nothing changed.

I'm having trouble debugging this and would appreciate any help.

cam
  • 4,409
  • 2
  • 24
  • 34
Adam
  • 1
  • 1
    Please run your code in a debugger, line-by-line, and compare what really happens with what you think should be happening. That's generally the method to fix such things - the dialog is between you and the debugger. – Kuba hasn't forgotten Monica Oct 15 '21 at 02:20
  • 1
    Avoid including `conio.h` in any code you write. It is an archaic relic from the DOS days and makes your code 100% non-portable to anything other than a DOS/windows OS. (not to mention it isn't necessary to your code). If you need to hold a terminal window open so you can see the output with, e.g. VS, etc..) just use `std::cin.get();` See also [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/q/1452721/364696) – David C. Rankin Oct 15 '21 at 02:37
  • Another tip, you only need one `std::cout` for any continual block of output (not one-per-line). That's what `'\n'` is for. Both your banners can be done with a single `std::cout`. Also have a look at [C++: “std::endl” vs “\n”](https://stackoverflow.com/questions/213907/c-stdendl-vs-n) to understand the difference between using one or the other. – David C. Rankin Oct 15 '21 at 02:40
  • Your logic to open the file to write is odd. I mean if it failed you try again then ignore that that will almost certainly fail again and write to the file anyways even though its not open. – drescherjm Oct 15 '21 at 02:40

1 Answers1

0

In you loginAcc function, the variable 'admin' and 'status' are never set. The only part that executed is char admin;, which only means "create this variable", not "set it to anything". So the value stored in there could be anything, from 42 to ÿ.

It would be a good idea to bring all those variables,

string username;
char admin;
string password;
char status;

string inputusername;
string inputpassword;

to right after the start of your functions. That will make the compiler point out errors like this.

Daniël van den Berg
  • 2,197
  • 1
  • 20
  • 45