Here is my code. This part works. It accepts the string and writes it to the file
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
|
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <cmath>
using namespace std;
class UserPw
{
public:
UserPw(string u, string p);
string getUser();
string getPassword();
private :
string user;
string password;
};
UserPw::UserPw(string u, string p)
{
user = u;
password = p;
}
string UserPw::getUser()
{
return user;
}
string UserPw::getPassword()
{
return password;
}
class PasswordFile
{
public:
PasswordFile(string f);
vector<UserPw> getFile();
void addpw(UserPw newentry);
void WriteToFile();
bool checkpw(UserPw entry);
void updatepw(UserPw entre, string newpass);
void deletepw(UserPw entre);
private:
string filename;
vector<UserPw> entry;
};
PasswordFile::PasswordFile(string f)
{
filename = f;
fstream infile;
infile.open(filename.c_str());
string u,p;
infile >> u >> p;
while(infile.good())
{
entry.push_back(UserPw(u,p));
infile >> u >> p;
}
infile.close();
}
vector<UserPw> PasswordFile::getFile()
{
return entry;
}
void PasswordFile::WriteToFile()
{
fstream outfile;
outfile.open(filename.c_str());
for (int i=0; i < entry.size(); i++)
{
outfile <<entry[i].getUser() << " " << entry[i].getPassword() <<"\n";
}
outfile.close();
}
void PasswordFile::addpw(UserPw newentry)
{
entry.push_back(newentry);
this -> WriteToFile();
}
int main()
{
PasswordFile passfile("password.txt");
passfile.addpw(UserPw("dbotting","123qwe"));
passfile.addpw(UserPw("egomez","qwerty"));
passfile.addpw(UserPw("tongyu","liberty"));
passfile.addpw(UserPw("omurphy", "bruins"));
}
|
I can't get these instructions to work, I've been working on it for hours.
1. bool checkpw(UserPw entry)
This function accepts a username/password combination and checks the entry in the PasswordFile object. If the username/password combination matches that in the PasswordFile object, the function returns true. Otherwise it returns false.
2. void updatepw(UserPw updateentry)
This function accepts a username/password combination and changes the entry with the username to the password provided in updateentry. For example, if updateentry has the username/password values John/cat, then updatepw will change the password for John to cat. Don't forget to write the updated information to the file.
3. void deletepw(UserPw deleteentry)
This function accepts a username/password combination and deletes the entry with the provided username. For example, if deleteentry has the username/password values John/cat, then deleteentry will delete the entry with the username John. (The password part is ignored.) Again, don't forget to write the updated password information to the file.
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
|
bool PasswordFile::checkpw(UserPw entry)
{
for (int i=0; i< entry.size; i++)
{
if (entry[i].getUser() = entry[i].getUser())
{
if(entry[i].getPassword() = entry[i].getPassword())
{
return true;
}
}
}
return false;
}
void PasswordFile::updatepw(UserPw entre, string newpass)
{
for(int i = 0; i < entry.size(); i++)
{
if (entry[i].getUser() == entre[i].getUser())
{
if(entry[i].getPassword() == entre.getPassword())
{
this -> deletepw(entre);
this -> addpw(UserPw(entre.getUser(), newpass));
this -> WriteToFile();
}
}
}
}
void PasswordFile::deletepw(UserPw deleteentry)
{
entry.push_back(deleteentry);
this -> WriteToFile();
for(int i = 0; i < entry.size(); i++)
{
if (deleteentry[i].getUser() == entry[i].getUser())
{
if(entry[i].getPassword() == entry[i].getPassword())
{
(entry[i] == entry.size() - 1);
entry.pop_back();
this -> WriteToFile();
}
}
}
}
|