I was doing a project where you have to enter a password.
using System;
namespace program.cs
{
class Program
{
static void Main(string[] args)
{
bool checkpoint1 = false;
long password = 99101L;
Console.WriteLine("Write the Password to Access");
string keyPassword = Console.ReadLine();
if(keyPassword.Length != 0)
{
checkpoint1 = true;
}
else
{
Console.WriteLine("The Password you entered was blank.");
}
if (checkpoint1)
{
long key = Convert.ToInt64(keyPassword);
if (key == password)
{
Console.WriteLine("Access Granted!");
}
else
{
Console.WriteLine("Password Incorrect! \n Access Denied");
}
}
}
}
}
But when I enter a string as the password it throws an error that on the long key = Conver.ToInt64(keyPassword)
line, that it cannot change to long, so how can I solve this to that it prints that the type is a string, and if I try the getType and typeof() it, it will show a string no matter what as readline always takes in a string. Please help.
Edit: I solved it, end result code:
using System;
namespace program.cs
{
class Program
{
static void Main(string[] args)
{
bool checkpoint1 = false;
long password = 99101L;
Console.WriteLine("Write the Password to Access");
string keyPassword = Console.ReadLine();
if(keyPassword.Length != 0)
{
checkpoint1 = true;
}
else
{
Console.WriteLine("The Password you entered was blank.");
}
if (checkpoint1)
{
if(!long.TryParse(keyPassword, out var key))
{
Console.WriteLine("The Password you entered is not a number, please try again");
}
else
{
long Realkey = Convert.ToInt64(keyPassword);
if (Realkey == password)
{
Console.WriteLine("Access Granted!");
}
else
{
Console.WriteLine("Password Incorrect! \n Access Denied");
}
}
}
}
}
}