0

I'm a student who just started learning C# and I want to know how to check to see if the password matches the username in a text file.

static void CheckExist()
        { 
            Console.WriteLine("Enter username: ");  //ask the user for the username and store in a variable 
            string EuserName = Console.ReadLine();
            Console.WriteLine("Enter password: "); //ask the user for the password and store in a variable 
            string EpassWord = Console.ReadLine();

            string [] lines = File.ReadAllLines(filename);  //read all lines of the file into an array
            bool UserExist = false;  //declares a Boolean variable to track if the user/password exists
            string[] x;

            foreach (string i in lines)//foreach string in the array
            {
                x = i.Split(","); //split the line on comma and store in an array X
                if (x[0] == EuserName && x[2] == EpassWord)  //if x index 0 equals the username and x index 2 equals the password
                {
                    UserExist = true; // user exists, set the Boolean variable and exit loop
                    break;
                }
            } //end of loop 
            if (UserExist == false)
            {
                Console.WriteLine("User doesn't exist");
            }

            //if the Boolean variable is still false the user didn't exist, otherwise they did

        }

This is the updated code, proved working

Tris
  • 169
  • 1
  • 11
  • 1
    You shouldn't split the whole text by a comma. Instead. Read individual lines (e.g., using `File.ReadAllLines()` and extract the usernames and passwords of each line). You could create a dictionary of usernames and passwords, then after finding the username that the user enters, check if the password matches the corresponding one. Side note: `condition1` and `condition2` are terrible variable names; try to choose more meaningful names. – 41686d6564 stands w. Palestine May 16 '21 at 04:05
  • 1
    Additionally, your file looks like a CSV format. You probably shouldn't try to parse it manually anyway. See [this post](https://stackoverflow.com/q/3507498/8967612) for ways to parse CSV files. – 41686d6564 stands w. Palestine May 16 '21 at 04:07
  • In the first `for` you can store the found line in a variable to check if the password exists on that line. This way you will check if the password is valid for the informed user. – Pedro Fernandes Filho May 16 '21 at 04:33
  • 2
    This is probably an execise only, but: **never store passwords in plain text**. Use salted hashes instead. – Klaus Gütter May 16 '21 at 04:47
  • 3
    @Klaus yes, but maybe we should graduate C# 101 before getting into stuff like that – Caius Jard May 16 '21 at 04:52
  • 1
    @KlausGütter thanks for the tip, I didn't know what salted hashes was before. But this will never actually be used, it is just an assessment task. If I was to work with users actual personal details, I would probably use Excel or Access. – Tris May 18 '21 at 10:31
  • 1
    1. looks like you are using {} to index... that doesn't look like C# to me. 2. Don't compare bools to "true" or "false". Use if (userExist) or if (!userExist). 3. Take care with naming your variables. userExist is not a good name. It is not checking if a user exists, but rather exists AND has provided a correct password. I would call it something like userPasswordOk. – JoelFan May 19 '21 at 04:14
  • 2
    @JoelFan thanks, I have fixed the syntax, and will think more about variable naming conventions in the future! – Tris May 19 '21 at 05:06

1 Answers1

0

Implement the following logic:

//ask the user for the username and store in a variable 
//ask the user for the password and store in a variable 

//read all lines of the file into an array

//declare a Boolean variable to track if the user/password exists

//foreach string in the array

  //split the line on comma and store in an array X

  //if x index 0 equals the username and x index 2 equals the password

    // user exists, set the Boolean variable and exit loop

//end of loop 

//if the Boolean variable is still false the user didn't exist, otherwise they did

This is what you should do when you code: write the logic out in the language you think in and then translate it to c#. From the code in your question you already know how to do all the above

Caius Jard
  • 72,509
  • 5
  • 49
  • 80