0

I have looked really hard on this. Integers seem easy, but this is what I have been trying to figure out in C# in visual studio.

I want to have a user input of a letter like "a" then the console writes "apple", b=bobby, c=charlie, etc. and when they don't put a letter then it gives an error message like "no letters used". I am not sure if I am suppose to convert the user input from a string with ToChar or what the best way to do this is. I haven't gotten into arrays yet and haven't figured out the switch command with characters (instead of integers or strings).

This is how I was trying to do it:

Console.WriteLine("Enter a letter ");
choice = Convert.ToChar(Console.ReadLine());

if (char choice = 'a'){
    Console.WriteLine("apple");

}else if (char choice = 'b'{
    Console.WriteLine("bobby");
}else if (char choice = 'b'{
    Console.WriteLine("bobby");
}else (char choise=!IsLetter){
    Console.WriteLine("No Letters entered");

3 Answers 3

2

If you want to stick to if else, here is what you can do:

if (choice == 'a')
{
    Console.WriteLine("apple");
}
else if (choice =='b')
{
    Console.WriteLine("bobby");
}
else if (char choice = 'c')
{
    Console.WriteLine("charlie");
}
else
{
    Console.WriteLine("No Letters entered");
}

You don't need to put condition on your else anymore :)

Sign up to request clarification or add additional context in comments.

1 Comment

This helped me see the whole context of how to use If Else - Thanks!
2

Use a switch statement, probably best for your scenario

static void Main(string[] args)
    {
        //initialise bool for loop
        bool flag = false;

        //While loop to loop Menu
        while (!flag)
        {
            Console.WriteLine("Menu Selection");
            Console.WriteLine("Press 'a' for apple");
            Console.WriteLine("Press 'b' for bobby");
            Console.WriteLine("Type 'exit' to exit");

            //Read userinput
            //Store inside string variable
            string menuOption = Console.ReadLine();

            switch (menuOption)
            {
                case "a":
                    //Clears console for improved readability
                    Console.Clear();
                    //"\n" Creates empty line after statement
                    Console.WriteLine("apple has been selected\n");
                    //Break out of switch
                    break;
                case "b":
                    Console.Clear();
                    Console.WriteLine("bobby has been selected\n");
                    break;
                case "exit":
                    Console.Clear();
                    Console.WriteLine("You will now exit the console");
                    //bool set to false to exit out of loop
                    flag = true;
                    break;
                    //Catch incorrect characters with default
                default:
                    Console.Clear();
                    //Error message
                    Console.WriteLine("You have not selected an option\nPlease try again\n\n");
                    break;
            } 
        } 


        Console.ReadLine();

3 Comments

Thank you and everyone for their comments. This really helped clarify some things. So it basically does just stay a string.
The only thing I was questioning and pondering over for a while is the !flag command. I was thinking !flag should be if bool flag = not false. But would this mean that it is already true? Or does it imply that it just isn;t active until it is true?
The flag is set true by default. U could do the opposite what I have done and initialise the flag as true then just take the "!" out of the while condition. Then set the flag to false in the "exit" case to exit out the loop
0

This is how you write it using a switch:

switch (choice){
case 'a':
    Console.WriteLine("apple");
    break;
case 'b':
    Console.WriteLine("bobby");
    break;
case 'c':
    Console.WriteLine("charlie");
    break;
default:
    Console.WriteLine("No Letters entered");
    break;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.