I know I'm troublesome because I always ask about a topic but I'm really don't understand it. This time, it's about login account. So this function of mine work pretty well with the first succeed login, but if the first in a success, the second is always a success too, even if the pass or username is incorrect. I think it's about bool variables but not really sure.
Lots of things wrong here.
- Lots and lots of globals which you didn't show, so we don't know if they're right. Globals should be avoided.
- L1: Not clear what i is for. Poor variable name, or at least include a comment explaining i.
- L6: If file doesn't open, you continue as if everything is okay.
- L12: What is a for? Poor variable name.
- Use of hard coded constant 50 in many places. Presumably this is the max number of players, but that's not clear. Should use a constexpr.
- L14: This whole if brackect is unnecessary if you return false] after line 7.
- Line 18: Why is line global? Looks like an array of char, not a std::string. Variables should be as local as possible.
- L20: Looks like you're comparing char by char. You can compare two std:strings directly.
- L28: You don't need to use memcpy if you're using using std:string.
- L55: If you call login multiple times, does loginsuccess get reset before each call? login should return a bool and not set a global.
L78: Why are you calling login_menu after saying it's an invalid account? Does login_menu call login again? This would be recursive. You don't want login to be recursive.
Where do you keep track of the number of unsuccessful login attempts for a user?
I'm somewhat confused. Are you writing in C or C++? This sure looks like C code with the exception of cin and cout.
I'm so sorry if it's that unclear. My program is a lot longer. I found my program only wrong in this part so I think it would be easier for you to check T_T. I'm writing in c++ and my lecturer said I can only use char because I'm learning Introduction to Programming and it's better to understand the logic
L19-20. You're comparing 50 chars of both variables (are these c-style null-terminated?) - no matter how many of these chars are actually used. How are line and username defined? Are they null value initialised? If they are not, there's no expectation that the chars following the null terminator are going to be equal. If c-style strings, then use strcmp() to compare. If these are std::string, then you don't need the j loop and can just use == to compare.
Same for L36-37
L28 - use strcpy() and not memcpy()
The function should return bool - true if logged in or false otherwise. l55-70 should be in the caller function. There's no need to have loginsucess - as that is the return value.
What is L16 for? The maximum number of login attempts? If yes, then have 50 as a const with a name. The same for 50 used elsewhere (maximum size of array?). Make it a const with a name.