-1

I am trying to make a login programme that reads and writes from a textfile. For some reason, only the first line of the textfile works but the rest wont be successful login.

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

bool loggedIn() {
    string username, password, un, pw;
    cout << "Enter username >> "; cin >> username;
    cout << "Enter password >> "; cin >> password;

    ifstream read("users.txt");
    while (read) {
        getline(read, un, ' ');
        getline(read, pw);
        if (un == username && pw == password) {
            return true;
        }
        else {
            return false; 
        }
    }
}

Text File:

user1 pass1
user2 pass2

Alternatives I tried:

read.getline(un, 256, ' ');
read.getline(pw, 256);
junie
  • 11
  • 2
  • Regarding your problem. look at the code: how do you *not* `return` in the first iteration, and therefore advance to the second, etc. ? That loop... isn't. You either `return true;` or `return false;`. Lose the `else` block entirely and just put a naked `return false;` right before the closing `}` of the function. Unrelated, your "records" are per line, so read the entire line into a single string, then use formatted extraction via a `std::istringstream`, primed with the read-line, to pull the user id and pwd. – WhozCraig May 17 '22 at 19:30
  • @WhozCraig thank you so much i am really dumb at 3 am – junie May 17 '22 at 19:35
  • 1
    Have you tried running your code line by line in a debugger while monitoring the values of all variables, in order to determine at which point your program stops behaving as intended? If you did not try this, then you may want to read this: [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/12149471) You may also want to read this: [How to debug small programs?](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – Andreas Wenzel May 17 '22 at 20:57

1 Answers1

1

while (read) is the same as while (!read.fail()), which is the wrong loop condition to use in your situation. You are not checking if both getline() calls are successful before comparing the strings they output.

You also need to move the return false; statement out of the loop. Since you have a return in both the if and else blocks, you are comparing only the 1st user in the file and then stopping the loop regardless of the result. You want to keep reading users from the file until a match is found or the EOF is reached.

Try this instead:

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

bool loggedIn() {
    string username, password, un, pw;
    cout << "Enter username >> "; cin >> username;
    cout << "Enter password >> "; cin >> password;

    ifstream read("users.txt");
    while (getline(read, un, ' ') && getline(read, pw)) {
        if ((un == username) && (pw == password)) {
            return true;
        }
    }

    return false; 
}

Alternatively, use 1 call to std::getline() to read an entire line, and then use std::istringstream to read values from the line, eg:

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>

using namespace std;

bool loggedIn() {
    string username, password, un, pw, line;
    cout << "Enter username >> "; cin >> username;
    cout << "Enter password >> "; cin >> password;

    ifstream read("users.txt");
    while (getline(read, line)) {
        istringstream iss(line);
        if ((iss >> un >> pw) && (un == username) && (pw == password)) {
            return true;
        }
    }

    return false; 
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770