Resolved Using an uninitialized variable

FBIBARRY

New member
Joined
Nov 5, 2020
Messages
4
Programming Experience
Beginner
Hi
I have made a calculator game and am trying to add different options e.g. add a survey. For the response, I tried to use yes and no but I don't understand how it is unassigned??

Thanks


C#:
using System;
using System.Collections.Generic;
using System.Text;
namespace Program
{
    class Program
    {
        static void Main(string[] args)
        {
            int Num1, Num2, yes, no, output;
            char option;
            Console.Write("Enter the First Number : ");
            Num1 = Convert.ToInt32(Console.ReadLine());
            Console.Write("Enter the Second Number : ");
            Num2 = Convert.ToInt32(Console.ReadLine());
            _ = Convert.ToString(Console.ReadLine());
            _ = Convert.ToString(Console.ReadLine());
            Console.WriteLine("Main Menu");
            Console.WriteLine("1. Added together or;");
            Console.WriteLine("2. Second number subtracted from first number or;");
            Console.WriteLine("3. Multiply numbers together or");
            Console.WriteLine("4. first number is divided by the second number;");
            Console.WriteLine("5. Survey  ");
            Console.WriteLine("Enter the option from the above list to perform the task: ");
          
            option = Convert.ToChar(Console.ReadLine());
            switch (option)
            {
                case '1':
                    output = Num1 + Num2;
                    Console.WriteLine("The result of Addition is : {0}", output);
                    break;
                case '2':
                    output = Num1 - Num2;
                    Console.WriteLine("The result of Subtraction is : {0}", output);
                    break;
                case '3':
                    output = Num1 * Num2;
                    Console.WriteLine("The result of Multiplication is : {0}", output);
                    break;
                case '4':
                    output = Num1 / Num2;
                    Console.WriteLine("The result of Division is : {0}", output);
                    break;
                case '5':
                    output = yes; - "use of unassigned local variable 'yes'??
                    Console.WriteLine("You're good", output);
                    break;
                case '6':
                    Console.WriteLine("Do you like fortnite?");
                    break;
                default:
                    Console.WriteLine("Invalid Option");
                    break;
            }
            Console.ReadLine();
        }     
    }
}
 
Last edited by a moderator:

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
2,409
Location
Chesapeake, VA
Programming Experience
10+
On line 10, you declared the variable named yes. On line 46, you are trying to use it by assigning it to a variable named output.

Where do you think you assigned a value to your variable named yes?
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
2,409
Location
Chesapeake, VA
Programming Experience
10+
I broke out these posts because they a completely new problem/topic. In the future please start a new thread when you have a new question.
 
Top Bottom