Reputation: 149
I want the user to be able to enter the number at the menu however when I do this it says its a string being enter. I have tried to convert as I left the code in but it will not work.
namespace Day1.Day1.Exercises
{
class PasswordGenerator
{
static void Main(string[] args)
{
int MainMenu = 0;
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("----------------------------------------------------------------");
Console.WriteLine(" PASSWORD GENERATOR");
Console.WriteLine("----------------------------------------------------------------");
Console.WriteLine("1 - Generate Password");
Console.WriteLine("2 - Help");
Console.WriteLine("3 - Exit");
MainMenu = Console.ReadLine();
int input = Convert.ToInt16(MainMenu);
if (input == 1)
{
Console.WriteLine("Generating your password");
}
else if (input == 2)
{
Console.WriteLine("----------------------------------------------------------------");
Console.WriteLine(" HELP");
Console.WriteLine("----------------------------------------------------------------");
Console.WriteLine("Please enter a number allocated on screen to an option");
Console.WriteLine("Please Enter the length of your password you wish it to be ");
Console.WriteLine("Wait for your password to be generated");
}
}
}
}
Upvotes: 0
Views: 101
Reputation: 165
You should prefer int.TryParse() because Console.ReadLine() could return an empty string as well. In this case int.Parse(string) will throw an exception.
bool valid = int.TryParse(Console.ReadLine(), out MainMenu);
Upvotes: 0
Reputation: 3441
Just create the MainMenu variable with the result of Console.ReadLine
...
var MainMenu = Console.ReadLine();
int input = int.Parse(MainMenu);
...
Upvotes: 3