Reputation: 29
I'm trying to use the "while" function so everytime the user types a wrong username or password, the CMD tells the user either "Wrong username." or "Wrong password". I sort of did it now, the user can type it multiple times until they get the the correct answer, problem is that the CMD is not telling the user that the username/password they're using is wrong! I will post the whole code here:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Hello
{
class Program
{
static void Main(string[] args)
{
string name = "Michael";
string pass = "coolguy";
int time = 12;
string NameInput;
string PassInput;
string Exit = "Y";
string ExitInput;
Console.Write("\nPlease, enter the time: ");
time = int.Parse(Console.ReadLine());
if (time <= 12)
{
Console.WriteLine("\nGood Morning. It's " + time + " AM");
}
else if (time > 12)
{
Console.WriteLine("\nGood Evening. It's " + time + " PM");
}
Console.WriteLine("\nPlease, enter your name.");
do NameInput = Console.ReadLine();
while (NameInput != name);
Console.WriteLine("\n Welcome, Michael. Please, enter your password: ");
do PassInput = Console.ReadLine();
while (PassInput != pass);
Console.WriteLine("\nYou successfully have logged in.");
Console.WriteLine("\nType (Y) to log out.");
ExitInput = Console.ReadLine();
if (ExitInput == Exit)
{
return;
}
}
}
}
Upvotes: 0
Views: 28
Reputation: 29
Fixed it by nesting an "if" statement inside the "do-while" statement:
Console.WriteLine("\n Welcome, Michael. Please, enter your password: ");
do
{
PassInput = Console.ReadLine();
if(PassInput != pass)
{
Console.WriteLine("\nWrong password.");
}
} while (PassInput != pass);
Upvotes: 1