sylvann sylvann - 12 days ago 5
C# Question

Simple C# login with 3 attempts

I need to create a simple C# Sharp program that takes userid and password as input (type string). After 3 wrong attempts user should be rejected.

I have started but I'm not sure how the logic should be properly done.

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

namespace UserId
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Type username");
String UserId1 = Console.ReadLine();
Console.WriteLine("Type password");
String Pass = Console.ReadLine();

String UserIdCorrect = "test1";
String PassCorrect = "password1";
int MaxAttempts = 3;



Console.ReadKey();

if (UserId1 != UserIdCorrect && Pass != PassCorrect ) {
MaxAttempts++;

}


Console.ReadKey();

}
}
}

C4u C4u
Answer

I guess you are a beginner. I've commented the code.

int maxAttempts = 3;

// looping n (maxAttempts) times
for(int i = 0; i < maxAttempts; i++)
{
    // get input and check it
}

// do what ever you want here.
// at least show up a message

Many ways. As HebeleHododo commented you could also use a while-loop and check with if-else if your maxAttempts is reached.