-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
102 lines (88 loc) · 2.58 KB
/
main.cpp
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
#include <iostream>
#include <fstream> // reading and writing files
#include <string>
using namespace std;
bool isLoggedIn()
{
string username, password;
string un, pw; // comparison strings
cout << "Enter a username: ";
cin >> username;
cout << "Enter a password: ";
cin >> password;
ifstream read(username + ".txt"); // ifstream reads a file
getline(read, un); // reads the username
getline(read, pw); // reads the password
if(un == username && pw == password) // if both un & username and pw & password are the same, true
{
return true;
}
else
{
return false;
}
}
int main()
{
int choice;
cout << endl;
cout << "Main Menu: " << endl;
cout << "-------------------------------" << endl;
cout << "1. Register" << endl;
cout << "2. Login" << endl;
cout << endl;
cout << "Your choice: ";
cin >> choice;
if(choice == 1)
{
string username, password;
cout << "Registration: " << endl;
cout << endl;
cout << "Select a username: ";
cin >> username;
cout << "Select a password: ";
cin >> password;
ofstream file; // ofstream creates a file
file.open(username + ".txt");
file << username << endl << password;
file.close();
cout << "Welcome " << username << "!" << endl;
}
else if(choice == 2) // if user chooses to login
{
bool status = isLoggedIn();
if(!status) // if isLoggedIn() returns false
{
system("clear");
cout << endl;
cout << "Invalid login!" << endl;
main();
return 0;
}
else // if isLoggedIn() returns true, the dashboard is displayed
{
cout << "Successfully logged in!" << endl;
cout << endl;
int choiceTwo;
// Display dashboard:
cout << "Welcome back!" << endl;
cout << endl;
cout << "DASHBOARD" << endl;
cout << "No new messages." << endl; // example of what could appear on the user's dashboard
cout << endl;
cout << "1. Sign Out" << endl;
cout << "2. Back to main menu" << endl;
cout << "Your choice: " << endl;
cin >> choiceTwo;
if(choiceTwo == 1)
{
system("exit"); // exits program
}
else if(choiceTwo == 2)
{
main(); // returns to the beginning of the main() function
}
return 1;
}
}
}