Shadow Sage
Shadow Sage

Reputation: 17

Double data type calculator C#

Hello I am fairly new to C# programming and I need some help with my calculator. I'm try to use double data members and method, but when I use it nothing happens. Could someone explain what I am doing wrong?

static void Main(string[] args)
    {
        Double FirstOperand;
        Double SecondOperand;
        Double Result;

        FirstOperand = Convert.ToDouble(Console.ReadLine());
        Console.Write("What is your first number?");
        SecondOperand = Convert.ToDouble(Console.ReadLine());
        Console.Write("What is your second number?");
        Result = FirstOperand + SecondOperand;
        Console.Write("Result is {0}", Result);
    }

Upvotes: 0

Views: 1108

Answers (2)

oziomajnr
oziomajnr

Reputation: 1751

The problem is that you are reading the value before asking for the value. So when your application starts the first thing that would happen is that it would request for the value so nothing would show up on your screen. it would be waiting for you to enter a value. So the correct code should be

    static void Main(string[] args)
{
    Double FirstOperand;
    Double SecondOperand;
    Double Result;

    Console.Write("What is your first number?");
    FirstOperand = Convert.ToDouble(Console.ReadLine());

    Console.Write("What is your second number?");
    SecondOperand = Convert.ToDouble(Console.ReadLine());
    Result = FirstOperand + SecondOperand;
    Console.Write("Result is {0}", Result);
}

Upvotes: 0

D Stanley
D Stanley

Reputation: 152626

You're asking for input before printing the message that asks for input. Swap those operations:

    Console.Write("What is your first number?");
    FirstOperand = Convert.ToDouble(Console.ReadLine());
    Console.Write("What is your second number?");
    SecondOperand = Convert.ToDouble(Console.ReadLine());
    Result = FirstOperand + SecondOperand;
    Console.Write("Result is {0}", Result);

The next steps in your journey could be to add error handling/retry logic, formatting, etc.

Upvotes: 4

Related Questions