0

I am making a console calculator. Currently I am stuck at this check. Essentially, after I ask the user for a number, I want to make a conditional statement where when they press anything else I want to give them a message.

How would I do this? I was thinking if there was any way I can check between the datatypes and compare them in an if statements.

static void Main(string[] args)
{
    Console.Write("What sort of calculation do you want to perform? (+,-,*, or /) ");
    string operato= Console.ReadLine();

    Console.Write("Choose a number..");
    int numeroUno = Convert.ToInt32(Console.ReadLine());
    if (numeroUno);

    Console.Write("Choose another number ");
    int numeroDos = Convert.ToInt32(Console.ReadLine());

    Calculator calculator = new Calculator();
    calculator.a = numeroUno;
    calculator.b = numeroDos;
    calculator.Add(numeroUno, numeroDos);
    calculator.subtract(numeroUno, numeroDos);
    calculator.Multiplication(numeroUno, numeroDos);
    calculator.Division(numeroUno, numeroDos);

    if (operato == "+")
    {
        Console.WriteLine(calculator.Add(numeroUno, numeroDos));
    }
    else if (operato == "-")
    {
        Console.WriteLine(calculator.Add(numeroUno, numeroDos));        
    }
    else if (operato == "*")
    {
        Console.WriteLine(calculator.Multiplication(numeroUno, numeroDos));
    }
    else if (operato == "/")
    {
        Console.WriteLine(calculator.Division(numeroUno, numeroDos));
    }
    else
    {
        Console.WriteLine(" Pick on of the four options idiot");
    }

    Console.ReadLine();
}
Peter Duniho
  • 68,759
  • 7
  • 102
  • 136
  • If(numeroUno) is where I would have wanted to put the code in – GrizzlyBear Jul 16 '20 at 20:14
  • 4
    use `int.TryParse(Console.ReadLine(), out numeroUno)` instead of `Convert.ToInt32(Console.ReadLine())` – Dmitry Bychenko Jul 16 '20 at 20:15
  • Sorry, I am ignorant, but what is the benefit of this, Aren't they both doing the same thing? @DmitryBychenko – GrizzlyBear Jul 16 '20 at 20:18
  • 1
    @GrizzlyBear `int.TryParse` **will not** throw an exception if the `Console.ReadLine` value can't be parsed; it will either be `true` or `false`. If per say user enters a `h` non numeric then `Convert.ToInt32(Console.ReadLine())` would thrown an exception. – Trevor Jul 16 '20 at 20:19
  • @GrizzlyBear: if user inputs, say, `"bla-bla-bla"` (not a valid `int`), `Convert.ToInt32` *throws exception* when `int.TryParse` returns `false` (which you can check with a help of `if`) – Dmitry Bychenko Jul 16 '20 at 20:21
  • 1
    Also why all the `calculator.Add(numeroUno, numeroDos); calculator.subtract(numeroUno, numeroDos); calculator.Multiplication(numeroUno, numeroDos); calculator.Division(numeroUno, numeroDos); ` and then you are checking them again after all this and not sure what the user even picked? One more potential issue I can see, you could have overflows on your calculations if not checked. – Trevor Jul 16 '20 at 20:25
  • @Çöđěxěŕ I am just bad at coding man. I made a class and those were the methods, I don't know how and why it's bad. I only know its bad because you said it ahaha. Also Thank you guys Dmitry Bychenko Thank you – GrizzlyBear Jul 16 '20 at 20:30
  • 1
    @GrizzlyBear it's **not bad**, you learn by asking :). If I see something wrong, I try and say something about it when I can. Good Luck! – Trevor Jul 16 '20 at 20:31
  • The "datatype" is `string`. Nothing you can do about that. But you _can_ conditionally verify that the `string` value can be converted to an `int` value, using the `int.TryParse()` method. See marked duplicate. – Peter Duniho Jul 16 '20 at 20:48
  • @Çöđěxěŕ regarding your comment, on why did i add calculator.Add(numeroUno, numeroDos); calculator.subtract(numeroUno, numeroDos); calculator.Multiplication(numeroUno, numeroDos); calculator.Division(numeroUno, numeroDos); I was initalizing it because they were made in a class.. If I didn't would I be able to use them in the 'if;' statements? – GrizzlyBear Jul 17 '20 at 13:22
  • Also this community is awesome! Keep being awesome you guys! – GrizzlyBear Jul 17 '20 at 13:22

1 Answers1

0

I can advise you to use Try parse

https://learn.microsoft.com/it-it/dotnet/api/system.int32.tryparse?view=netcore-3.1

with this you can check if the typed character is a number.

Example:

   public class Example
{
   public static void Main ()
   {
      String [] values ​​= {null, "160519", "9432.0", "16,667",
                          "-322", "+4302", "(100);", "01FA"};
      foreach (var value in values)
      {
         int number;

         bool success = Int32.TryParse (value, out number);
         if (success)
         {
            Console.WriteLine ("Converted '{0}' to {1}.", Value, number);
         }
         else
         {
            Console.WriteLine ("Attempted conversion of '{0}' failed.",
                               value ?? "<Null>");
         }
      }
   }
}

If it were not a number I would use a switch case control I find it cleaner