0

I just started learning C# and I'm trying to make a little month guesser for my class, where you input the birth month and then it guesses the month you were born. At first I used an array for the months but then changed it to a list. I've ended up with it saying, "the name months cannot be found in the current context" and I'm not sure how to fix it although I bet it's a simple fix. Here is the code:

using System;
using System.Collections.Generic;
                    
public class Program
{
    public static void Main()
    {
        

        Console.WriteLine("Welcome to the month guessing game!");
        Console.WriteLine("I will try to guess your birth month and age!");
        Console.WriteLine("Alright, so first of all are you born in 2008 or 2007?");
        bool year;
        
        string answer = Console.ReadLine();
        if (answer == "2007") {
            year = true;
            var months = new List<string>{ "September", "October", "November", "December"};
            
        }
        else {
            year = false;
            var months = new List<string>{ "Janurary", "February", "March", "April", "May", "June", "July", "August"};
            
        }
        if (year == true) {
            bool i = true;
            while (i == true) {
                var random = new Random();
                int index = random.Next(months.Count);
                Console.WriteLine("I guess...");
                Console.WriteLine(months[index]);
                Console.WriteLine("Was this correct?");
                Console.WriteLine("Type yes or no?");
                string ans2 = Console.ReadLine();
                if (ans2 == "yes" || ans2 == "Yes") {
                    i = false;
                    Console.WriteLine("Im a genius!");
                }
            }
        }
        if (year == false) {
            bool i = true;
            while (i == true) {
                Random rnd = new Random();
                int index2 = rnd.Next(months.Length);
                Console.WriteLine("I guess...");
                Console.WriteLine(months[index2]);
                Console.WriteLine("Was this correct?");
                Console.WriteLine("Type yes or no?");
                string ans2 = Console.ReadLine();
                if (ans2 == "yes" || ans2 == "Yes") {
                    i = false;
                    Console.WriteLine("Im a genius!");
                }
                
                
            }
        }
    }
}

I hope someone can help and thank you for reading.

wratio
  • 1
  • 1
  • Variables are only accessible within the scope their declared in, meaning your `months` variable is 1. Two variables, not one, 2. Only accessible within their respective scope (from the start of a `{` to the end `}`). Under `string answer = ...` place the line `List months;` and the remove the `var` from `var months = ...` – MindSwipe Mar 07 '22 at 13:02
  • The problem you have is the `months` variable is defined inside the scope of an `if` (and an `else`) and therefore it only exists within those scopes. Move the definition of `months` outside the `if`/`else` if need be. More details in the linked duplicates for you – Jamiec Mar 07 '22 at 13:03
  • I think you need to define your variable months before you enter the first if. then inside the if and else you can set the variable to the list you want it to be. – B.Letz Mar 07 '22 at 13:04

0 Answers0