Help with this code please

I made this code where you create an account and then the username and password are saved so that you don't have to create a new one every time you run the program. However , if I make a new account , the old one is overwritten. How can I make the username.txt and password.txt files remember multiple account and password entries ?

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
#include <iostream>
#include <fstream>
#include <string.h>
#include <stdlib.h>
#include <cstring>

using namespace std;


   char user[10];
   ofstream username;
   char pass[10];
   ofstream password;
   ifstream readusername;
   char readuser[10];
   ifstream readpassword;
    char readpass[10];
    int decision;

    void login ()
{
    readusername.open ("username.txt");

    readusername >> readuser;


    readpassword.open ("password.txt");

    readpassword >> readpass;

    char userint[10],passint[10];
    e2:
    cout<<"Enter username : \n";
    cin>>userint;
    if (strcmpi(userint,readuser)==0)
    {
        e1:
        cout<<"Enter password : \n";
        cin>>passint;
        if (strcmpi(passint,readpass)==0)
        {
            cout<<"Logging in..."<<endl;

        }
        else
        {
            cout<<"Incorrect password !"<<endl;
            goto e1;
        }
    }
    else
    {
        cout<<"This username does not exist !"<<endl;
        goto e2;
    }

}

void createaccount ()
{
    cout<<"Choose a username"<<endl;
    cin>>user;
    username.open ("username.txt");
    username << user;
    username.close();


    cout<<"Choose a password"<<endl;
    cin>>pass;
    password.open ("password.txt");
    password << pass;
    password.close();
    cout<<"Account created succesfully !\nLog into your new account"<<endl;
    login();

}


int main ()

{
    cout<<"1.Create account\n2.Log in\n";
    cin>>decision;
    if (decision==1)
    {
        createaccount();
    }
    if (decision==2)
    {
        login();
    }
}
You need to write to the end of the file so that existing records are not overwritten, use:

app append All output operations happen at the end of the file, appending to its existing contents.

http://www.cplusplus.com/reference/fstream/fstream/open/
It sort of works this way. It introduces the new data to the file. The problem is that now , instead of writing on a new line when i use the app thing , it writes right next to the former username and password and corrupts both of them. How can i make it write on a completely new line in the txt document ?
Line 64,71: When you write user and pass to their respective files, you don't write a '\n' which would force a new line. endl would also have the same effect.
Hello Raul4pk,

Try changing line 71 to password << pass << endl;.

And if the ".txt" file has information in it then it should be:
1
2
UserName  // Needs to end with a new line "\n"
                // <--- This should be a blank line so the read works right. 


For at least the user name consider using "std::getline()" in place of the "std::cin >>" because if the user name would have a space in it the whole name would not be read from the file with "cin >>".

Hope that helps,

Andy
So it's supposed to be password <<pass <<endl; ??
Okay , it's working now but the program only reads the first username. How can i make it read the second and other usernames ?
Hello Raul4pk,

Yes.

The next problem I see is that the "Login()" function only reads one "username" and one "password" and pretty much ignores the rest of the file.

I would consider creating a "Read()" function that would read the entire file and store the information in a "std::map" http://www.cplusplus.com/reference/map/map/ . This way you can pass the map to the different functions and loop through the map or add to the end. This would make it easier to work with your information.

I would also suggest finding a way to get rid of the "goto" statements as this is bad form and not the best way to deal with the problem. Consider changing line 37 to a for loop to give a maximum of 3 tries to enter the password before the program ends. As you have it with the "goto" it looks like it would be an an endless loop.

Hope that helps,

Andy
Hello Raul4pk,

Based on what you have lines 24 and 29 need to inside some kin of loop like a while loop to continually read the file something like:

while (readusername >> readuser && readpassword >> readpass).

Then again this will only work as long as the two files are in sync. Otherwise one of the files would reach "eof" before the other and the user names and passwords could end up being off by one. see previous message: http://www.cplusplus.com/forum/beginner/208583/#msg982588 about a read function.

It might be easier to rewrite the "login()" function than trying to fix what you have.

Working on the solution.

Hope that helps,

Andy
Thanks , Andy
Topic archived. No new replies allowed.