void vectoring::cpw(string usr, char psw[20]){
string a;
string b;
string c;
int counter = 0;
int rows = 0;
vector<string>un;
vector<string>pw;
vector<string>lock;
vector<datastorage> dss;
datastorage tempds;
ifstream data("cashier.txt");
while (data.good()){
data >> a >> b >> c;
if(data.good()){
un.push_back(a);
pw.push_back(b);
lock.push_back(c);
rows++;
}
}
data.close();
for(int i=0; i < rows ; i++){
tempds.setusername(un[i]);
tempds.setpassword(pw[i]);
tempds.setlock(lock[i]);
dss.push_back(tempds);
}
for (int h = 0; h < dss.size(); h++){
if (usr == dss.at(h).getusername()){
if (dss.at(h).getlock() == "yes"){
cout << "\naccount is locked\n";
counter = 3;
}
}
}
bool t = false;
bool y = false;
while (counter < 2){
string pss;
pss = pp.encryption(psw);
for (int x = 0; x < dss.size(); x++)
{
if (usr == dss.at(x).getusername()){
t = true;
for (int o = 0; o < dss.size(); o++){
if (pss == dss.at(o).getpassword()){
y = true;
break;
}
}
}
}
if(t == true){
if(y == true){
char newpw[20];
string a;
string b;
int pcount = 0;
cout << "\nlogin sucessful\n";
while(pcount ==0){
cout << "\nplease type in your new password\n";
cin >> newpw;
b = newpw;
int i = 0;
int o = 0;
while(newpw[i]){
if(isalpha(newpw[i])){
n = true;
//while(isdigit(newpw[o])){
if(isdigit(newpw[o] = 0)){
o++;
}
else{
m = true;
break;
}
}
else{
i++;
}
}
if(b.length()>9 && n==true && m==true){
a = pp.encryption(newpw);
//cs.encryption(newpw);
ofstream writedata("cashier.txt");
for(int i=0; i < rows; i++){
if (usr == dss.at(i).getusername()){
writedata << dss.at(i).getusername()+" "<< a+" " << dss.at(i).getlock()+"\n";
}
else{
writedata << dss.at(i).getusername()+" "<< dss.at(i).getpassword()+" " << dss.at(i).getlock()+"\n";
}
}
writedata.close();
cout << "password changed!\n";
counter = 10;
pcount = 10;
}
else{
cout << "Password must contain at least 8 character\n";
cout << "Password must contain at least one letter\n";
cout << "Password must contain at least one numeric letter\n";
}
}
}
else{
cout << "\nwrong password please try again\n";
cin >> psw;
counter++;
}
}
else
{
cout << "\nwrong username please try again\n";
cin >> usr;
}
if(counter == 2){
cout << "\naccount is locked\n";
ofstream writedata("cashier.txt");
for(int i=0; i < rows; i++){
if (usr == dss.at(i).getusername()){
writedata << dss.at(i).getusername()+" "<< dss.at(i).getpassword()+" " << "yes\n";
}
else
writedata << dss.at(i).getusername()+" "<< dss.at(i).getpassword()+" " << dss.at(i).getlock()+"\n";
}
writedata.close();
}
}
ds.flush();
}
hi my apologise im really new to this forum and c++ i have pasted my whole code so maybe you guys would understand..
as the requirements require me to do this..
"Password must contain at least 8 character\n";
"Password must contain at least one letter\n";
"Password must contain at least one numeric letter\n"
Thanks!
Would you mind editing your post and putting the source inside code tags? It will make it a lot more legible and folks here will be more likely to look at it.
Why do you use a char array and C++ strings? Why not use strings for reading in? string newPassword;
Then also instead of using cin >> newPassword; use getline(cin, newPassword); so users can use spaces in their password and it gathers everything up to the newline (\n) character.
I have problems going through the rest of your code. What are you doing with all these integers? (pcount, i, o) and bools? (n, m). Give more descriptive names!
too long. Try to make just one thing per function and comment your code
"Password must contain at least 8 character\n";
"Password must contain at least one letter\n";
"Password must contain at least one numeric letter\n"
You could encapsulate those requirements in one function password.is_good()
line 14-31: Are you trying to check if the user and password are valid? Let a function take care of that.
Maybe you should be logged to try to change your password
1 2
if( user.is_logged() )
user.change_password();
line 32-39 and 44-58: What is the purpose?
btw: if( var==true ) is equivalent to if( var )
Edit: instead of reading and writing to the file every time you could load all the information to the memory. Make a commit function to write the changes.
You could use binary files, so you will not update the info that didn't change