[csharp] User Login

Viewer

copydownloadembedprintName: User Login
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace ConsoleApp3
  8. {
  9.     internal class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             const string USER_IDENTIFICATION = "admin";
  14.             const string USER_SECRET_WORD = "123";
  15.             int trials = 3;
  16.             string entered_username, entered_password;
  17.             bool found = false;
  18.             while(trials > 0 && found == false)
  19.             {
  20.                 Console.WriteLine("Enter username: ");
  21.                 entered_username = Console.ReadLine();
  22.                 if(entered_username != USER_IDENTIFICATION) {
  23.                     Console.WriteLine("User name is not correct!");
  24.                     trials--;
  25.                     Console.WriteLine($"Trials left: {trials}");
  26.                     continue;
  27.                 }
  28.                 Console.WriteLine("Enter password: ");
  29.                 entered_password= Console.ReadLine();
  30.                 if(entered_password!= USER_SECRET_WORD)
  31.                 {
  32.                     Console.WriteLine("Password is not correct!");
  33.                     trials--;
  34.                     Console.WriteLine($"Trials left: {trials}");
  35.                     continue;
  36.                 }
  37.                 found = true;
  38.             }
  39.             if (found ==  true) {
  40.                 Console.WriteLine($"Successfully logged in as: {USER_IDENTIFICATION} in {3 - trials} tries");
  41.             }
  42.             else
  43.             {
  44.                 Console.WriteLine("Invalid login!");
  45.             }
  46.             Console.ReadKey();
  47.         }
  48.     }
  49. }
  50.  

Editor

You can edit this paste and save as new:


File Description
  • User Login
  • Paste Code
  • 22 Feb-2024
  • 1.64 Kb
You can Share it: