Guest User

Untitled

a guest
Mar 19th, 2018
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.96 KB | None | 0 0
  1. using System;
  2.  
  3. namespace LoginFunction
  4. {
  5. class Program
  6. {
  7. static void Main(string[] args)
  8. {
  9. Console.WriteLine("How many users will you register? ");
  10.  
  11. // You can move this declaration to the " Convert.ToInt32(Console.ReadLine());" line instead. There's no need to have it initialised before that.
  12. int users;
  13.  
  14. // isFound is never used
  15. bool loop = true, isFound = false;
  16.  
  17. // These vars are only used in the for-loop and should be declared there.
  18. string[] username, password;
  19.  
  20. // No need to declare this here. It's only used in the while loop and should be declared in that scope.
  21. string tempusername, temppassword;
  22.  
  23. // 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.
  24. // The names are bad because they should be plurar. It's usernames and passwords.
  25. username = new string[10];
  26. password = new string[10];
  27.  
  28. // This will fail if you try and give it anything but a valid number. -1 is a valid number and will break your code.
  29. users = Convert.ToInt32(Console.ReadLine());
  30.  
  31. for (int x = 0; x < users; x++)
  32. {
  33. Console.WriteLine("Please enter the user's name: ");
  34. username[x] = Console.ReadLine();
  35.  
  36. Console.WriteLine("Please enter the user's password:");
  37. password[x] = Console.ReadLine();
  38.  
  39. Console.WriteLine("User {0} has been registered at index of {1}", username[x], x);
  40. }
  41.  
  42. Console.WriteLine("Please test a few of the user accounts?");
  43. // 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.
  44. while (users > 0 && loop)
  45. {
  46. Console.WriteLine("Enter the user's name:");
  47. tempusername = Console.ReadLine();
  48. Console.WriteLine("Enter the user's password:");
  49. temppassword = Console.ReadLine();
  50.  
  51. // See how calling your arrays username and password is confusing now?
  52. if (Program.DoubleArrayChecker(username, password, tempusername, temppassword))
  53. {
  54. Console.WriteLine("Pass correct");
  55. }
  56. else
  57. {
  58. Console.WriteLine("Pass incorrect");
  59. }
  60.  
  61. Console.WriteLine("try again? 1 - Yes, 2 - No");
  62. // Why recycle the temppassword var for this? This is a very bad practice!
  63. temppassword = Console.ReadLine();
  64.  
  65. // The spacing between your ifs is confusing.
  66. if (temppassword == "2")
  67. {
  68. loop = false;
  69. }
  70.  
  71. else if (temppassword == "1")
  72. {
  73. // Loop is true at this point regardless.
  74. loop = true;
  75. }
  76.  
  77. else
  78. {
  79. // Why is the default to break?
  80. loop = false;
  81. }
  82. }
  83. }
  84.  
  85. public static bool DoubleArrayChecker(string[] users, string[]passes, string username, string password)
  86. {
  87. var index = 0;
  88. // I check this because if you give two inequal arrays the code would fail when one array runs out of values before the other.
  89. // Substract 1 because arrays start at 0.
  90. var smallestArray = Math.Min(users.Length, passes.Length) - 1;
  91.  
  92. do
  93. {
  94. if (users[index] == username && passes[index] == password)
  95. {
  96. return true;
  97. }
  98.  
  99. index++;
  100.  
  101. } while (index <= smallestArray);
  102.  
  103. return false;
  104. }
  105. }
  106. }
Add Comment
Please, Sign In to add comment