0

This code never gives me the first option despite me following the correct prompts. I cant find what is wrong with this expression. It constantly gives the false option ( in this case "noooo"). What are other simple alternatives ( simple because I need it for school)

 string postalcode;
        int i = 0;


        postalcode = Console.ReadLine();

        Console.WriteLine(postalcode[0]);

        if (postalcode[0].CompareTo(i).ToString().Equals(0))
        {
            Console.WriteLine("YAY");
            Console.ReadLine();

        }
        else
        {
            Console.WriteLine("nooo");
            Console.ReadLine();



        }
E Khan
  • 1
  • 1
  • 3
  • 1
    You're comparing a character (presumably `'0'`) to a number `0`. These are different things. Your code should be `if (str[0].Equals('0'))` – Rob Nov 10 '17 at 06:52
  • Works! Thanks a lot. Also, is there a way I can have it that a certain character number MUST be a letter? Example: The 5th character must be any letter, and not an integer. – E Khan Nov 10 '17 at 06:56
  • Yes - but you should really search a bit first. [This](https://stackoverflow.com/questions/9975640/check-if-char-isletter) is the first result after searching for 'C# check if character is letter' – Rob Nov 10 '17 at 06:58
  • I would love to follow those examples but the issue is that we haven't "learned" it in school yet, so using any new or semi difficult function is not allowed. Is there an alternative, simple way. – E Khan Nov 10 '17 at 07:00

0 Answers0