Ifstream and ofstream mixup

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
  void registerNewAdmin(vector<Admin> &admins) {

	bool read = true;
	while (read) {
	
		ifstream infile;
		infile.open("admins.dat");

		string strUser;


		while (infile.good()) {
			getline(infile, strUser);
			if (strUser.length() > 1) {
				admins.push_back(Admin(strUser));
			}
		}

		infile.close();

		ofstream outfile;
		outfile.open("admins.dat", ios::app);
	
		string usernames[admins.size()]	;
		
		int i;
	
		for (i=0; i<admins.size(); i++) {
			usernames[i] = admins[i].getUsername();		
		}

	
		Admin user = Admin();
		
		cout << "Username: ";
		string username;
		cin >> username;
		bool loop=1;
		bool flag=0;
		while( loop ){
			flag=0;
				for(i=0; i<admins.size(); i++){
					if (username == usernames[i]){
						flag=1;
					}
				}
				if(flag == 0){
					loop = 0;
				}
				else{
					cout << "Username alread exists: ";
					cin >> username;
					
				}
				
			}
		user.setUsername(username);
		cout << "Password: ";
		string password;
		cin >> password;
		user.setPassword(password);		
		user.save(outfile);
		char action;
		cout << "Would you like to add another user? (y/n): ";
		cin >> action;
		if (action == 'y') {
			continue;
		}
		else {
			break;
		}
		outfile.close();
	}
		
}

void listAdmins(vector<Admin> &admins) {

	ifstream infile;
	infile.open("admins.dat");
	string strUser;

	while (infile.good()) {
		getline(infile, strUser);
		if (strUser.length() > 1) {
			admins.push_back(Admin(strUser));
		}
	}

	infile.close();
	
	cout << endl << endl;		
    
	for (int i = 0; i < admins.size(); i++) {
		cout << i+1 << ") " << admins[i].getUsername() << endl;		
	}
	
	cout << endl << endl << endl;
}



Those two functions register and list admins(which is a class)
If you call the registerNewAdmin and register 3 new admins and then list them(which are registered fine in the file),instead of listing them correctly,you get this:

1)1st entry
2)1st entry
3)2nd entry
4)1st entry
5)2nd entry
6)3rd entry

but if you close and run the code again and straight up list the users you get them correctly..any ideas?
If I put 6 things in a bucket, should I expect it to contain 3?
what?
You keep adding things to your vector and not removing them.

Adding this line may enlighten:
1
2
3
4
5
6
7
void listAdmins(vector<Admin> &admins) {

	cout << "admins already contains " << admins.size() << " elements!\n" ;    

	ifstream infile;
	infile.open("admins.dat");
	string strUser;


When you print out the list, it is displaying the contents of the vector admins, rather than what is contained in the file.


This is an interesting pattern:
1)1st entry
2)1st entry
3)2nd entry
4)1st entry
5)2nd entry
6)3rd entry


if you break it up into groups, you should see how it was built that way:

Group 1
1)1st entry

Group 2
2)1st entry
3)2nd entry

Group 3
4)1st entry
5)2nd entry
6)3rd entry


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Admin(string str) {
		istringstream issin(str);
		string split[2];
		int i = 0;

		while (getline(issin, split[i], '|')) {
			i++;
		}
		
		this->username = split[0];
		this->password = split[1];
	}

      void save(ofstream &outfile) {
		outfile << this->toString() << endl;
	}

	string toString() {
		string str = getUsername() + "|" + getPassword();
		return str;
	}


if you are refering to line 86

EDIT: i fixed it,i just stoped calling the constructor and just stored the usernames in a vector,thanks
Last edited on
Topic archived. No new replies allowed.