@mwilki7/

DHLDataEntryLogin

C++

This is only the user and password information.

fork
loading
Files
  • main.cpp
  • data

This Plugin Crashed!

Error: Error: must not create an existing file {"type":"CREATE_FILE","wid":"0.7852012779713049","path":"main.cpp","file":{"path":"main.cpp","content":{"asEncoding":{"base64":"Ly8KLy8gRGlzY2xhaW1lcjoKLy8gbXdpbGtpNzogdGhpcyBpcyBub3Qgb3JpZ2luYWxseSBteSByZXBsCi8vIHRoaXMgd2FzIG1vZGlmaWVkIHRvIGhlbHAgc29tZW9uZSBvbiB0aGUgIkFzayIgcGFnZQovLwoKI2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8ZnN0cmVhbT4KI2luY2x1ZGUgPHN0cmluZz4KCnVzaW5nIG5hbWVzcGFjZSBzdGQ7Cgp2b2lkIHBhdXNlKCkKewogICAgdW5zaWduZWQgaW50IGJpZ251bWJlciA9IDA7CiAgICBiaWdudW1iZXItLTsKCiAgICAvLyBnZXQgcmlkIG9mIGdhcmJhZ2UgaW5wdXQKICAgIGNpbi5jbGVhcigpOwogICAgY2luLmlnbm9yZShiaWdudW1iZXIsJ1xuJyk7CgogICAgLy8gYmVoYXZlcyBsaWtlIHN5c3RlbSgicGF1c2UiKTsKICAgIGNvdXQgPDwgIlByZXNzIGVudGVyIHRvIGNvbnRpbnVlIjsKICAgIGNpbi5nZXQoKTsKfQoKYm9vbCBJc0xvZ2dlZEluKCkKewogICAgc3RyaW5nIHVzZXJuYW1lLCBwYXNzd29yZCwgdW4sIHB3OyAKCiAgICBjb3V0IDw8ICIgRW50ZXIgVXNlcm5hbWU6ICAiOwogICAgY2luID4+IHVuOwogICAgY291dCA8PCAiIEVudGVyIFBhc3N3b3JkOiAgIjsKICAgIGNpbiA+PiBwdzsgCiAgICAKICAgIHN0cmluZyBmaWxlcGF0aCA9ICJkYXRhLyIgKyB1biArICIudHh0IjsgCiAgICBpZnN0cmVhbSByZWFkKGZpbGVwYXRoLmNfc3RyKCkpOwoKICAgIC8vIGlmIH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IHVzZXJuYW1lIDw8IGVuZGwgPDwgcGFzc3dvcmQ7CiAgICAgICAgY291dCA8PCB1c2VybmFtZSA8PCAiIHJlZ2lzdGVyZWQgdG86ICIgPDwgZmlsZXBhdGggPDwgZW5kbDsKICAgICAgICAvLyBvZnN0cmVhbSBhdXRvbWF0aWNhbGx5IGNsb3NlcyBpdHNlbGYKICAgIH0KICAgIGVsc2UgaWYgKGNob2ljZSA9PSBMT0dJTikKICAgIHsgCiAgICAgICAgYm9vbCBzdGF0dXMgPSBJc0xvZ2dlZEluKCk7CiAgICAgICAgaWYgKCFzdGF0dXMpCiAgICAgICAgewogICAgICAgICAgICBjb3V0IDw8ICIgRmFpbGVkIExvZ2luISIgPDwgZW5kbDsKICAgICAgICAgICAgcGF1c2UoKTsKICAgICAgICB9CiAgICAgICAgZWxzZSAKICAgICAgICB7CiAgICAgICAgICAgIGNvdXQgPDwgIiBTdWNjZXNzZnVsbHkgTG9nZ2VkIEluISIgPDwgZW5kbDsgICAgICAgICAgICAKICAgICAgICAgICAgcGF1c2UoKTsKICAgICAgICB9CiAgICAgIAogICAgfQoKICAgIGNvdXQgPDwgIlByb2dyYW0gZmluaXNoZWQiIDw8IGVuZGw7CiAgICByZXR1cm4gMDsKfQ=="},"asBuffer":null},"loaded":true}}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
//
// Disclaimer:
// mwilki7: this is not originally my repl
// this was modified to help someone on the "Ask" page
//

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

void pause()
{
    unsigned int bignumber = 0;
    bignumber--;

    // get rid of garbage input
    cin.clear();
    cin.ignore(bignumber,'\n');

    // behaves like system("pause");
    cout << "Press enter to continue";
    cin.get();
}

bool IsLoggedIn()
{
    string username, password, un, pw; 

    cout << " Enter Username:  ";
    cin >> un;
    cout << " Enter Password:  ";
    cin >> pw; 
    
    string filepath = "data/" + un + ".txt"; 
    ifstream read(filepath.c_str());

    // if user exists, I can find it at data/username.txt
    // unless it's an invalid username
    if (!read.good())
    {
        // invalid username
        return false;
    }

    getline(read, username);
    getline(read, password);

    if (pw == password)
    {
        return true;
    }
    else
    {
        return false;
    }
}

int main ()
{
    int choice = -1;

    int REGISTER = 1;
    int LOGIN = 2;

    cout << "1:Register\n2:Login\nYour choice:  ";

    cin>> choice;

    if (choice == REGISTER)
    {
        string username, password;

        cout <<"Select a username:  ";
        cin  >> username;
        cout << "Select a password:  ";
        cin  >> password;

      
        ofstream file;
        string filepath = "data/" + username + ".txt";
        file.open(filepath.c_str(), std::ios_base::app);
        file << username << endl << password;
        cout << username << " registered to: " << filepath << endl;
        // ofstream automatically closes itself
    }
    else if (choice == LOGIN)
    { 
        bool status = IsLoggedIn();
        if (!status)
        {
            cout << " Failed Login!" << endl;
            pause();
        }
        else 
        {
            cout << " Successfully Logged In!" << endl;            
            pause();
        }
      
    }

    cout << "Program finished" << endl;
    return 0;
}