Click here to Skip to main content
15,920,956 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Im having an issue with setting up a calculator. The variable(method) is a char and its being input on the first while loop.
on the second while loop i want the variable(method) from the first loop to be transfered into the second loop.
but there is an error and it says that the variable is unassigined.
please help. here is the code:

<pre lang="c#">
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Test
{
class Program
{
    static void Main(string[] args)
    {
        int continu = 0;
        int continu2 = 0;
        Console.WriteLine("Welcome To The Alpha Calculator!");
        Console.WriteLine("First,Type A Method");
        char method;
        while (continu2 != 0)
        {
            Console.Write("Type '+' or '-' or '*' or '/' :");
            method = char.Parse(Console.ReadLine());
            if (method == '+' || method == '/' || method == '*' || method == '-')
            {
                continu = 1;
                continu2 = 1;
                Console.WriteLine(method + " Picked");

            }
            else
            {
                Console.WriteLine("Unknown Method Try Again");


            }
            char method2 = method;
        }


        while (continu != 0)
        {

            Console.Write("Pick A First Digit:");
            double num1 = double.Parse(Console.ReadLine());
            Console.Write("Pick A Second Digit:");
            double num2 = double.Parse(Console.ReadLine());
            double sum = 0;
          if (method == '+')
            {
                sum = num1 + num2;
            }
            else if (method == '-')
            {
                sum = num1 - num2;
            }
            else if (method == '/')
            {
                sum = num1 / num2;
            }
            else if (method == '*')
            {
                sum = num1 * num2;
            }
            Console.WriteLine("The Solution is: " + sum);
            continu = 0;

        }
    }
}

}


What I have tried:

I tried changing the values of the loops variables(continu) to 1 but didnt work.
Posted
Updated 25-Oct-18 8:08am

Its because you declare method char variable and assigned value to it in first while loop only. If for any reason first while loop does not execute you don't have any value in variable in 2nd while - so resulting in error.

Move below lines before first while loop executes and will solve your issue.

char method;

Console.Write("Type '+' or '-' or '*' or '/' :");
method = char.Parse(Console.ReadLine());
--- first while and so on below ---
 
Share this answer
 
Look at your code (I'll chop out stuff to make it more obvious):
C#
        int continu = 0;
        int continu2 = 0;
        char method;
        while (continu2 != 0)
        {
...
        }


        while (continu != 0)
        {
...
        }
Because continu2 is always zero when it arrives at the first loop, the code inside the loop is never executed.
Because continu is set to zero, the same thing happens to the second loop.

And your method does absolutely nothing as a result ...
 
Share this answer
 
I noticed your first while() loop is for continu2 !=0.
However, when a valid value is picked, you set it to 1, so the loop goes on.
You need to either set continu2 to 0 or, better still, put a break in the if block.

char method2 = method; // but method2 is not in scope of the rest of program.

Is it actually telling you that 'method' is unassigned? The work variable is a bit vague.

More:
Now, when you char.Parse() the method, you are going to get an error when the content is not an int. You simply want the character. So, along with the loop fixes, above, just get the char from the keyboard and test its value.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900