All pastes #1359402 Raw Edit

Calculator Homework

public text v1 · immutable
#1359402 ·published 2009-03-12 22:23 UTC
rendered paste body
using System;

namespace ConsoleCalculator
{
    class Calculator
    {
        static void Main(string[] args)
        {
            int num1, num2, result;
            string op;

            Console.Write("Enter the first digit: ");
            if (!int.TryParse(Console.ReadLine(), out num1))
            {
                Console.WriteLine("You must enter a valid integer.");
                WaitForIt();
                return;
            }
            Console.Write("Enter the second digit: ");
            if (!int.TryParse(Console.ReadLine(), out num2))
            {
                Console.WriteLine("You must enter a valid integer.");
                WaitForIt();
                return;
            }
            Console.Write("Enter an operator (+ or -): ");
            op = Console.ReadLine();
            if (!Perform(op, num1, num2, out result))
            {
                Console.WriteLine("You must enter a plus or minus.");
                WaitForIt();
                return;
            }
            else
            {
                Console.WriteLine("The result is: " + result);
                WaitForIt();
            }
        }

        private static void WaitForIt()
        {
            Console.Write("Press Enter Key to continue...");
            Console.ReadLine();
        }

        private static bool Perform(string op, int num1, int num2, out int result)
        {
            switch (op)
            {
                case "+":
                    result = num1 + num2;
                    return true;
                case "-":
                    result = num1 - num2;
                    return true;
                default:
                    result = 0;
                    return false;

            }
        }
    }
}