2

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
    }
}
RedR0z10
  • 101
  • 6
  • 2
    The *program* stops there, because it's waiting for input. [`Console.Read()`](https://learn.microsoft.com/en-us/dotnet/api/system.console.read?view=netcore-3.1) reads the next character from input stream and waits for that. You need to click the console that comes up and press a button. This, however, will not work as Console.Read returns the char code, you'll want to use [`Console.ReadLine`](https://learn.microsoft.com/en-us/dotnet/api/system.console.readline?view=netcore-3.1) and take a look at [how to convert a string to a number](https://stackoverflow.com/q/1019793/9363973) – MindSwipe Aug 20 '20 at 06:23
  • Well of course it stops, because you tell it to stop. Read waits for you to input a single character into the console window, only then will it resume execution. – CShark Aug 20 '20 at 06:24
  • Can you clarify, do you mean that the flow stops I.e. waiting for input, or do you mean the process stops I.e. it crashes and isn't running any more? – Carl Aug 20 '20 at 06:26

3 Answers3

4

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.

MortenMoulder
  • 6,138
  • 11
  • 60
  • 116
1

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();
Lewitas
  • 66
  • 2
  • 7
1

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.

YouryDW
  • 393
  • 1
  • 7