Reputation: 7
So I have a small problem with this code snippet.. When i run it in the cmd.exe it works fine, but when i click one of the userValues (1-6) in the if-else statements below and hits enter.. Nothing happens and the compiler shots down... I am pretty sure that this is the right way to do it, so I dont understand why any one of my six string messages wont show up when i hit a number between 1 and 6? Is there anyone of you guys that can help me?
Please note that I am new to C# and new to Stack overflow, and I have heard a lot of people say that you will be eaten alive if you ask dumb questions in here.. Sorry for the inconvenience if this seems like an extremely stupid or easy question. I am just trying to learn this
class Program
{
public static ReadFile f = new ReadFile();
public static bool MaleTour { get; set; }
public static bool FemaleTour { get; set; }
public static bool MixTour { get; set; }
private static int playerSize;
static void Main(string[] args)
{
Menu();
}
private static bool Menu()
{
{
Console.WriteLine("You have now entered the 2017 Wimbledon tournament!" + "\n" + "\n");
Console.Write("Choose one of the 6 options:" + "\n" + "Press 1 for Default tournament:" + "\n" + "Press 2 for Women's single:" + "\n" +
"Press 3 for Men's single:" + "\n" + "Press 4 for Women's double:" + "\n" + "Press 5 for Men's double:" + "\n" +
"Press 6 for Mix double:" + "\n" + "Insert your choice...:");
char Menu = Console.ReadKey().KeyChar;
string userValue = Console.ReadLine();
if (userValue == "1")
{
string message = "You have entered a default tournament";
Console.WriteLine(message);
return true;
}
else if (userValue == "2")
{
string message = "You have entered women's single";
Console.WriteLine(message);
return true;
}
else if (userValue == "3")
{
string message = "You have entered men's single";
Console.WriteLine(message);
return true;
}
else if (userValue == "4")
{
string message = "You have entered women's double";
Console.WriteLine(message);
return true;
}
else if (userValue == "5")
{
string message = "You have entered men's double";
Console.WriteLine(message);
return true;
}
else if (userValue == "6")
{
string message = "You have entered mix double";
Console.WriteLine(message);
return true;
}
else
{
string message = "Sorry! You have to choose one of the 6 tournament options";
Console.WriteLine(message);
return true;
}
Console.ReadLine();
}
}
Upvotes: 0
Views: 652
Reputation: 155
First, when you have a lot of conditions, it's better to use "switch / case" method. After, it's better too to post the error stack (output of your debug console. It allows us to help you).
public static bool MaleTour { get; set; }
public static bool FemaleTour { get; set; }
public static bool MixTour { get; set; }
private static int playerSize;
static void Main(string[] args)
{
Menu();
Thread.Sleep(10000); // if you want to do a break in your program.
}
private static bool Menu()
{
{
Console.WriteLine("You have now entered the 2017 Wimbledon tournament!" + "\n" + "\n");
Console.Write("Choose one of the 6 options:" + "\n" + "Press 1 for Default tournament:" + "\n" + "Press 2 for Women's single:" + "\n" +
"Press 3 for Men's single:" + "\n" + "Press 4 for Women's double:" + "\n" + "Press 5 for Men's double:" + "\n" +
"Press 6 for Mix double:" + "\n" + "Insert your choice...:");
string userValue = Console.ReadLine();
switch (userValue)
{
case "1":
Console.WriteLine("You have entered a default tournament");
case "2":
Console.WriteLine("You have entered women's single");
case "3":
Console.WriteLine("You have entered men's single");
case "4":
Console.WriteLine("You have entered women's double");
case "5":
Console.WriteLine("You have entered men's double");
case "6":
Console.WriteLine("You have entered mix double");
default:
Console.WriteLine("Sorry! You have to choose one of the 6 tournament options");
return false;
}
return true;
}
}}
Upvotes: 2
Reputation:
There are multiple issues with your code.
1) char Menu is supposed to contain the user input, but you are testing userValue, which is most likely empty (enter).
2) In Menu, in each if-else statement you have: return true. This means that the last statement in menu (Console.ReadLine()) is unreachable. You should see a compiler warning that there is unreachable code. If you want to run additional code after calling return, implement try-finally.
With your code the console will not wait for user input after reading the menu. So the thread is done and the program will stop.
3) Define Menu as void, not as a function. Unless you are planning to use the result.
4) Do not nest if statements, use switch.
static void Main(string[] args)
{
var isValidMenuItem = false;
while (!isValidMenuItem)
isValidMenuItem = Menu();
Console.WriteLine("Press a key to stop the program.");
Console.ReadKey(true);
}
private static bool Menu()
{
Console.WriteLine("You have now entered the 2017 Wimbledon tournament!" + "\n" + "\n");
Console.Write("Choose one of the 6 options:" + "\n" + "Press 1 for Default tournament:" + "\n" + "Press 2 for Women's single:" + "\n" +
"Press 3 for Men's single:" + "\n" + "Press 4 for Women's double:" + "\n" + "Press 5 for Men's double:" + "\n" +
"Press 6 for Mix double:" + "\n" + "Insert your choice...: ");
// Since there are less than 10 options we can use one char.
var userValue = Console.ReadKey().KeyChar;
Console.WriteLine();
switch (userValue)
{
case '1':
Console.WriteLine("You have entered a default tournament");
break;
case '2':
Console.WriteLine("You have entered women's single");
break;
case '3':
Console.WriteLine("You have entered men's single");
break;
case '4':
Console.WriteLine("You have entered women's double");
break;
case '5':
Console.WriteLine("You have entered men's double");
break;
case '6':
Console.WriteLine("You have entered mix double");
break;
default:
Console.WriteLine("Sorry! You have to choose one of the 6 tournament options");
return false;
}
// Do some additional things
return true;
}
Upvotes: 0
Reputation: 15151
You have a double input:
char Menu = Console.ReadKey().KeyChar;
string userValue = Console.ReadLine();
Remove one of those, if you leave the ReadKey one the user will only need to push the number, but you need to change userValue to be a char.
If you leave the ReadLine the user would need to press the number and then Return.
Upvotes: 0