Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Trying to make a calculator

so basically whenever i try to change the variable "restart" inside of the while loop it gives me an error saying "a local or parameter named ‘restart’ cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter". if anyone knows how to fix this please tell me


public class Program
{
    public static void Main()
    {
        bool restart = true;
        while (restart == true)
        {
            Console.WriteLine("please enter Multiply, Divide, Add Or Subtract");
            string Method = Console.ReadLine();
            if (Method == "Add")
            {
                Console.WriteLine("Please Enter First Number");
                double numberOne = Convert.ToDouble(Console.ReadLine());
                Console.WriteLine("Please Enter The Second Number");
                double numberTwo = Convert.ToDouble(Console.ReadLine());
                Console.WriteLine(numberOne + numberTwo);
                Console.WriteLine("Would You Like To Use The Calculator Again? (true = yes, false = no)");
                bool restart = Convert.ToBoolean(Console.ReadLine());
            }
            else if (Method == "Subtract")
            {
                Console.WriteLine("Please Enter First Number");
                double numberOne = Convert.ToDouble(Console.ReadLine());
                Console.WriteLine("Please Enter The Second Number");
                double numberTwo = Convert.ToDouble(Console.ReadLine());
                Console.WriteLine(numberOne - numberTwo);
                Console.WriteLine("Would You Like To Use The Calculator Again? (true = yes, false = no)");
                bool restart = Convert.ToBoolean(Console.ReadLine());
            }
            else if (Method == "Multiply")
            {
                Console.WriteLine("Please Enter First Number");
                double numberOne = Convert.ToDouble(Console.ReadLine());
                Console.WriteLine("Please Enter The Second Number");
                double numberTwo = Convert.ToDouble(Console.ReadLine());
                Console.WriteLine(numberOne * numberTwo);
                Console.WriteLine("Would You Like To Use The Calculator Again? (true = yes, false = no)");
                bool restart = Convert.ToBoolean(Console.ReadLine());
            }
            else if (Method == "Divide")
            {
                Console.WriteLine("Please Enter First Number");
                double numberOne = Convert.ToDouble(Console.ReadLine());
                Console.WriteLine("Please Enter The Second Number");
                double numberTwo = Convert.ToDouble(Console.ReadLine());
                Console.WriteLine(numberOne / numberTwo);
                Console.WriteLine("Would You Like To Use The Calculator Again? (true = yes, false = no)");
                bool restart = Convert.ToBoolean(Console.ReadLine());
            }
            else
            {
                Console.WriteLine("error found");
                Console.WriteLine("Would You Like To Use The Calculator Again? (true = yes, false = no)");
                bool restart = Convert.ToBoolean(Console.ReadLine());
            }
        }
    }
}```

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Let’s look at this part of your code:

bool restart = true;
while (restart == true)
{
    // Some stuff omitted...
    bool restart = Convert.ToBoolean(Console.ReadLine());
}

You redeclare restart. Instead, you should just assign it like this (leave bool off when you use the variable after first declaring it):

restart = Convert.ToBoolean(Console.ReadLine());
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading