-3

I started my apprenticeship 1 month ago and from my company I got my first task to create a Calculator in the console which is capable to do basic math (addition, subtraction, multiplication and ofc divide). Now I made my code so far to enter 2 numbers but what next? What command do I have to use to choose the correct term of calculating?

In the End I just want to put in a + if I want to add etc.

Here is my Code. I use Visual Studio 2019 and C#.

using System;

namespace Taschenrechner
{
    class Program
    {
        static void Main(string[] args)
        {
            int num1;
            int num2;


            Console.WriteLine("Taschenrechner");

            Console.WriteLine("Gib die erste Zahl ein");
            num1 = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("Gib die zweite Zahl ein");
            num2 = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("Gib die Rechenaufgabe an");
            
            
            Console.WriteLine(num1 + num2);
            Console.WriteLine(num1 - num2);
            Console.WriteLine(num1 * num2);
            Console.WriteLine(num1 / num2);


            Console.ReadKey();

        }
    }
}
squillman
  • 13,363
  • 3
  • 41
  • 60
Bododerelf
  • 37
  • 5
  • welcome - consider using a `switch` block - also dont be afraid to ask your mentors. – Daniel A. White Feb 10 '21 at 15:42
  • Note that `num1 / num2` probably won't yield the answer you expect (e.g. `6/4` will spit out `1`) . You can read about that [here](https://stackoverflow.com/questions/10851273/why-does-integer-division-in-c-sharp-return-an-integer-and-not-a-float). – ProgrammingLlama Feb 10 '21 at 15:44
  • It sounds like you are in over your head and asking StackOverflow to do your homework for you. I would advise talking to your team and explaining you do not know how to accomplish this task. – Timothy Stepanski Feb 10 '21 at 15:44
  • 1
    @John i am aware of that but they specificly said i only need to use Int because the other stuff like float is too much to begin with. it just have to be capable to divide as example 16/4 – Bododerelf Feb 10 '21 at 15:47
  • @Timothy Stepanski i don't want others to do my work thats not what i want to accomplish and i am sorry if it looks like that was the case. I just can't find any source that tells me which command i need to use. the only cases i found on google are Calculators with buttons. but i will do as Daniel A. White said and try to implement a switch – Bododerelf Feb 10 '21 at 15:49
  • 1
    You already know about Console.ReadLine, but now store the result in a variable. Then lookup the switch statement – Hans Kesting Feb 10 '21 at 15:59
  • You can store a symbol in a char as well and use that to check which operation you need to perform. You do not need to call Convert on it at all. Just grab the first thing in a string that ReadLine returns – Natalia Muray Feb 10 '21 at 16:13

1 Answers1

0

This should hopefully point you in the right direction as a starting point.

            int num1;
            int num2;

            Console.WriteLine("Taschenrechner");

            Console.WriteLine("Gib die erste Zahl ein");
            num1 = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("Gib die zweite Zahl ein");
            num2 = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("enter + to add, - to minus, * to multiply, / to divide");


            string CalcOperation = Console.ReadLine();
            switch (CalcOperation)
            {
                case "+":
                    Console.WriteLine(num1 + num2);
                    break;
                case "-":
                    Console.WriteLine(num1 - num2);
                    break;

                //etc

                default:
                    Console.WriteLine("Sorry you didn't enter a valid operation");
                    break;

            }

            //just so you can see output
            Console.ReadLine();    
mjb
  • 126
  • 3