1

is there's something wrong with my code below?

using System;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int num1 = 0, num2 = 0, rslt = 0;
            char oprtr;

            Console.Write("Enter calculation: ");
            num1 = Convert.ToInt32(Console.Read());
            oprtr = Console.ReadKey().KeyChar;
            num2 = Convert.ToInt32(Console.Read());

            switch (oprtr)
            {
                case '+':
                    rslt = num1 + num2;
                    break;
                case '-':
                    rslt = num1 - num2;
                    break;
                case '*':
                    rslt = num1 * num2;
                    break;
                case '/':
                    rslt = num1 / num2;
                    break;
                default:
                    break;
            }
            Console.Write("rslt: {0}", rslt);
            Console.WriteLine();
            Console.ReadKey();
            return;
        }
    }
}

after i compile and run it, the result is 0

it looks like there's nothing wrong with my code.

don't know what's wrong with my code

chipp
  • 63
  • 1
  • 1
  • 5
  • You need to explain what the desired behavior of the program is. Without that, the question is off topic and should be closed. – Anders Sep 06 '15 at 16:38

1 Answers1

1

You need to use Console.ReadLine() instead of Console.Read(). Console.Read() reads the pressed key code not the value.

Replace the following lines

Console.Write("Enter calculation: ");
num1 = Convert.ToInt32(Console.Read());
oprtr = Console.ReadKey().KeyChar;
num2 = Convert.ToInt32(Console.Read());

with

Console.Write("Enter calculation");
Console.Write("\nEnter 1st Operand: ");
num1 = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter Operator: ");
oprtr = Console.ReadKey().KeyChar;
Console.Write("\nEnter 2nd Operand: ");
num2 = Convert.ToInt32(Console.ReadLine());

EDIT:

Use int.TryParse method to avoid exception because if user press any alphabet or special character then it cannot be store into integer.

Example

int num1;
if (!int.TryParse(Console.ReadLine(), out num1)
{
    Console.WriteLine("Invalid Number");
}
else
{
    //num1 is ready for calculations
}

Another thing, you should avoid integers because for division, integer/integer = integer, i-e 5/2 = 2 but it should 2.5

NASSER
  • 5,900
  • 7
  • 38
  • 57
  • in my program, i wanna use input like:7*8 so, i think `Console.ReadLine()` is not the right one. and also, press `ENTER` 3 time is very tedious, i guess... – chipp Sep 09 '15 at 09:19
  • hello? can anybody give me answer? – chipp Sep 13 '15 at 15:16
  • @chipp Probably you need [this](http://stackoverflow.com/questions/333737/c-sharp-evaluating-string-342-yield-int-18) because you want to solve `"7 * 8"` i-e string to expression. – NASSER Sep 13 '15 at 16:15
  • actually, i just want to use the input stream like it's done in C++: `int num1, num2;` `char sw;` `cin << num1 << sw << num2; //input 7+3` `cout >> sw; //print + on the screen` – chipp Sep 16 '15 at 08:29
  • i just wanna know how to extract `char` type from those sequential input like that – chipp Sep 16 '15 at 08:38
  • btw, how to write code in this site? i found this web is very fussy and complicating simple things like [code] tag -_- and even just to make a newline – chipp Sep 16 '15 at 08:41
  • @chipp Well, do you want to input just like `7+3` in single line, right? – NASSER Sep 18 '15 at 07:28
  • @chipp Can you input more than three operands? eg. `7+3+8` or `7-8*1`? – NASSER Sep 18 '15 at 07:28
  • Please let me know the ultimate inputs that user can input, then I will help you to achieve the output. :) – NASSER Sep 18 '15 at 07:30