C# script in Visual Studio suddenly stops midway without error

Viewed 90

I'm trying to make a basic calculator to practice my C# skills. I started yesterday learning the language. I've tried running a debugger but nothing comes up. The script always shuts down in the same location, marked below. This is my script:

static void Main(string[] args)
{
    string cType; //Type of calculus to be selected
    int num01; //First input number
    int num02; //Second input number
    int result; //Function of num01 and num02
    
    Console.WriteLine("Please select the type of calculus you wish to perform." +
        "These are:\nM - Multiplication\nD - Division\nS - Subtraction\nA - Addition");
    //Informing the user of calculus type they can select

    cType = Console.ReadLine();
    //Setting the string cType as what the user inputs

    if (cType == "M")
    //Detecting if the input is M to start multiplication script
    {
        Console.WriteLine("You have selected multiplication");
        Console.Write("Please enter the first number: ");
        num01 = Console.Read();
        //Assign first number to string num01

        // THE SCRIPT STOPS WORKING HERE

        Console.Write("Please enter the second number: ");
        num02 = Console.Read();
        //Assign second number to string num02

        result = num01 * num02;
        //Calculate result

        Console.Write("{0} * {1} = {2}", num01, num02, result);
        //Output result as full sentence
    }
}
3 Answers

You should use .ReadLine() instead, and then try to convert that to an integer.

if(!int.TryParse(Console.ReadLine(), out int num01)) {
    Console.WriteLine("Input was not an integer. Please try again");
    //retry logic
}

//num01 is now the integer value of the input

You can remove the num01 assignment at the top, because there is no need to define it beforehand.

You need to change method Console.Read() to Console.ReadLine() and convert input to an int. Console.Read() return char as Int32.

        string cType; //Type of calculus to be selected
        int num01; //First input number
        int num02; //Second input number
        int result; //Function of num01 and num02

        
        Console.WriteLine("Please select the type of calculus you wish to perform." +
            "These are:\nM - Multiplication\nD - Division\nS - Subtraction\nA - Addition");
        //Informing the user of calculus type they can select

        cType = Console.ReadLine();
        //Setting the string cType as what the user inputs

        if (cType == "M")
        //Detecting if the input is M to start multiplication script
        {
            Console.WriteLine("You have selected multiplication");
            Console.Write("Please enter the first number: ");
            num01 = Int32.Parse(Console.ReadLine());
            //Assign first number to string num01

            Console.Write("Please enter the second number: ");
            num02 = Int32.Parse(Console.ReadLine());
            //Assign second number to string num02

            result = num01 * num02;
            //Calculate result

            Console.Write("{0} * {1} = {2}", num01, num02, result);
            //Output result as full sentence     
        }
        Console.ReadKey();

When reading using Console.Read() you'll be reading the ASCII value of the character that you entered (so only 1 character). As MortenMoulder states it is better to read multiple characters at once using Console.ReadLine() as a String and then try to parse it using int.TryParse() to get the integer value of what was entered.

Related