0

I have a function that calculate numbers that the user inputs and that spits out the answer in the console. It is super simple. The user enters a number, then an operator (+,-,/ and*) and then another number. The function takes (number1) and (number2) and adds, subtracts etc and gives back the answer in the console.

And then the console closes. So it's like a one use calculator.

My question is.. How can I make it so once the user have given the 3 inputs, and the program spits out the answer, the program loops back to "Enter a number", and the process starts again.

Here is the enite code:

class Program
{
    static void Main(string[] args)
    {

        Console.WriteLine("Are you in need to calculate some numbers. y/n? ");
        string ifY = Console.ReadLine();

        if (ifY == "n")
        {
            Console.WriteLine("OK! You do not need a calculator");
        }
        else




            Console.Write("Enter a number: ");
        double num1 = Convert.ToDouble(Console.ReadLine());


        Console.Write("Enter operator: ");
        string op = (Console.ReadLine());


        Console.Write("Enter a number: ");
        double num2 = Convert.ToDouble(Console.ReadLine());


        if (op == "+")
        {
            Console.WriteLine(num1 + num2);
        }
        else if (op == "-")
        {
            Console.WriteLine(num1 - num2);
        }
        else if (op == "/")
        {
            Console.WriteLine(num1 / num2);
        }
        else if (op == "*")
        {
            Console.WriteLine(num1 * num2);
        }
        else
        {
            Console.WriteLine("Invalid operator");
        }




    }
}

}

And I'm sorry if what I am reffering to as Function is incorrect. Please correct me. As I said, completely new to this.

I know it is very "nooby" written. But we all have to start somewhere. Pls don't make fun of me

1 Answers1

1
Console.WriteLine("Are you in need to calculate some numbers. y/n? ");
string ifY = Console.ReadLine();

while (ifY != "n")
{
    // Put your single use calculator code here.

    Console.WriteLine("Are you in need to calculate some numbers. y/n? ");
    ifY = Console.ReadLine();
}
    
Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
  • Ok, I kind of understand. But how is this doing so that once the code has been executed it returns to "Enter a number: ".? – Christian Rystad Feb 28 '21 at 01:08
  • I've made a clarifying edit. – Robert Harvey Feb 28 '21 at 01:11
  • I now added in that "while" statement? The only thing that did was to loop the response "OK! You do not need a calculator". – Christian Rystad Feb 28 '21 at 01:15
  • You'll be eliminating that part of the code. – Robert Harvey Feb 28 '21 at 01:16
  • When I press "y", it goes through the code. E.g multiplies num1 and num2, and then closes. – Christian Rystad Feb 28 '21 at 01:16
  • Yeah, this is why we don't do extended tutoring here. Is this a school assignment? Check with your teacher, TA or the learning lab. – Robert Harvey Feb 28 '21 at 01:18
  • How can I make it so we kind of get back to 'square one and starts the calculator anew. So that the user can enter another math question without quitting the console, and starting it back up. – Christian Rystad Feb 28 '21 at 01:18
  • Do you see where it says "put your single-use calculator code here?" Do that. But you no longer need your `if` condition. The `while` loop does that for you. – Robert Harvey Feb 28 '21 at 01:19
  • No, not a school asignment. Trying to learn this on my own. But quickly realised that there where some questions you just can't get out of a YouTube video, or a video snippet in an online course. This is why I'm aking here, as I have no other place to turn to for this dilemma. – Christian Rystad Feb 28 '21 at 01:21
  • Has the online course gone over `while` loops yet? – Robert Harvey Feb 28 '21 at 01:22
  • I am sorry my questions where noobie and kind of stupid. But this was a major hinder for me that you helped me cross. I'm super greatfull,! And yes, I have been over while loops, but I thought this was an if statement issue :P – Christian Rystad Feb 28 '21 at 01:27
  • Could you give a quick example of a do-while loop, in this context? – Christian Rystad Feb 28 '21 at 01:40
  • So Brett. You would sugest running a do-while loop, so that you are able to "move on" from that piece of code, by executing a break or a return function? I'm sorry if I'm referring to things with the wrong names here. – Christian Rystad Feb 28 '21 at 02:08
  • So I never got an answer for why I should use a do-while loop vs a while loop. I've been sitting with this issue all night and figured out that a "when" loop works best here, as I don't want the code to end. The problem i have run into on the other hand is that when I run a do-while loop. Is that there is no way to break out of it other than to terminate the entire functtion. Is there a way to use a do-while loop, while still being able to stay within a certain piece of code? I'm sorry if my question sounds stupid, but hey, we all gotta ask something, somewhere ^^ – Christian Rystad Feb 28 '21 at 09:27
  • C doesn't have a "when" loop. The only difference between a `while` loop and a `do while` loop, is that the `do while` loop is guaranteed to execute at least once. – Robert Harvey Feb 28 '21 at 14:58
  • `Is that there is no way to break out of it other than to terminate the entire [program]` -- Check out the `break` and `continue` keywords. – Robert Harvey Feb 28 '21 at 15:06
  • Thank you! It's now running as intended. – Christian Rystad Mar 01 '21 at 15:10