Logging in using a file

I'm attempting to make a function that reads a user ID from user input, checks a file for that user ID, and logs them in if it is found. No password.

I have it somewhat working, but it seems to only work with the very first user ID, and it also isn't quite how it should be.

This is what I'm doing currently:

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
  
bool logIn(string &login, bool found)
{
	fstream accounts;
	accounts.open("customers.txt");
	for (int i = 0; !accounts.eof(); i++)
	{
		getline(accounts, user.userID, ' ');  //Reads up to a space, should go to next line after but I don't know how to do that
		cout << "Line 22. login = " << login << ", user.userID = " << user.userID << endl; //Test code. Tells me what's what and when
		if (login == user.userID)
		{
			found = true;
			return true;
		}
		else
                ; //Should point to next line? not sure.
		
	}
	if (!found)
	return false;
}

int main()
{
//stuff leading up to this section of code

i = 0;
		do {
			cout << "Enter your four-digit user ID: ";
			cin >> login;
			cout << endl;
			found = logIn(login, found);
			if (found == true)
				cout << "Successfully logged in as " << login << "." << endl;
			else {
				cout << "Failed to log in, " << 2 - i << " attempts remaining." << endl;
				i++;
			} 
		} while (i < 3 && found == false);

 //stuff that happens if found is true
}


"customers.txt" reads as follows:

1111 John Doe 11 MyStreet MyCity 12345 NY 23000 200
1112 Kevin Moe 23 HisStreet HerCity 54321 MA 15 3500
1113 Mary Joe 78 SomeStreet NewTown 32451 PA 6500 12000

If my input is 1111, it works and logs me in as John Doe. Any other input doesn't work. It also currently reads the whole line while !accounts.eof() (ex. user.userID = 1111 first run, then John second run.) I want it to read the first 4 digits and skip the rest, which I think will fix it.

Sorry this is all over the place, I don't really know if I'm formulating this right.
Last edited on
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
#include <iostream>
#include <string>
#include <fstream>
#include <limits>

bool login( const std::string& login_key )
{
    static const char* file_name = "customers.txt" ;

    std::cout << "login key: " << login_key << '\n' ; // test code

    std::ifstream file(file_name) ; // open the file for input

    std::string user_id ;
    int line_number = 0 ; // test code
    while( file >> user_id ) // for each line in the file, read up to the first white space
    {
        std::cout << "    line #" << ++line_number << " got user id: " << user_id << '\n' ; // test code

        if( login_key == user_id )
        {
            std::string tail ; // test code
            std::getline( file, tail ) ; // test code
            std::cout << "    found user. tail: " << tail << '\n' ; // test code

            return true ; // found it, return true
        }

        // didn't find it in this line; throw away the rest of the line (move to the next line)
        // https://en.cppreference.com/w/cpp/io/basic_istream/ignore
        file.ignore( std::numeric_limits<std::streamsize>::max(), '\n' ) ;
    }

    return false ; // reached the end of stream without finding the user id
}
Last edited on
Topic archived. No new replies allowed.