1

I'm new to coding so this might not be the best way to do things. I'm just writing codes to get used to it and understand it better. I was trying to make a calculator which would run in Visual Studios' cmd. I'm trying to collect the data the user is inputting and see if it's "+, -, *, or /," and if it is then it should continue, but if it's not then they should get a message and retry(i haven't wrote this part of the code yet). I made an array of "+, -, *, and /" and am trying to tell the if statement that if it doesn't equal these values, the message should be displayed to retry, but it gives me an error saying you can't assign != to string values.

        double user_Input;
        string user_inputOperation;
        double user_secondInput;
        double answer;
        string[] operations = { "+", "-", "*", "/" };

        Console.WriteLine("Type in a number: " );
        user_Input = Convert.ToDouble(Console.ReadLine());

        Console.WriteLine("Type an operation(+, -, *, /): ");
        user_inputOperation = Console.ReadLine();


        if (user_inputOperation != operations)    // problem occurs here
        {
            Console.WriteLine("That's not right, try: +, -, *, or /");
            user_inputOperation = Console.ReadLine();
        }


        Console.WriteLine("Type in another number");
        user_secondInput = Convert.ToDouble(Console.ReadLine());

        if (user_inputOperation == "+")
        {
            answer = user_Input + user_secondInput;

            Console.WriteLine("The answer is: ");
            Console.WriteLine(answer);
        }

        Console.ReadKey();
    }
}

}

Altaf Aria
  • 15
  • 7
  • 2
    Possible duplicate of [Multiple string comparison with C#](https://stackoverflow.com/questions/6034503/multiple-string-comparison-with-c-sharp) – arielCo Nov 05 '18 at 02:19

3 Answers3

3

You need array.contains instead of !=

if (!operations.Contains(user_inputOperation)  
{
   Console.WriteLine("That's not right, try: +, -, *, or /");
   user_inputOperation = Console.ReadLine();
}
ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
Sajeetharan
  • 216,225
  • 63
  • 350
  • 396
  • I can't use contains for some reason. when I put !operations. it doesn't show the option for contains, and when I try to put it manually it doesn't work either. and thank you for helping I didn't expect a reply this quick. – Altaf Aria Nov 05 '18 at 02:41
  • 1
    you need to use System.linq – Sajeetharan Nov 05 '18 at 02:42
1

You could also change the array type to a string type with the operations and use the IndexOf method on that string:

string operations = "+-*/";

while(operations.IndexOf(user_inputOperation) < 0)
{
    Console.WriteLine("That's not right, try: +, -, *, or /");
    user_inputOperation = Console.ReadLine();
}

IndexOf will return a -1 if a string is not found

Leo Hinojosa
  • 251
  • 2
  • 7
0

You can also use Linq:

if (operations.Any(x => x.Equals(user_inputOperation.Trim()))) 
{ }
Gauravsa
  • 6,330
  • 2
  • 21
  • 30