First time using ifstream

Hi I need a bit of help writing a program that takes inputs from the int main, and writes them into a file password.txt. I am having issues, however, when I am trying to get the program to write into the .txt file.

password.txt starts out as:

jsmith turtle
madams apple


After the first compiling of my program the file looks like this:


dbotting 123qwe
egomez qwerty
tongyu liberty


Which is correct... BUT if I say compile it again, shouldn't is return the same thing? Instead it starts getting really messy and outputs.


dbotting 123qwe
egomez qwerty tongyu liberty
dbotting 123qwe
egomez qwerty
tongyu liberty


Also I am having trouble finding out how to show that user and password match in the int main(). Not sure how to go about writing that code...

Here's my entire code:

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
#include <string>
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;

class UserPw
{
public:
    UserPw(string user, string password);//Constructor
    string getUser(); // returns the user
    string getPassword();
    bool Checkpw(string user, string passwd); // returns true is user and password both match
    private :
    string user;
    string password;
};

UserPw::UserPw(string user, string password)
{
    this->user=user;
    this->password=password;
}

string UserPw::getUser()
{
    return user;
}

string UserPw::getPassword()
{
    return password;
}

bool UserPw::Checkpw(string user, string passwd)
{
    if(this->user == passwd)
    {
        return true;
    }
    else
    {
        return false;
    }
}

class PasswordFile
{
public:
    PasswordFile(string filename);// opens the file and reads the names/passwords in the vector entry
    vector<UserPw> getFile(); // returns the vector entry
    void addpw(UserPw newentry); //this adds a new user/password to entry and writes entry to the file filename
    bool checkpw(string user, string passwd); // returns true if user exists and password matches
private:
    string filename; // the file that contains password information
    vector<UserPw> entry; // the list of usernames/passwords
    void synch();
};

PasswordFile::PasswordFile(string filename)
{
    string pass,user;
    this->filename=filename;
    ifstream infile;
    getline(infile, user);
    getline(infile, pass);
    infile.open(this->filename.c_str());
    while (infile.good())
    {
        entry.push_back(UserPw(user, pass));
        getline(infile, user);
        getline(infile, pass);


    }
}

vector<UserPw> PasswordFile::getFile()
{
    return entry;
}

void PasswordFile::addpw(UserPw newentry)
{
    entry.push_back(newentry);
    synch();
}

bool PasswordFile::checkpw(string user, string passwd)
{
        if(user == passwd)
        {
            return true;
        }
        else
        {
            return false; 
        }
}

void PasswordFile::synch()
{
    ofstream outfile;
    outfile.open(this->filename.c_str());
    for(int i=0; i<entry.size();i++)
        {
            outfile << entry[i].getUser() << " " << entry[i].getPassword() << endl;
        }
}

int main()
{
    PasswordFile passfile("password.txt");
    passfile.addpw(UserPw("dbotting","123qwe"));
    passfile.addpw(UserPw("egomez","qwerty"));
    passfile.addpw(UserPw("tongyu","liberty"));
    //write code the shows that passwords match usernames
    
}
Would appreciate the assist. :)
Not done looking through but:
1
2
3
4
5
6
7
8
9
10
11
bool UserPw::Checkpw(string user, string passwd)
{
    if(this->user == passwd)  // shouldn't this be if(this->user == user && password == passwd)?
    {
        return true;
    }
    else
    {
        return false;
    }
}


Will edit with more as I keep looking.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
PasswordFile::PasswordFile(string filename)
{
    string pass,user;
    this->filename=filename;
    ifstream infile;
    getline(infile, user); //What are you doing here?
    getline(infile, pass); // You're reading from the file before opening it.
 // Also, this doesn't make sense because you're putting an entire line into user and an 
//entire line into pass, while the username and password are only separated by a space in the file.
    infile.open(this->filename.c_str());
    while (infile.good())  // I guess you're trying to save what was in the file before the program ran?
// If that's your intent, you can open a file without deleting its contents
// I forget how exactly, something like file.open("pass.txt", ios::app, ios::in)
// Again, if this is actually what you're trying to do, look it up
    {
        entry.push_back(UserPw(user, pass));  // These 3 lines don't do what I think you want at all
        getline(infile, user);
        getline(infile, pass);
//Should be: infile >> user >> pass;
//                 entry.push_back(UserPW(user,pass));

    }
}
Last edited on
Alright I tried your edits, as this is what password.txt looks like after the first compilation. It prints madams apple twice?

Also how could I get int main to show if the user and password match each other, any suggestions?

jsmith turtle
madams apple
madams apple
dbotting 123qwe
egomez qwerty
tongyu liberty
Last edited on
Topic archived. No new replies allowed.