0

I just need a little bit of help. I'm pretty new to coding and I watched some videos and managed to get this far without following exact words from videos. I'm just trying to Make a Calculator in a console window that when you type "Multiply" Or "Divide" it will jump to that section and then you can multiply or divide there. Then when you finish it will close the console.

I thought it worked fine at first but since Divide is the second function even if you type "Divide" it won't do anything because it still technically you just went over multiply, you will have to type "Divide" again for it to start the function.

Any help would be appreciated.

using System;

namespace SelfTeaching
{ 

    class Program
    {
        static void Main(string[] args) //Meathod Aka "Main," This will get called when program Starts
        {
            int num01;
            int num02;

            Console.WriteLine("Multiply Or Divide");
            if (Console.ReadLine() == "Multiply")
            {
                Console.Write("Type Number 1: ");
                num01 = Convert.ToInt32(Console.ReadLine());
                Console.Write("Type Number 2: ");
                num02 = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("The Answer Is; " + num01 * num02);
                Console.ReadKey();
                Environment.Exit(0);
            }
            else if (Console.ReadLine() == "Divide")
            {
                Console.Write("Type Number 1: ");
                num01 = Convert.ToInt32(Console.ReadLine());
                Console.Write("Type Number 2: ");
                num02 = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine(num01 + " Divided by " + num02 + " Equals: " + num01 / num02);
                Console.ReadKey();
                Environment..Exit(0);
            }
        }
    }
}
vasily.sib
  • 3,871
  • 2
  • 23
  • 26
  • 3
    Don't call `Console.ReadLine()` twice. Call it once and store result in a variable, like this `var line = Console.ReadLine();` then check it like this `if (line == "Multiply") { ... } else if (line == "Divide") { ... }` or even better use a [switch-case statement](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/switch) – vasily.sib Jul 03 '20 at 03:26

2 Answers2

1

To get Multiply Or Divide from single input, you have to take that string from input and then to use if-else block to follow user's choice. Also you need to use double instead int, if you need precise result with decimal when you use divide.

class Program
{
    static void Main(string[] args)
    {
        double num01;
        double num02;
        Console.Write("Multiply Or Divide ");
        string userInput = Console.ReadLine();

        if (userInput.Equals("Multiply", StringComparison.OrdinalIgnoreCase))
        {
            Console.Write("Type Number 1: ");
            num01 = Convert.ToDouble(Console.ReadLine());
            Console.Write("Type Number 2: ");
            num02 = Convert.ToDouble(Console.ReadLine());
            Console.WriteLine("The Answer Is; " + num01 * num02);
            Console.ReadKey();
            Environment.Exit(0);
        }
        else if (userInput.Equals("Divide", StringComparison.OrdinalIgnoreCase))
        {
            Console.Write("Type Number 1: ");
            num01 = Convert.ToDouble(Console.ReadLine());
            Console.Write("Type Number 2: ");
            num02 = Convert.ToDouble(Console.ReadLine());
            Console.WriteLine(num01 + " Divided by " + num02 + " Equals: " + num01 / num02);

            Console.ReadKey();
            Environment.Exit(0);
        }
    }
}

Its a good practice to use DRY principle (Don't Repeat YourSelf)
This is example of better code:

 class Program
{
    static void Main(string[] args)
    {
        double num01;
        double num02;
        Console.Write("Multiply Or Divide ");
        string userInput = Console.ReadLine();
        Console.Write("Type Number 1: ");
        num01 = Convert.ToDouble(Console.ReadLine());
        Console.Write("Type Number 2: ");
        num02 = Convert.ToDouble(Console.ReadLine());
        if (userInput == "Multiply")
        {             
            Console.WriteLine("The Answer Is: " + num01 * num02);             
        }
        else if (userInput == "Divide")
        {              
            Console.WriteLine(num01 + " Divided by " + num02 + " Equals: " + num01 / num02);               
        }
        Console.ReadKey();
        Environment.Exit(0);
    }
}
  • Well, first of all code-only answers are bad. Also, this will not compile, because return type of `Console.WriteLine()` is a `void` and you can't assign it to `int userInput` and also you can't compare `(int)userInput` with `(string)"Multiply"` – vasily.sib Jul 03 '20 at 03:29
  • I edited your answer because it was a disaster, however its still wrong – TheGeneral Jul 03 '20 at 03:36
  • 1
    Sorry, i'm trying to give my first answer here on StackOverFlow. – Bogdan Djukic Jul 03 '20 at 03:36
  • 1
    Try and get this compiling in visual studio, testing before answering is a good choice. Also a good answer will explain the problem (in words) and describe the solution – TheGeneral Jul 03 '20 at 03:37
  • @Bogdan, no need to feel sorry, your intention is correct. Just edit your answer a bit more. `Convert.ToInt32(Console.ReadLine())` will throw an exception, because user is typing `Multiply` and you can't `Convert.ToInt32("Multiply")`. Also you can't compare `int` and `string` in this line `if (userInput == "Multiply")` – vasily.sib Jul 03 '20 at 03:38
  • 1
    @Bogdan - Don't get too fussed about being sorry. We're here to help you to get a good answer. Our job is to help you constructively, without judgement. – Enigmativity Jul 03 '20 at 03:42
  • You are right. I was trying to do it fast without checking. – Bogdan Djukic Jul 03 '20 at 03:42
0

You could make a function called Divide first off

static int Divide(int num1, int num2)
{
  int num3; 
  num1 / num2 = num3;
  
  return num3;  
}

then you can call that function when they want to divide if Readline is a particular value ( e.g Multiply or Divide ). Now, when they access this function, you can make a boolean var also called Divide that is true when a user has divided for instance:

if ( userinput = "divide" ) 
{ 
divide:
 // enter num1 
// enter num2 
 Divide(num1, num2): // call function
 Console.Write(num3); // display num3
 bool divide = true; 
 if ( divide == true; ) 
{
  goto Divide;
}

I know people advise against using gotos but... anyways, this would also effectively put people in a constant division loop. You should make a switch statement like @vasily.sib said, and probably with a while loop that constantly checks for keys being pressed so people can break out of the goto statement is what I would do. But anyways, its what you asked for :D

  • `goto` is `no-no`. Also, you are calling your method `Divide`, but you do multiply in it? – vasily.sib Jul 03 '20 at 03:56
  • oops, forgot about that! I just edited it. Also, what is wrong with goto statements? And what other alternative would there be? Let me try to implement another way. I'm most familiar with goto statements. – Mike Fernandez Jul 03 '20 at 04:00
  • There is nothing _"wrong"_ with `goto`, I guess it's just my personal opinion. All this labels are just make me feel sad. You may look [here](https://stackoverflow.com/questions/11906056/is-using-a-goto-statement-bad) for some "pros" and "cons" of `goto` – vasily.sib Jul 03 '20 at 04:14
  • Fair enough, I know some people find it to make the code to be somewhat less readable occasionally. I also know that it becomes progressively harder to manage goto statements when the code gets more complicated. If it becomes insistent to not use a goto statement then you could simply extract your method into a function I suppose. But with code this simple, eh, what does it matter. – Mike Fernandez Jul 03 '20 at 04:23