0

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");
                    }
                }
            }
        }

    }
}
Yung
  • 83
  • 1
  • 2
  • 10
  • Did you check the [documentation](https://learn.microsoft.com/en-us/dotnet/api/system.convert.toint64?view=net-5.0#System_Convert_ToInt64_System_String_) for clues? – Nicholas Hunter Apr 12 '21 at 11:29
  • "_If you prefer not to handle an exception if the conversion fails, you can call the [Int64.TryParse](https://learn.microsoft.com/en-us/dotnet/api/system.int64.tryparse?view=net-5.0) method instead. It returns a Boolean value that indicates whether the conversion succeeded or failed._" – Self Apr 12 '21 at 11:30
  • https://stackoverflow.com/questions/199470/whats-the-main-difference-between-int-parse-and-convert-toint32 – Self Apr 12 '21 at 11:35

1 Answers1

3

You can use long.TryParse:

 string keyPassword = Console.ReadLine();

 if (string.IsNullOrEmpty(keyPassword)) {
   Console.WriteLine("The Password you entered was blank.");
 } 
 else if (!long.TryParse(keyPassword, out var key)) {
   Console.WriteLine("The Password you entered is not a valid integer.");
 }  
 else if (key != password) {
   Console.WriteLine("Password Incorrect! \n Access Denied");
 }
 else { // Ugh, finally...
   Console.WriteLine("Access Granted!");
 }
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
  • but it is giving an error, ```Severity Code Description Project File Line Suppression State Error CS0136 A local or parameter named 'key' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter program.cs C:\Users\win 10\Desktop\C#\program.cs\program.cs\Program.cs 31 Active``` – Yung Apr 12 '21 at 13:31
  • Key is a variable – Yung Apr 12 '21 at 13:33
  • Now it is solved, I just changed ```key``` to ```Realkey``` – Yung Apr 12 '21 at 13:35