Skip to content

Instantly share code, notes, and snippets.

@Miesvanderlippe
Created March 10, 2018 09:13
Show Gist options
  • Save Miesvanderlippe/aa7c1bd902af97d93314fd3dbaca6ea6 to your computer and use it in GitHub Desktop.
Save Miesvanderlippe/aa7c1bd902af97d93314fd3dbaca6ea6 to your computer and use it in GitHub Desktop.
using System;
namespace LoginFunction
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("How many users will you register? ");
// You can move this declaration to the " Convert.ToInt32(Console.ReadLine());" line instead. There's no need to have it initialised before that.
int users;
// isFound is never used
bool loop = true, isFound = false;
// These vars are only used in the for-loop and should be declared there.
string[] username, password;
// No need to declare this here. It's only used in the while loop and should be declared in that scope.
string tempusername, temppassword;
// What happens if I try to register 11 users? You should move this to after you ask how many users you want and use that number for the index.
// The names are bad because they should be plurar. It's usernames and passwords.
username = new string[10];
password = new string[10];
// This will fail if you try and give it anything but a valid number. -1 is a valid number and will break your code.
users = Convert.ToInt32(Console.ReadLine());
for (int x = 0; x < users; x++)
{
Console.WriteLine("Please enter the user's name: ");
username[x] = Console.ReadLine();
Console.WriteLine("Please enter the user's password:");
password[x] = Console.ReadLine();
Console.WriteLine("User {0} has been registered at index of {1}", username[x], x);
}
Console.WriteLine("Please test a few of the user accounts?");
// The users > 0 should be an if-statement but you should just not allow for this situation by checking the amount of users earlier on.
while (users > 0 && loop)
{
Console.WriteLine("Enter the user's name:");
tempusername = Console.ReadLine();
Console.WriteLine("Enter the user's password:");
temppassword = Console.ReadLine();
// See how calling your arrays username and password is confusing now?
if (Program.DoubleArrayChecker(username, password, tempusername, temppassword))
{
Console.WriteLine("Pass correct");
}
else
{
Console.WriteLine("Pass incorrect");
}
Console.WriteLine("try again? 1 - Yes, 2 - No");
// Why recycle the temppassword var for this? This is a very bad practice!
temppassword = Console.ReadLine();
// The spacing between your ifs is confusing.
if (temppassword == "2")
{
loop = false;
}
else if (temppassword == "1")
{
// Loop is true at this point regardless.
loop = true;
}
else
{
// Why is the default to break?
loop = false;
}
}
}
public static bool DoubleArrayChecker(string[] users, string[]passes, string username, string password)
{
var index = 0;
// I check this because if you give two inequal arrays the code would fail when one array runs out of values before the other.
// Substract 1 because arrays start at 0.
var smallestArray = Math.Min(users.Length, passes.Length) - 1;
do
{
if (users[index] == username && passes[index] == password)
{
return true;
}
index++;
} while (index <= smallestArray);
return false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment