0

I have to find the square of a number / 12345 / - it's done. I wanted to make the program a bit more complicated and I have this:

using System;

class Program
{
    static void Main()
    {
        Console.WriteLine("The square of the number 12345 is");
        Console.WriteLine(Math.Sqrt(12345));
        Console.WriteLine("Enter a number to calculate the square:");
        int numVal = int.Parse(Console.ReadLine());
        Console.WriteLine("The square of your number is" + " " + Math.Sqrt(numVal));
        Console.WriteLine("Do you wish to enter another number? Yes / No");
        string input = Console.ReadLine();
            if (input == "Yes")
            {
                Console.WriteLine("Enter a number to calculate the square:");
                int newNum = int.Parse(Console.ReadLine());
                Console.WriteLine("The square of your number is" + " " + Math.Sqrt(newNum));
            }
            else
            {
                Console.WriteLine("Have a nice day!");
            }

    }
}

Now a have this problem: when the program asks if I want to enter another number, the answer should be with capital letter / Yes, No /. Is there a way to make it work even if I enter the answear with lower case / yes, no /?

Neuron
  • 5,141
  • 5
  • 38
  • 59
test player
  • 333
  • 4
  • 14

3 Answers3

2

As per your input, below line will react.

if(string.Equals(input, "Yes", StringComparison.CurrentCultureIgnoreCase))
{
  // Your stuffs
}

or

if(string.Equals(input, "Yes", StringComparison.OrdinalIgnoreCase))
{
  // Your stuffs
}

Note: OrdinalIgnoreCase compares the character codes without cultural aspects. This is good for exact comparisons, like login names, but not for sorting strings with unusual characters like é or ö. This is also faster because there are no extra rules to apply before comparing.

For more info : Go Here or here

gkrishy
  • 756
  • 1
  • 8
  • 33
  • I wouldn't suggest Ordinal (better Invariant if English string is hardcoded and always CurrentCulture if it may be localized) but not doubt **this is the only right answer**. – Adriano Repetti Jul 28 '15 at 12:55
1

you can try:

...
string input = Console.ReadLine();
            if (input.ToUpper() == "YES")
            {
                ...
apomene
  • 14,282
  • 9
  • 46
  • 72
  • Thank you! It worked just fine and I learned something new. I allways search for the fastest and easiest way to make my program work. :) – test player Jul 28 '15 at 12:47
  • I will keep that in mind, thank you very much, Adriano! – test player Jul 28 '15 at 12:53
  • @NitinSawant, sorry I cant really understand your comment. ..My answer is straight forward and answers the Q directly... – apomene Jul 28 '15 at 13:12
  • @apomene _problem_ of `ToUpper()` comparison is that it isn't _locale-safe_. It's perfectly OK for an en-US toy example, OP is satisfied and his code works. However it'll fail in real world if your application is internationalized. Discussion about this issue can be incredibly long but just imagine you have a command `"include"`. Now imagine you deploy your application in a Turkish locale. You'll see that `"include".ToUpper() == "INCLUDE"` will fail. Turkish example is somehow _typical_, if you have time you may have fun reading [this post](http://stackoverflow.com/a/27229590/1207195). – Adriano Repetti Jul 28 '15 at 14:06
-2
string.Equals(input, "Yes", StringComparison.OrdinalIgnoreCase)
Chris Bohatka
  • 363
  • 1
  • 4
  • 14
  • Using ordinal comparison (instead of CurrentCulture or InvariantCulture, depends on context) you vanish half of the benefits of String.Equals() and it's no better than a naive ToUpper(). – Adriano Repetti Jul 28 '15 at 12:53