This is the task I need to finish:
Create a program that asks you to enter a password. If you enter an error, it must be reported and you can try again. The program only ends when the correct password is entered. You can then expand the program by telling how many times you have tried to log in
Where I am at right now:
string losenord;
Console.WriteLine(“Enter password to log in: “);
losenord = Console.ReadLine();
if (losenord == “Password123”)
{
Console.WriteLine(“YOU HAVE LOGGED IN”);
}
else
{
Console.WriteLine(“WRONG PASSWORD! TRY AGAIN”);
}
How do I :
Let the program try again
and tell the user how many times they failed
You’ve made a good start.
In the ‘Useful Links’ sidebar on this subreddit there are links to lots of resources for beginners – C# Fundamentals: Development for Absolute Beginners would be a good place to start.
You will need to understand do-while loops, integers (including addition and/or incrementing) and string formatting and/or interpolation.
string losenord; Console.WriteLine(“Enter password to log in: “); losenord = Console.ReadLine();
bool successfullyLoggedIn = false; int retryCount = 0;
while (!successfullyLoggedIn) { if (losenord == “Password123”) { Console.WriteLine(“YOU HAVE LOGGED IN”); successfullyLoggedIn = true; // loop ends } else { retryCount++; Console.WriteLine(“Failed “ + retryCount.ToString() + “ times. ! TRY AGAIN”); } }
Happy cake day
Thank you, and happy cake day 🙂
When I use this code the (Failed “” times ! TRY AGAIN) never ends
C# devs
null reference exceptions