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();
}
}
}
}