reading login data file line per line

i'm new to c++ and doing my project about login system.

i have a text file named loginData.txt which contain the username, password, and ID. the format is like this below

1
2
3
  username password accountID
  usrname1 password1 accountID1
  username2 password2 accountiD2


my problem is reading those each data strin line per line and store each it into variable, *that's only if the username and password match

what i tried so far

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
using namespace std;
int main(){
ifstream data;
bool valid = true;
string usrname,pass,id;
data.open("dataLogin.txt");
while(!valid){
data >> username >> pass >> id;
if (username == inputUsrname && pass = inputPass){
valid = true;
}
else{
valid=false;
}
thanks sakura,

after i tried that code i'm having an issue which how to return it, here my code below
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
bool login_validation(string usrName, string pass) {
    bool valid = false;
    string getUsrname, getPass, getID;
    ifstream data;
    data.open("dataLogin.txt");
    // here open file to replace the test
    while (data >> getUsrname >> getPass >> getID) {
        if (usrName == getUsrname && pass == getPass) {
            valid = true;
            break;
        }
    }

    if(valid){
        return true;
    }
    else{
        return false;
    }



and it's my loginData.txt
 
daniel pass 1000023432


it still saying that my username & password is incorrect
i already did that correctly, i also checked the dataLogin.txt is in the right directory and the file format is correct :
daniel pass 1000023432

here's my fullcode , if u don't mind pls look into it :p

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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include "accountData.h" // not related --

void main_Menu();
bool login_validation(string usrName, string pin);
void login_page();
void forgot_password();
void clearScreen();
void account_Page();

int main()
{
    clearScreen();
    main_Menu(); // call main_menu();

    return 0;
}

void main_Menu()
{
    int choice;
    cout << "-------------------------------------------------------------" << endl;
    cout << "                       WELCOME TO                            " << endl;
    cout << "                             BANK                                  " << endl;
    cout << "-------------------------------------------------------------" << endl;


    cout << "1.\t LOGIN" << endl;
    cout << "2.\t FORGOT PASSWORD" << endl;
    cout << "Enter your choice (1-2)" << endl;
    cin >> choice;
    while (choice <0 || choice >3) // Validate input
    {
        cout << "INVALID INPUT, PLEASE ENTER 1 OR 2 ONLY\n";
        cin >> choice;
    }
    switch (choice) {
        case 1:  login_page(); // if user input '1' then call function login_page
            break;
        case 2: forgot_password(); // if user input '2' then call function forgot_password
            break;

    }
    void ClearScreen();
}

void login_page()
{
    string username, password;
    clearScreen();                              // call clear screen
    bool validateLogin;					// set initial value of validateLogin as false
    // declare string variables as input for argument login_Validatio
    do{								        			// do while syntax .. .
        cout << "Enter Username : \n";  				// prompt user to input username
        getline(cin,username);       			        // store nameLogin string
        cin.ignore();
        cout << "Enter Password : \n";	    			// prompt user to input username
        getline(cin,password);             				// store passLogin string
        cin.ignore();
        validateLogin = login_validation(username,password);

        if(validateLogin) 						// if validateLogin accept true value then proceed
        {
            clearScreen();							// clear screen
            account_Page();							// go to account_Page function
        }
        else
            cout << "INVALID INPUT. ENTER CORRECT USERNAME/PASSWORD!.\n"; //display if login input is false

    }while(!validateLogin); // keep repeating loop while login input isn't correctly entered.

}

void forgot_password()
{
    bool goOn=false; // initial value set to false
    int choice; // variable to store user's choice
    clearScreen(); // call clearScreen function
    const int MIN_VALUE = 1000000; // set min value of random-number generated
    const int MAX_VALUE = 9999999; // max value of random-numer generated
    int ticketNumber;             // declare ticket_Number variable
    unsigned seed = (unsigned int) time(0);        // seed generated by time

    srand(seed);

    ticketNumber = (rand() % (MAX_VALUE - MIN_VALUE + 1)) + MIN_VALUE; // formula to generate random-number
    cout << "--------------------------------------------\n";           // display
    cout << "          WE'VE MADE YOU A TICKET           \n";           // display
    cout << "\t  Ticket Number : " << ticketNumber << endl;            // display
    cout << "PLEASE KEEP THIS TICKET NUMBER TO CONTACT US.\n";          // display
    cout << "--------------------------------------------\n";           // display


    do {        // do start

        cout << "Enter 1 to Main Menu" << endl;     // display choice 1
        cout << "Enter 2 to Exit" << endl;          // display choice 2
        cin >> choice;                              // prompt user to enter choice;

        switch (choice){                            // switch statement start

            case 1 : {                              // case 1
                goOn = true;                        //
                clearScreen();                      // clear screen func
                main_Menu();                        // go to main_Menu()
                break;                              // break statement if case 1 fulfill
            }
            case 2 :{                               // case 2
                cout << "Exit Now.";                // display exit
                break;                              // break statement ifcase 2 fulfill
            }

            default : {                                             // default switch
                cout <<"INVALID INPUT. ENTER 1 OR 2 ONLY!.";        // display
                break;                                              // break
            }
        }

    }while (goOn);
}

bool login_validation(string usrName, string pass) {
    bool valid = false;
    string getUsrname, getPass, getID;
    ifstream data;
    data.open("dataLogin.txt");
    while (data >> getUsrname >> getPass >> getID) { 
        if (usrName == getUsrname && pass == getPass) {
            valid = true;
            break;
        }
    }

    if(valid){
        return true;
    }
    else{
        return false;
    }
    data.close();
}

void clearScreen() //function to clear the screen
{
    cout << string( 100, '\n' );
}

void account_Page(){ // start func account_Page()
    //AccountData logindata;
    int choice;
    ifstream userAccount;
    if(userAccount.good()){
        userAccount.open("filename.txt"); //ignore it (updte)
    }
    else {
        cout << "SOME FILES ARE MISSING. PLEASE CONTACT ADMINISTRATOR!.\n";
    }


    clearScreen();
    string usrName, usrBalance;
    getline(userAccount, usrName, '|');
    cout << "---------------------------------------------------\n";
    cout << "Hi, " << usrName << endl;
    cout << endl;
    cout << endl;
    cout << endl;
    cout << endl;
    cout << "---------------------------------------------------\n";


    cout << "1. CHECK BALANCE \n";
    cout << "2. FUNDS TRANSFER \n";
    cout << "3. SELECT PAYMENTS \n";
    cout << "4. ACCOUNT SETTINGS \n";
    cout << "5. LOG OUT\n";
    cout << "6. CANCEL \n";

    cout << "Enter your choice: \n";

    cin >> choice;

    userAccount.close();

} // end func account_Page()

void check_Balance(){
    clearScreen();
    ifstream accountDetails; // get balanceData from text file..

    cout << "Your Balance is : \n";

}

int cancel_Select(){

    cout << "Exit Now...";
    return 0;

}

Last edited on
account_Page() function can only be accessed if validateLogin(L.62) true,
the contents of dataLogin only contains username, password, and ID..

updt
maybe my problem is located in (L.129) the code seems can't read each of it. maybe each string separated by backspace?
Last edited on
my current problem is in line 129, the file seems can't read each string and store it into assigned variable

functions related to this case only login_validation() and login_Page()

dataLogin is the same with loginData(edited),
"dataLogin.txt" contains as below :

1
2
hooman90 pass 100002768
user secret 1000023422


problem is i want to read and store hooman90, pass, 100002768 in getUsrName,getPass,getID (l.129)
Last edited on
case closed, i finally get the solution, thanks for u sakura for saving my hours
Topic archived. No new replies allowed.