-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.cpp
More file actions
65 lines (60 loc) · 1.4 KB
/
auth.cpp
File metadata and controls
65 lines (60 loc) · 1.4 KB
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
#include "auth.h"
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;
bool existUser(const string &username)
{
ifstream infile("users.txt");
string line, user, pass;
while (getline(infile, line))
{
stringstream ss(line);
getline(ss, user, ',');
if (user == username)
{
return true;
}
}
return false;
}
bool login()
{
cout << "Enter the username : ";
string username, password;
cin >> username;
cout << "Enter the password : ";
cin >> password;
ifstream infile("users.txt");
string line, user, pass;
while (getline(infile, line))
{
stringstream ss(line);
getline(ss, user, ',');
getline(ss, pass, ',');
if (user == username && pass == password)
{
cout << "Login Successful" << endl;
return true;
}
}
cout << "Invalid Username or Password. Try again.\n";
return false;
}
bool signup()
{
string username, password;
cout << "Enter the username : ";
cin >> username;
if (existUser(username))
{
cout << "Username already exists. Please log in.\n";
return false;
}
cout << "Enter the password : ";
cin >> password;
ofstream outfile("users.txt", ios::app);
outfile << username << "," << password << endl;
cout << "Signup successful.\n";
return true;
}