Ctrl C Ctrl V
Ctrl C Ctrl V

Reputation: 23

Beginner C# User Input/Convert/Switch Statement Question

I am a beginner, and I just started to write a simple calculator program in C#, and when I run my app in terminal, it's user input will still have ReadLine after I press enter, if I press enter twice, these codes is given out

Unhandled exception. System.FormatException: Input string was not in a correct format.
   at System.Number.ThrowOverflowOrFormatException(ParsingStatus status, TypeCode type)
   at System.Number.ParseInt32(ReadOnlySpan`1 value, NumberStyles styles, NumberFormatInfo info)
   at System.Convert.ToInt32(String value)

Here is my codes

using System;

namespace MyApplication
{
  class Program
  {
    static void Main(string[] args)
    {
      Console.WriteLine("Enter Yor First Number");
      Console.ReadLine();
      int x = Convert.ToInt32(Console.ReadLine());
      Console.WriteLine("Enter Yor Second Number");
      Console.ReadLine();
      int y = Convert.ToInt32(Console.ReadLine());
      Console.WriteLine("Choose your calculation");
      Console.WriteLine("Type + for Addition");
      Console.WriteLine("Type - for Subtraction");
      Console.WriteLine("Type * for Multiplication");
      Console.WriteLine("Type / for Division");
      Console.ReadLine();
      string expression = Console.ReadLine();
      switch(expression) 
      {
        case "+":
          Console.WriteLine(x+y);
        break;
        case "-":
          Console.WriteLine(x-y);
        break;
        case "*":
          Console.WriteLine(x*y);
        break;
        case "/":
          Console.WriteLine(x/y);
        break;

      }
    }
  }
}

Upvotes: 0

Views: 529

Answers (2)

AL.Sharie
AL.Sharie

Reputation: 1615

You have to check the user input, is it a valid int or empty if it is not a valid number you have to make it enter the number again

using System;

namespace MyApplication
{
  public class Program
  {
    public void Main(string[] args)
    {
         FirstNumber:
      Console.WriteLine("Enter Yor First Number");
      string stringX = Console.ReadLine();
      int x = 0;
        if(String.IsNullOrWhiteSpace(stringX) || !int.TryParse(stringX, out x)){
            goto FirstNumber;
        }
        
         SecondNumber:
      Console.WriteLine("Enter Yor Second Number");
      string stringY = Console.ReadLine();
      int y = 0;
        if(String.IsNullOrWhiteSpace(stringY) || !int.TryParse(stringY, out y)){
            goto SecondNumber;
        }
        
         Expression:
      Console.WriteLine("Choose your calculation");
      Console.WriteLine("Type + for Addition");
      Console.WriteLine("Type - for Subtraction");
      Console.WriteLine("Type * for Multiplication");
      Console.WriteLine("Type / for Division");

      string expression = Console.ReadLine();
        
        if(expression!="+" && expression!="-" && expression!="/" && expression!="*"){
            goto Expression;
        }
      switch(expression) 
      {
        case "+":
          Console.WriteLine(x+y);
        break;
        case "-":
          Console.WriteLine(x-y);
        break;
        case "*":
          Console.WriteLine(x*y);
        break;
        case "/":
          Console.WriteLine(x/y);
        break;

      }
    }
  }
}

Upvotes: 1

Vivek Nuna
Vivek Nuna

Reputation: 1

So when you enter twice, next ReadLine method gets called and it’s not a valid integer value. So you should always check whether the value is convertible to integer or not. You can use int.TryParse method. You can also debug by using try catch block.

Upvotes: 0

Related Questions