Hello Friends , I from India, name samrat. i am c# sharp beginner.
The Problem is My code is showing Error 1 The name 'choice' does not exist in the current context.
So any help would be appreciated.
code-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace BasicCalculator
{
class Calculator
{
//Method declaration
public static int add(int no1, int no2)
{
return no1+no2;
}
public static int sub(int no1, int no2)
{
return no1 - no2;
}
public static int mul(int no1, int no2)
{
return no1 * no2;
}
public static int div(int no1, int no2)
{
return no1 / no2;
}
public static int Menu()
{
Console.WriteLine("Enter your choice like 1 or 2 and so on...\n\n");
Console.WriteLine("1. Addition");
Console.WriteLine("2. Subtraction");
Console.WriteLine("3. Multiplication");
Console.WriteLine("4. Division");
int choice = Int32.Parse(Console.ReadLine());
return choice;
}
public static void Main()
{
//variables declaration
char ans;
int num1, num2, result;
//Asking user for numbers
Console.WriteLine("Enter the no.1");
num1 = Int32.Parse(Console.ReadLine());
Console.WriteLine("Enter the no.2");
num2 = Int32.Parse(Console.ReadLine());
do
{
switch (choice)
{
case 1: result = add(num1, num2); //Addition method call
Console.WriteLine("Addition is {0}",result);
break;
case 2: result = sub(num1, num2); //Subtraction method call
Console.WriteLine("Subtraction is {0}", result);
break;
case 3: result = add(num1, num2); //Multiplication method call
Console.WriteLine("Multiplication is {0}", result);
case 4: result = add(num1, num2); //Division method call
Console.WriteLine("Division is {0}", result);
default: Console.WriteLine("Do you want to continue? y/n");
ans = Convert.ToChar(Console.ReadLine());
break;
}
} while (ans =='y');
}
}
}
Error is- Error 1 The name 'choice' does not exist in the current context G:\Visual Studio 2008\Projects\....\.....\BasicCalculator\BasicCalculator\Calculator.cs 57 25 BasicCalculator