-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
174 lines (140 loc) · 4.04 KB
/
main.cpp
File metadata and controls
174 lines (140 loc) · 4.04 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
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include <bits/stdc++.h>
using namespace std;
int main()
{
ifstream fin;
fin.open("email.in");
if(fin.fail())
{
perror("email.in");
return 1;
}
cout << "If you want to sign up for an email press 1. If you want to log into your email click 2." << endl;
int one;
cin >> one;
// capture the size of the file
string tempInput;
int Size = 0;
while(getline(fin, tempInput))
Size++;
bool alteredFile = false;
if(one == 1)
{
ofstream fout;
fout.open("email.out", ios::app);
if(fout.fail())
{
perror("email.out");
return 1;
}
cout << "Enter your username (all lowercase): ";
string username;
cin >> username;
cout << "Enter your password: ";
string password;
cin >> password;
cout << "Confirm your password: ";
string confirmPassword;
cin >> confirmPassword;
if(password != confirmPassword)
{
cout << "The passwords that you have entered do not match. Try again." << endl;
return 1;
}
// check to see if another account has already been made with the same username
string tempString;
fin.clear();
fin.seekg(0);
for(int i = 0; i < 2*Size; i++)
{
fin >> tempString;
if(tempString.compare(username) == 0 && i % 2 == 0)
{
cout << "There is already an account with this same username. Please try again with a different one." << endl;
return 1;
}
}
fout << username << " " << password << endl;
cout << "We have created your account!" << endl;
cout << username << " " << password << endl;
alteredFile = true;
}
else if(one == 2)
{
fin.clear();
fin.seekg(0);
ofstream fout;
fout.open("email.out");
if(fout.fail())
{
perror("email.out");
return 1;
}
cout << "Enter your username: ";
string username;
cin >> username;
string tempString;
bool foundUsername = false;
for(int i = 0; i < 2*Size; i++)
{
fin >> tempString;
if(username.compare(tempString) == 0 && i % 2 == 0)
{
foundUsername = true;
cout << "Enter your password: ";
string password;
cin >> password;
fin >> tempString;
if(password.compare(tempString) == 0)
cout << "Great! You are in the system!" << endl;
else cout << "Sorry, but the password has been entered in incorrectly. Please try again." << endl;
}
}
if(foundUsername == false)
cout << "Sorry, but the username has not been entered in correctly. Please try again." << endl;
}
else
{
cout << "You have not entered a valid command. Please try again." << endl;
return 1;
}
// copy the contents from the altered file over to the unaltered file
fin.clear();
fin.seekg(0);
if(alteredFile == true)
{
// copy the contents of ofstream into ifstream
fin.close();
ofstream copyContents;
ifstream copyContents2;
copyContents.open("email.in");
copyContents2.open("email.out");
string copyString2;
if(copyContents && copyContents2)
{
while(getline(copyContents2, copyString2))
copyContents << copyString2 << "\n";
}
copyContents.close();
copyContents2.close();
return 0;
}
else
{
string copyString;
ofstream fout;
fout.open("email.out");
if(fout.fail())
{
perror("email.out");
return 1;
}
if(fin && fout)
{
while(getline(fin, copyString))
fout << copyString << "\n";
}
}
fin.close();
return 0;
}