0

I'm very new to programming but im trying to create a very basic log in for my program. Safety is not a concern I just want it to allow me to continue through to the main menu if the input userName and password matches with whatever exists within the List.

The list is a list of a class that contains username and password

The first if statement Register(); sends to a method where you can enter a new username and password and that is saved within the List <Loginuser>

In the if system below it wont let me pass no matter what, and when i tried a Do while loop, it sends me through no matter what.

Thanks a lot for any pointers to why I can't seem to compare the input to the List

static void Main(string[] args)
{
    List<Loginuser> Loginuser = new();

    Loginuser.Add(new Loginuser { userid = "JJ", userpass = "123" });
    Loginuser.Add(new Loginuser { userid = "Kepa", userpass = "HP" });

    Loginscreen();

    void Loginscreen()
    {
        Console.WriteLine("1.Register Account\n2.Log in");
        int val = Convert.ToInt32(Console.ReadLine());
        if (val == 1)
        {
            Register();
        }
        else if (val == 2)
        {
            Login();
        }

        void Register()
        {
            Loginuser Loguser = new Loginuser();

            Console.WriteLine("Write in your new username");
            Loguser.userid = Console.ReadLine();
            Console.WriteLine("Write in your new password");
            Loguser.userpass = Console.ReadLine();

            Loginuser.Add(Loguser);


            Console.WriteLine("User is registered, press Enter to return");
            Console.ReadLine();
            Loginscreen();
        }

        void Login()
        {
            Console.WriteLine("Welcome");
            Console.Write("Enter Username: ");
            string userName = Console.ReadLine();
            Console.Write("Enter Password: ");
            string passWord = Console.ReadLine();

            if (userName == Loginuser && passWord == Loginuser)
            {
                Console.WriteLine("Welcome" + userName);
                Console.ReadLine();
                MainMenu();
            }
            else
            {
                Console.WriteLine("press enter to try again");
                Console.ReadLine();
                Login();
            }
        }
    }
}
vernou
  • 6,818
  • 5
  • 30
  • 58
Jobosan
  • 3
  • 1
  • Welcome to Stack Overflow! This is a good opportunity for you to start familiarizing yourself with [using a debugger](https://stackoverflow.com/q/25385173/328193). When you step through the code in a debugger, which operation first produces an unexpected result? What were the values used in that operation? What was the result? What result was expected? Why? To learn more about this community and how we can help you, please start with the [tour] and read [ask] and its linked resources. – David Oct 27 '22 at 18:02

1 Answers1

0

You are comparing a string against a list. userName and passWord are strings, Loginuser is a list of Loginuser objects (you should consider renaming the list to prevent confusing it with the Loginuser type). You want to check if any Loginuser object in your Loginuser list has a username and password matching the provided ones. Try iterating through the list and then checking for equality against those two properties of the object. For example using LINQ:

bool isAllowedToLogIn = Loginuser.Any(user => user.userId == userName && user.userpass == passWord)

Some side notes:

  • LINQ allows you to put potentially lengthy loop-operations into sometimes even a single line of code while also preserving readability. So the long version of the code above would be
// for better readability you should consider renaming your 
// Loginuser list to Loginusers or sth like this (plural)
bool isAllowedToLogIn = false;
foreach (Loginuser user in Loginuser) 
{
    if (user.userId == userName && user.userpass == passWord)
    {
        isAllowedToLogIn = true;
        break;
    }
}
  • You are using == for string comparison. While this is technically fine and does work, it is more common to use myString.Equals(someOtherSting) because strings are reference types (the managed .NET equivalent of pointers). For strings == does work for comparing the values of two strings (because the == operation was manually defined to do so by the C# language team), but for other reference types you'd be checking whether they are located in the same memory region (are the same object), while you probably actually want to compare their values.
  • == is commonly used to compare value types or so-called structs like most numeric types (int, double, decimal, long, etc) and booleans.

I'd advise you give the C# reference a quick read :)

Frederik Hoeft
  • 1,177
  • 1
  • 13
  • 37
  • That's very useful, thank you very much. Yes my problem was to figure out how to compare the string with a list but I just couldnt figure out what to, but this solved my problem! Cheers – Jobosan Oct 27 '22 at 18:30
  • If this answer solves your question consider marking it as the [accepted answer](https://stackoverflow.com/help/accepted-answer) this way other people know that your problem has been resolved :) – Frederik Hoeft Oct 27 '22 at 18:37