0

I am watching a youtube video from 2020 on how to make login system with c++. I have the exact same code as him but I have 1 error where when I save my first password everything works fine the password and user are saved as ID:1, but when I add another user the ID is set to -858993459. Here's the code, it's only in main.cpp.

#include <iostream>
#include <fstream>
#include <string>
#include <math.h>
#include <sstream>


using namespace std;

class loginManager {
public:
    loginManager() {
        accessGranted = false;
   }
   void login() {
        cout << "Enter your UserName and Password.\n";
        cout << "UserName: ";
        cin >> userNameAttempt;
        int usrID = retriveFile(userNameAttempt, "users.dat");
        if (usrID != 0) {
            cout << "Password: ";
            cin >> passwordAttempt;
            int pswID = retriveFile(passwordAttempt, "passwords.dat");
            if (usrID == pswID) {
                cout << "Successfull login!\n";
                accessGranted = true;
                login();
            }
            else {
                cout << "Wrong password!\nTry Again!\n\n";
                login();
            }
        }
        else {
            cout << "Wrong username!\nTry Again!\n\n";
            login();
        }
    }
    void addUser(const string username, const string password) {
        if (retriveFile(username, "users.dat") != 0) {
            cout << "That username is already in use!" << endl;
            return;
        }
        int id = 1 + getLastID();
        saveFile(username, "users.dat", id);
        saveFile(password, "passwords.dat", id);
    }
    int getLastID() {
        fstream file;
        file.open("users.dat", ios::in);
        file.seekg(0, ios::end);
        if (file.tellg() == 0) {
            return 0;
        }
        string s;
        for (int i = -1; s.find("#") == string::npos; i--) {
            file.seekg(i, ios::end);
            file >> s;
        }
        file.close();
        s.erase(0, 5);
        int id;
        istringstream(s) >> id;
        return id;
    }
    int retriveFile(string attempt, const char* pathFile) {
        string line;
        fstream file;
        string currentChar;
        long long eChar;

        file.open(pathFile, ios::in);
        while(1) {
            file >> currentChar;
            if (currentChar.find("#ID:") != string::npos) {
                if (attempt == line) {
                    file.close();
                    currentChar.erase(0,4);
                    int id;
                    istringstream(currentChar) >> id;
                    return id;
                }
                else {
                    line.erase(line.begin(), line.end());
                }

            }
            else {
                istringstream(currentChar) >> eChar;
                line += (char)decrypt(eChar);
                currentChar = "";
            }
            if (file.peek() == EOF) {
                file.close();
                return 0;
            }
        }   
    }
    void saveFile(string p_line, const char* pathFile, const int& id) {
        fstream file;
        file.open(pathFile, ios::app);
        file.seekg(0, ios::end);
        if (file.tellg() != 0) {
            file << "\n";
        }
        file.seekg(0, ios::beg);
        for (int i = 0; i < p_line.length(); i++) {
            file << encrypt(p_line[i]);
            file << "\n";
        }
        file << "#ID:" << id;
        file.close();
    }

    long long encrypt(int p_letter) {
        return powf(p_letter, 7) * 3 - 70;
    }
    long long decrypt(long long p_letter) {
        return powf((p_letter + 70) / 3, 1/7.f);
    }
private:
        string userNameAttempt;
        string passwordAttempt;
        bool accessGranted;
};


int main() {
    loginManager app;
    app.addUser("Something", "Somethingelse");
    app.login();
    return 0;
}
  • 1
    `-858993459` is a close to Microsoft debugging code. Uninitialized stack memory. You probably want to debug getLastID(). Maybe `s` is empty – drescherjm May 04 '22 at 21:54
  • You probably want a `ulong` instead of `long`. Also, if this is for learning purposes then it isn't a big deal, but you should be using a standard encryption method instead of what is defined here (which is arguably not even encryption). Better yet, you could hash the passwords, which is what most authentication systems do anyways. – h0r53 May 04 '22 at 21:55
  • So is the tutorial old or should I change something also I think the error is in getLastID() – Segi GameHD May 04 '22 at 21:59
  • warning C4717: 'loginManager::login': recursive on all control paths, function will cause runtime stack overflow – pm100 May 04 '22 at 22:01
  • 2
    The problem with Internet tutorials is no one curates them. Unless you already *almost* know how to do the job, or have a good quack detector, it's hard to tell if a tutorial really is any good. – user4581301 May 04 '22 at 22:02
  • if the files do not exist this code goes into an infinite loop, I expected it to create a new file for me – pm100 May 04 '22 at 22:05
  • Questions seeking debugging help should generally provide a [mre] of the problem, which includes the exact content of the input files. It should also specify the exact output and the desired output of the program. – Andreas Wenzel May 04 '22 at 22:29
  • -858993459 = 0xcccccccc, which means you've read uninitialized memory. See [When and why will a compiler initialise memory to 0xCD, 0xDD, etc. on malloc/free/new/delete?](https://stackoverflow.com/q/370195/995714) – phuclv May 05 '22 at 00:43
  • `getLastID()` says `s.erase(0, 5); int id; istringstream(s) >> id;` but to get out of the loop `s` had to be `#ID:1` so the erase makes `s` empty and then the extraction to `id` fails because there is no integer in the stream. And that's why `id` has an uninitialized value. I think the erase is meant to only erase the `#ID:` part so it should be `s.erase(0, 4); int id; istringstream(s) >> id;` – Jerry Jeremiah May 05 '22 at 01:55

0 Answers0