So this section of code is a small part of my program. It essentially reads through the file of user information which stores full names, bank balances, usernames and passwords.
My question is, why when I enter a username that is NOT in the file, it throws up an error when there is an if statement that says if username is not found, then go to the register method?
public static int player;
public static void Username_Check()
{
string[] str = File.ReadAllText(@"X:\btec computing\unit 1\C sharp\online_casino_prog\user_info.csv").Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
string[] users = new string[str.Length];
Console.WriteLine("Enter your username. ");
string username = Console.ReadLine();
bool user_found = false;
for (int i = 0; i < (str.Length); i++)
{
string[] person = str[i].Split(',');
if (person[2] == username)
{
Console.WriteLine("Welcome back {0}!", person[0]);
user_found = true;
player = i;
Password_Check();
}
}
if (user_found == false)
{
Console.WriteLine("Sorry, we could not find an account linked to the username '{0}', Please register an account with us! ",username);
Register();
}
}
This is the error i am getting:
Unhandled Exception: System.IndexOutOfRangeException: Index was outside the bounds of the array. at online_casino_prog.Program.Username_Check() in X:\btec computing\unit 1\C sharp\online_casino_prog\online_casino_prog\Program.cs:line 58 at online_casino_prog.Program.Main(String[] args) in X:\btec computing\unit 1\C sharp\online_casino_prog\online_casino_prog\Program.cs:line 30