C#C
C#3y ago
Mek

❔ ✅ Issues With Type Conversion When Validating User Input

List<string> initialText = new()
{
    "What Would You Like To Do?",
    "--------------------------",
    "** Submit Answer As 1, 2, or 3 **"
};

List<string> initialSelection = new()
{
    "1) Play Game",
    "2) View Previous Games",
    "3) View Statistics",
};

foreach (string item in initialText)
{
    Console.WriteLine(item);
};

foreach (string ite in initialSelection)
{
    Console.WriteLine(ite);
};

int userSelection;

if (!int.TryParse(Console.ReadLine(), out userSelection))
{
    tries--;
    Console.WriteLine("Input Must Be An Integer Of 1, 2, or 3. Try Again! " + tries + " Left.");
    SelectionScreen(tries);
}
else if (int.TryParse(Console.ReadLine(), out userSelection))
{
    int selectedItem = userSelection;

    if (initialSelection[selectedItem-1])
    {

    }
}
In this section of code, I print items to the screen and request user input. I'm trying to basically go if user input is not an integer, decrement tries and recall function. If user input is an integer but that integer does not match an index of List<string> initialSelection, then decrement tries and recall function. Otherwise, if user input is an integer and is a correct index of the list, then start next function but I'm having issues with the validation and type conversion. Thanks
Was this page helpful?