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

Recommended Answers

All 5 Replies

choice is local to the method it's declared in. However if you declare a variable choice in main (it could be called anything really I was just matching it up with your switch statement) and use it like choice = Menu(); Then it will display the menu and assign the option chosen to the variable choice back in main.

choice is local to the method it's declared in. However if you declare a variable choice in main (it could be called anything really I was just matching it up with your switch statement) and use it like choice = Menu(); Then it will display the menu and assign the option chosen to the variable choice back in main.

Thanks for very quick reply. But another error shows up now.

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;
            int choice = Menu();
            //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 = mul(num1, num2); //Multiplication method call
                            Console.WriteLine("Multiplication is {0}", result);
                            break;

                    case 4: result = div(num1, num2); //Division method call
                            Console.WriteLine("Division is {0}", result);
                            break;
                    default: Console.WriteLine("Do you want to continue? y/n");
                             ans = Convert.ToChar(Console.ReadLine());
                             break;
                }
            } while (ans =='y');
        }
    }
}

Error is Error 1 Use of unassigned local variable 'ans'

>Use of unassigned local variable 'ans'

Local variables in c# are initially unassigned. So, initialize the 'ans' variable.

char ans='y';

On line 43 initialize it as char ans = 'n'; (or anything else besides 'y' really). Since ans is not getting a value except for in the default case of your switch block it's entirely possible to go through that without getting a value for it, therefore it doesn't know what to use.

EDIT: Adatapost is suggesting to set it to 'y'. My suggestion does not contradict that but I assumed that you'd want to default to going through the loop once if the choice was not presented to the user (rather than going around again).

commented: cool +0

Problems is solved in quick succession. Thanks to all of you.:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.