I don't understand why my switch case isn't working. I'm trying to get the user input from a Console.Read variable which is converted to Int32 but the switch is jumping directly into the default case.
Main method:
class Program {
static void Main(string[] args) {
Console.WriteLine("##############################################");
Console.WriteLine("# #");
Console.WriteLine("# 1 - ADD ITEM 3 - VIEW ITEMS #");
Console.WriteLine("# 2 - EDIT ITEM 4 - DELETE ITEM #");
Console.WriteLine("# #");
Console.WriteLine("##############################################");
// Get input from user
int userChoice = Convert.ToInt32(Console.Read());
// Call function that responds to user selection from main menu
Functions function = new Functions();
function.SelectFunction(userChoice);
}
}
This is the switch method taken from a class called Functions:
public void SelectFunction(int choice) {
switch (choice) {
case 1:
Console.WriteLine("ADD ITEM");
/*Console.WriteLine("Please enter ID:");
var id = Console.ReadLine();
Console.Write("Item name: ");
var name = Console.ReadLine();*/
break;
case 2:
Console.WriteLine("EDIT ITEM");
break;
case 3:
Console.WriteLine("VIEW ITEMS");
break;
case 4:
Console.WriteLine("DELETE ITEM");
break;
default:
Console.WriteLine("Input not found in any of the choices. Please try again.");
Console.ReadKey();
break;
}
}