0

So i am making a program that allows you to register and then login using console. Since a console can only store data inside of it as long as it is not closed(as far as I know) I make use of .txt files . I have written some code, when you are logging in it's supposed to check if someone else is already using the username you want but the problem is, for example if someones username is "Manly" and the next person wants their name to be "Man" it says the username already exists because "Man" exists inside (Man-"ly")"Manly" if you know what I'm saying.

This is a class inside my VS project.

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace RegisterAndLogin
{
    class Register
    {
        public string UserName { get; set; }
        public string Password { get; set; }

        public void Registration()
        {
            Console.WriteLine("Hello user! Enter the username you want to register with.");
            UserName = Console.ReadLine();

            string UserNamesInFile = File.ReadAllText(@"C:\Users\KIRCA\Documents\C# with Denis Panjuta\Usernames.txt");
            if (UserNamesInFile.Contains(UserName))
            {
                Console.WriteLine("The username you entered is already being used. Try a new one.");
                while (UserNamesInFile.Contains(UserName))
                {
                    Console.WriteLine("Hello user! Enter the username you want to register with.");
                    UserName = Console.ReadLine();
                }
            }

            Console.WriteLine("Now enter the password you want to register with.");
            Password = Console.ReadLine();

            using (StreamWriter file = new StreamWriter(@"C:\Users\KIRCA\Documents\C# with Denis Panjuta\Usernames.txt", true))
            {
                file.WriteLine(UserName);
            }

            using (StreamWriter file1 = new StreamWriter(@"C:\Users\KIRCA\Documents\C# with Denis Panjuta\Passwords.txt", true))
            {
                file1.WriteLine(Password);
            }

            Console.WriteLine("You are succesfully registered");
            Console.WriteLine("-----------------------------------------------------------------------");   

            
        }
    }
}

I reccomend copy pasting the code into a net.framework, console c#, VS project and checking it out to understand what I'm saying.

Here is the rest of the code I have.

Class called Login:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace RegisterAndLogin
{
    class Login
    {
        public string LoginUserName { get; set; }
        public string LoginPassword { get; set; }
        public void LoginMethod()
        {
            Console.WriteLine("Hello again user! Enter your username.");
            LoginUserName = Console.ReadLine();

            string UserNameFileContents = File.ReadAllText(@"C:\Users\KIRCA\Documents\C# with Denis Panjuta\Usernames.txt");

            if (UserNameFileContents.Contains(LoginUserName))
            {
                Console.WriteLine("Username is correct. ");
            }
            else
            {
                Console.WriteLine("Username is incorrect! ");
            }

            Console.WriteLine("Now enter your password. ");
            LoginPassword = Console.ReadLine();

            string PasswordFileContents = File.ReadAllText(@"C:\Users\KIRCA\Documents\C# with Denis Panjuta\Passwords.txt");

            if (PasswordFileContents.Contains(LoginPassword))
            {
                Console.WriteLine("Password is also correct. Press any key to continue to homepage. ");
                Console.ReadLine();
            }
            else
            {
                Console.WriteLine("Password is incorrect. ");
            }
        }
    }
}

Code for Main program, program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RegisterAndLogin
{
    class Program
    {
        static void Main(string[] args)
        {
            Register registration = new Register();
            Login login = new Login();
            Console.WriteLine("Enter 1 to register or enter 2 to login.");
            string UserNumberInput = Console.ReadLine();
            if(UserNumberInput == "1")
            {
                registration.Registration();

                Console.WriteLine("You may now login.");
                login.LoginMethod();
            }
            else if(UserNumberInput == "2")
            {
                login.LoginMethod();
            }
            else
            {
                Console.WriteLine("Enter 1 or 2 only.\nNumber or letter is invalid");
                while(UserNumberInput != "1" && UserNumberInput != "2")
                {
                    Console.WriteLine("Enter 1 to register or enter 2 to login.");
                    UserNumberInput = Console.ReadLine();
                    Console.WriteLine("--------------------------------------------------");
                }
                if (UserNumberInput == "1")
                {
                    registration.Registration();

                    Console.WriteLine("You may now login.");
                    login.LoginMethod();
                }
                else if (UserNumberInput == "2")
                {
                    login.LoginMethod();
                }
            }

            Console.ReadLine();
            
        }
    }
}```

  • how about changing `if (UserNamesInFile.Contains(UserName)` to an equals comparison (ignoring upper/lower ?) `if(UserNamesInFile.Equals(UserName, StringComparison.OrdinalIgnoreCase))` ? – kshkarin Oct 12 '20 at 16:39
  • For future questions please review [MCVE] guidance on posting code - there is really no need to post so much code. – Alexei Levenkov Oct 12 '20 at 16:53
  • This is potentially closed prematurely as I do not believe that the problem is solely due to the fact that the user is using a .Contains(), where he should be using a .Equals(). It also has to do with the method in which the usernames are being read. You change the method for which you are reading them and your problem gets narrowed down and easily solved with swapping the .Contains() to a .Equals(). You can't do a read all text and then call a .Contains() on that string to check for duplicates. It will not produce the results you are looking for. Seek an alternative way to read them in – ReRoute Oct 19 '20 at 01:47

0 Answers0