0

I'm new to C# and programming.

I tried everything but I'm too stupid to convert the string "password" to an int. This is my failing code:

namespace idk
{
    public static class Program
    {
        public static void Main()
        {
            var password = Console.ReadLine();

            int gues = 0;
          
            do
            {
                gues = gues + 1;
            } while (gues == password);
            
            Console.WriteLine("done");
        }
    }
}
Jerry Stratton
  • 3,287
  • 1
  • 22
  • 30
  • 1
    What value do you want to convert "password" to?? Do you want to **(1)** determine the **length** of the string? **(2)** Do you want to calculate some kind of a checksum over the string entered? **(3)** something else entirely?? You're not being very clear about your goals.... a string is multiple characters - what `int` do you want to convert these to?? – marc_s Apr 22 '22 at 14:13
  • what **is** `password`? What do your type into you app? I can't imagine `"Hello World"` to be convertible to an `int`, so you seem to have some hidden knowledge about your data that we do not have. – MakePeaceGreatAgain Apr 22 '22 at 14:18

3 Answers3

0

System.Convert.ToInt32(password)

MS Reference

You can then compare gues (int) with password (int)

Fábio
  • 477
  • 5
  • 12
0

Use TryParse(String, Int32) Converts the string representation of a number to its 32-bit signed integer equivalent. A return value indicates whether the conversion succeeded. Also be aware that normally passwords should be stored in SecureString type avoiding conversion to String.

Andrii
  • 21
  • 4
0

Best thing is only to allow numbers to begin with if you want numbers anyway. See following answer for some reference how to do it:

https://stackoverflow.com/a/38737410/18056986