NEED HELP with assignment.

So any help would be greatly appreciated. I'm trying to write a program for class which involves combining a few previous programs I had to write. The problem im having is I need for the create() method at the end check the file to see if the attempted user name has been created and if so increment by adding a number and continuosly until a number hasnt been found.

Heres my logic im trying to accomplish

username = mortk

if(username is found){

username will be mortk1

if (mortk1 is found) {

username is mortk2


and this will continue until its not found, however I want to be able to loop it continuously until its not found.


Here is the code I currently have for my program:

#include <iostream>
#include <string>
#include <fstream>
#include <algorithm>
#include <cstdlib>

using namespace std;



void create();
void login();

ifstream file("userCred.txt");
ofstream created("userCred.txt", ios::in | ios::out | ios::app);


int main()
{

int choice = 0;

do {
do {
cout << endl << endl;
cout << "***** SECURITY ACCOUNT ACCESS *****" << endl << endl;
cout << "1) Create New Account" << endl;
cout << "2) Login to Existing Account" << endl;
cout << "3) Exit " << endl << endl;
cout << "Please Enter Your Choice: ";
cin >> choice;

} while (choice != 1 && choice != 2 && choice != 3);

switch (choice) {

case 1:
create();
break;

case 2:
login();
break;

default:
return 0;

}

} while (choice == 1 || choice == 2);

}

void create() {

//This is declaring all the strings that will be needed to create the user

string fname;
string lname;
string username;
string password;
string email;
string check;
string yes = "y";
string yes2 = "Y";
string no = "n";
string no2 = "N";
char a;
int pwdCheck;

//This declares where the new users credentials will be stored.
cout << endl << endl;
cout << "New User Account Creation" << endl;
cout << endl;

//This code queries the user and verifies the information
do
{
cout << "Please Enter Your First Name: ";
getline(cin, fname);
getline(cin, fname);
cout << "Please Enter Your Last Name: ";
getline(cin, lname);
cout << endl;
cout << "Please Verify That Your Name Is: " << fname << " " << lname << endl;

// This code verifies that the user has entered a the correct information.
do
{
cout << endl;
cout << "Is This Information Correct? Please ENTER (Y for Yes or N for No): ";
getline(cin, check);
} while ((check != yes) && (check != yes2) && (check != no) && (check != no2));

cout << endl;

} while ((check != yes) && (check != yes2));

// This code will store the password and verify that it meets the length requirement and that there are no empty spaces
// If it doesnt meet these requirements it will reprompt

do {
cout << "Please Enter a Password for the Created User: ";

do {
getline(cin, password);
} while (0 == password.length());

pwdCheck = password.find(' ');

if (pwdCheck > 0) {
cout << "Spaces are not permitted in your password, please try again." << endl;
cout << endl;
}
if (10 > password.length()) {
cout << "Passwords must be atleast 10 characters in length" << endl;
cout << endl;
}

} while (pwdCheck > 0 || 10 > password.length());

//This will take the entered information and create the username in lowercase
username = lname.substr(0, 4) += fname.at(0);

for (int i = 0; i < username.length(); i++) {
username[i] = tolower(username[i]);

}


//This creates the email account and outputs the generated email for the user

email = username + "@fbi.gov";

cout << endl << endl;
cout << "Account has been created for: " << username << endl;
cout << "Email Address: " << email << endl;

// This will check for duplicate usernames and adjust



// This will write the information to the credentials file for storage



}







void login() {

string username;
string password;
string credCopy;
string creds;
int check;
int credFile;
int safety = 0;
bool authorized = false;

//This streams the credentials file into a string named transfer

string transfer((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
credCopy = string(transfer);

do {
// This will prompt the user for their username and password and store it for comparison
cout << endl;
cout << "Please enter your username: ";
cin.ignore();
getline(cin, username);
cout << endl;
cout << "Please enter your password: ";
getline(cin, password);
cout << endl;


//Creating credentials string and storing it as string creds

creds = username + " :: " + password;

//Finding entered credentials location within the string

check = credCopy.find(creds);
credFile = credCopy.length();

//Granting or Denying Access to the user
if (check >= 0 && check < credFile) {

cout << endl << endl;
cout << " Access Granted" << endl;
cout << endl;
cout << " Welcome " << username << "!" << endl;
cout << endl;
authorized = true;
safety = 10;
file.close();
}
else {

if (safety < 2) {
cout << "The Username/Password combination could not be verified, please try again." << endl;
cout << endl << endl;
safety++;
}

else {
safety++;
}

}



} while ((authorized == false) && (safety < 3));

if (safety == 3) {

cout << "This terminal has been locked for security purposes, please contact your network administrator" << endl << endl;
}
}

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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#include <iostream>
 #include <string>
 #include <fstream> 
 #include <algorithm>
 #include <cstdlib>

 using namespace std;



 void create();
 void login();

 ifstream file("userCred.txt");
 ofstream created("userCred.txt", ios::in | ios::out | ios::app);


 int main()
 {

 int choice = 0;

 do {
 do {
 cout << endl << endl;
 cout << "***** SECURITY ACCOUNT ACCESS *****" << endl << endl;
 cout << "1) Create New Account" << endl;
 cout << "2) Login to Existing Account" << endl;
 cout << "3) Exit " << endl << endl;
 cout << "Please Enter Your Choice: ";
 cin >> choice;

 } while (choice != 1 && choice != 2 && choice != 3);

 switch (choice) {

 case 1:
 create();
 break;

 case 2:
 login();
 break;

 default:
 return 0;

 }

 } while (choice == 1 || choice == 2);

 }

 void create() {

 //This is declaring all the strings that will be needed to create the user

 string fname;
 string lname;
 string username;
 string password;
 string email;
 string check;
 string yes = "y";
 string yes2 = "Y";
 string no = "n";
 string no2 = "N";
 char a;
 int pwdCheck;

 //This declares where the new users credentials will be stored.
 cout << endl << endl;
 cout << "New User Account Creation" << endl;
 cout << endl;

 //This code queries the user and verifies the information
 do
 {
 cout << "Please Enter Your First Name: ";
 getline(cin, fname);
 getline(cin, fname);
 cout << "Please Enter Your Last Name: ";
 getline(cin, lname);
 cout << endl;
 cout << "Please Verify That Your Name Is: " << fname << " " << lname << endl;

 // This code verifies that the user has entered a the correct information.
 do
 {
 cout << endl;
 cout << "Is This Information Correct? Please ENTER (Y for Yes or N for No): ";
 getline(cin, check);
 } while ((check != yes) && (check != yes2) && (check != no) && (check != no2));

 cout << endl;

 } while ((check != yes) && (check != yes2));

 // This code will store the password and verify that it meets the length requirement and that there are no empty spaces
 // If it doesnt meet these requirements it will reprompt

 do {
 cout << "Please Enter a Password for the Created User: ";

 do {
 getline(cin, password);
 } while (0 == password.length());

 pwdCheck = password.find(' ');

 if (pwdCheck > 0) {
 cout << "Spaces are not permitted in your password, please try again." << endl;
 cout << endl;
 }
 if (10 > password.length()) {
 cout << "Passwords must be atleast 10 characters in length" << endl;
 cout << endl;
 }

 } while (pwdCheck > 0 || 10 > password.length());

 //This will take the entered information and create the username in lowercase
 username = lname.substr(0, 4) += fname.at(0);

 for (int i = 0; i < username.length(); i++) {
 username[i] = tolower(username[i]);

 }


 //This creates the email account and outputs the generated email for the user

 email = username + "@fbi.gov";

 cout << endl << endl;
 cout << "Account has been created for: " << username << endl;
 cout << "Email Address: " << email << endl;

 // This will check for duplicate usernames and adjust



 // This will write the information to the credentials file for storage



 }







 void login() {

 string username;
 string password;
 string credCopy;
 string creds;
 int check;
 int credFile;
 int safety = 0;
 bool authorized = false;

 //This streams the credentials file into a string named transfer 

 string transfer((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
 credCopy = string(transfer);

 do {
 // This will prompt the user for their username and password and store it for comparison
 cout << endl;
 cout << "Please enter your username: ";
 cin.ignore();
 getline(cin, username);
 cout << endl;
 cout << "Please enter your password: ";
 getline(cin, password);
 cout << endl;


 //Creating credentials string and storing it as string creds

 creds = username + " :: " + password;

 //Finding entered credentials location within the string

 check = credCopy.find(creds);
 credFile = credCopy.length();

 //Granting or Denying Access to the user
 if (check >= 0 && check < credFile) {

 cout << endl << endl;
 cout << " Access Granted" << endl;
 cout << endl;
 cout << " Welcome " << username << "!" << endl;
 cout << endl;
 authorized = true;
 safety = 10;
 file.close();
 }
 else {

 if (safety < 2) {
 cout << "The Username/Password combination could not be verified, please try again." << endl;
 cout << endl << endl;
 safety++;
 }

 else {
 safety++;
 }

 }



 } while ((authorized == false) && (safety < 3));

 if (safety == 3) {

 cout << "This terminal has been locked for security purposes, please contact your network administrator" << endl << endl;
 }
 }
line 31 change to choice = getch(); "headerfile <conio.h>
line 93: why not switch(choice)
1
2
3
4
 case 'y':
case 'Y':
//do stuff
break;


and small bug i found , when creates account and verify if user first/last name run that part..and you select 'n'

1
2
3
4
5
6
while( !file.eof() )
{
//do stuff

//read(check if at end of file) 
}
this will run till end of file.
Last edited on
Alright I made the suggested changes but how do I go about preventing duplicate usernames by a numbering convention.
Hi,

Can you not just append a number to the name by using std::string + operator like you have elsewhere in the code? Put all the user names into a std::vector.

Some other things I noticed, just to help you out for some good guidelines & style:

} while (choice != 1 && choice != 2 && choice != 3);

I really dislike no hate constructs like that :+) You already have a switch instead. Make the exit option set a bool Quit variable to true, make that the end condition for a while loop.

With this:

88
89
90
91
92
93
94
95
96
97
do
 {
 cout << endl;
 cout << "Is This Information Correct? Please ENTER (Y for Yes or N for No): ";
 getline(cin, check);
 } while ((check != yes) && (check != yes2) && (check != no) && (check != no2));

 cout << endl;

 } while ((check != yes) && (check != yes2));


You can avoid this nightmare :+) by making use of the toupper function to halve the logic, Just check for Y or N

What is the fascination beginners seem to have with do loops :+) ? Did you know that all three looping constructs can be converted from one to another? I use a for loop when I know how times to loop, and a while loop otherwise.

You could do with some more functions. There is a guideline which says that functions should be no longer than 40 LOC, some go for even less. This will improve the readability and aid understanding of the code

Proper indentation would make the code easier to read.

Good Luck !!
I had tried for the string + operator but i still need some form of looping. for instance.

created file has users:
mortk
mortk1

id need some form of looping that takes the character in spot 5 and increment it. Is there a way to do this using ASCII character value maybe?
Ok,

So if you store all the user names in a std::set, each item must be unique.

insert returns the position of an item if it is already there.

Sort the container.

Use equal_range to find all the values that have your user-name in them. You will need a sub-string to only look at the first n chars of the string.

Once you have found the last one with a number on the end, it should be easy enough to find out what that number is, ( it might be a 2 digit number), convert that part of the string to an int, increment it, then append it to the base string again.

So that was a methodology using STL, there are variations on that. Some one else might have a more elegant solution.

For example, use a range based for to go through a container, anything which matches - push into another container std::vector say, then go from there.

http://www.cplusplus.com/reference/set/set/insert/
http://www.cplusplus.com/reference/algorithm/equal_range/
http://www.cplusplus.com/reference/string/stoi/


Good Luck !!
is there anyway you might be able to give me an example of that, I dont mean to push my luck but Im pretty new to c++.
Topic archived. No new replies allowed.