using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class CalculateNumber
    {

        int Number1, Number2;
        char option;
        int Result;

        public void Number()
        {
            Console.WriteLine("Enter the First number");
            Number1 = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("Enter the second number");
            Number2 = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("Main Menu");
            Console.WriteLine("1.Addition");
            Console.WriteLine("2.Subtraction");
            Console.WriteLine("3.Multiplication");
            Console.WriteLine("4.Division");

            Console.WriteLine("Enter the Operation you want to perform");
            option = Convert.ToChar(Console.ReadLine());

            while(1)
            {
            switch (option)
            {
                case '1':
                    Result = Number1 + Number2;
                    Console.WriteLine("The result of addition is:{0}", Result);
                    break;

                case '2':

                    Result = Number1 - Number2;
                    Console.WriteLine("The result of Subtraction is:{0}", Result);
                    break;

                case '3':

                    Result = Number1 * Number2;
                    Console.WriteLine("The result of Multiplication is:{0}", Result);
                    break;

                case '4':

                    Result = Number1 / Number2;
                    Console.WriteLine("The result of Division is:{0}", Result);
                    break;
                default:
                    Console.WriteLine("Invalid Option  ");
                    break;
            }
            }
        }
            Console.ReadLine();

        }
    }
    class ClassMain
    {
        static void Main(string[] args)
        {
            CalculateNumber obj = new CalculateNumber();
            obj.Number();
        }
    }

}

// i want this calculator to rerun but once i select a case the program goes unhandable

Edited 3 Years Ago by Dani: Formatting fixed

That is because you used a constant number as your loop controller. iin the line where you said

while(1)

so the loop will continue going on because 1 will always equal to itself.
If that's how you want it (case inside loop), then use code like:

//show your menu and read in value
while(option>=1 && option <=4)
{
case '1':
break;
...
...
...default:
break;
//show menu again and readin
}
Console.WriteLine("Done");
while(1)

This will never work, C# expects a boolean or an expression that resolves to a boolean. The number 1 resolves to an integer. This is one of the reasons I prefer C# over C and C++ !

it is wrong to say while(1)because this will prevent the list of cases other than case 1 to be solved make it as sentinal value for example while(option!=-1)
and then start your switch case . In my opinion , I'll make the option an integer not character so it will be case 1 instead of case '1' this will be better . hope this could help enough for you

Why are you using WHILE loop ? You can directly write SWITCH...CASE statement without iterating through WHILE loop.

Anotherthing You are checking WHILE(1) So it will be alwayz true and your loop will be never terminates. So it will become infinite...

start quote:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class CalculateNumber
    {

        int Number1, Number2;
        char option;
        int Result;

        public void Number()
        {
            Console.WriteLine("Enter the First number");
            Number1 = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("Enter the second number");
            Number2 = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("Main Menu");
            Console.WriteLine("1.Addition");
            Console.WriteLine("2.Subtraction");
            Console.WriteLine("3.Multiplication");
            Console.WriteLine("4.Division");

            Console.WriteLine("Enter the Operation you want to perform");
            option = Convert.ToChar(Console.ReadLine());

            while(1)
            {
            switch (option)
            {
                case '1':
                    Result = Number1 + Number2;
                    Console.WriteLine("The result of addition is:{0}", Result);
                    break;

                case '2':

                    Result = Number1 - Number2;
                    Console.WriteLine("The result of Subtraction is:{0}", Result);
                    break;

                case '3':

                    Result = Number1 * Number2;
                    Console.WriteLine("The result of Multiplication is:{0}", Result);
                    break;

                case '4':

                    Result = Number1 / Number2;
                    Console.WriteLine("The result of Division is:{0}", Result);
                    break;
                default:
                    Console.WriteLine("Invalid Option  ");
                    break;
            }
            }
        }
            Console.ReadLine();

        }
    }
    class ClassMain
    {
        static void Main(string[] args)
        {
            CalculateNumber obj = new CalculateNumber();
            obj.Number();
        }
    }

}

i want this calculator to rerun but once i select a case the program goes unhandable

Edited 3 Years Ago by mike_2000_17: Fixed formatting

This article has been dead for over six months. Start a new discussion instead.