-1

I've been having some trouble with my code, where should i place my ToUpper(); method in order to take effect?

using System;

namespace Test_1
{
    class Program
    {
        static void Main(string[] args)
        {
         
         string User_Operation = "";
         
         Console.Write("Please enter a number: ");
         double first_int = Convert.ToDouble(Console.ReadLine());
         Console.Write("Please enter a number: ");
         double second_int = Convert.ToDouble(Console.ReadLine());

        Console.Write("Please enter operation: ");
        User_Operation = Console.ReadLine();
         User_Operation.ToUpper();
        

        if (User_Operation == "ADDITION")
        {
             Console.WriteLine(first_int + second_int);
        }
        else if (User_Operation == "SUBTRACTION")
        {
            Console.WriteLine(first_int - second_int);
        }
        else if (User_Operation == "MULTIPLICATION")
        {
            Console.WriteLine(first_int * second_int);
        }
        else if (User_Operation == "DIVISION")
        {
            Console.WriteLine(first_int / second_int);
        }
        else
        {
            Console.WriteLine("Invalid Operator Input");
        }

        
        }
    }
}
jps
  • 20,041
  • 15
  • 75
  • 79
calister28
  • 43
  • 4
  • Does this answer your question? [ToUpper() method not working](https://stackoverflow.com/questions/11393088/toupper-method-not-working) – madreflection Nov 20 '20 at 18:30
  • User_Operation = User_Operation.ToUpper(); And use switch case this is better than if else – Stanislav Nov 20 '20 at 18:32
  • User_Operation = User_Operation.ToUpper(); Or use a switch with Switch(User_Operation.ToUpper), then your cases (rather than if else-if). C# strings are immutable, so ToUpper doesn't change the existing string it returns a new string that is upper case. You need to save or use that new string that is being return by the to upper function. – shox Nov 20 '20 at 18:38
  • Strings are immutable, so you need to do a new assignment if you want to change one. In this case you just need to assign the return value from the call to `ToUpper` back to the variable: `User_Operation = User_Operation.ToUpper();` – Rufus L Nov 20 '20 at 18:47

1 Answers1

1

You could optimize your code better, but on your problem:

if you change:

User_Operation = Console.ReadLine();

to

User_Operation = Console.ReadLine().ToUpper();

it will be enough.

P.S.: The reason that it’s not working on your code is because you are not assigning the new value User_Operation.ToUpper(); to anything. If you did User_Operation = User_Operation.ToUpper(); it would work too, but it’s not as efficient as my answer.

Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77