10

I'm working a little with switch statements and want to know how to ignore the case sensitivity when it comes to input values.

Here is my code:

using System;

namespace SwitchStatements
{
class MainClass
{
    public static void Main(string[] args)
    {
    Start:
        Console.WriteLine("Please Input the Grade");
        char grade = Convert.ToChar(Console.ReadLine());

        switch (grade)
        {
            case 'A':
                Console.WriteLine("Excellent Work!");
                break;
            case 'B':
                Console.WriteLine("Very Good Effort! Just a couple of     Errors =)");
                break;
            case 'C':
                Console.WriteLine("You Passed. Push Yourself Next Time");
                break;
            case 'D':
                Console.WriteLine("Better put in more effort next time. I know you can do better");
                break;
            default:
                Console.WriteLine("Invalid Grade.");
                break;
        }
        Console.ReadKey();
        goto Start;
    }
}
}

If I put 'a' in instead of 'A' it returns the default response.

Can I use perhaps a .Comparison of some sort? If so where would I put it?

8 Answers 8

9

You can use ConsoleKey as condition for switch, the code will be like the following.

var grade =Console.ReadKey().Key;
switch (grade)
{
    case ConsoleKey.A:
        Console.WriteLine("Excellent Work!");
        break;
    case ConsoleKey.B:
        // Something here
        break;    
    case ConsoleKey.C:
        // Something here
        break;

    case ConsoleKey.D:
        // Something here
        break;          
    case ConsoleKey.E:
       // Something here
        break;             
    default:
       // Something here
        break;
}

So that you can avoid converting the input to uppercase/Lower case, and then it goes for another conversion To Char. Simply use ConsoleKey Enumeration inside the switch.

0
6

You can use ToUpper(); Like

Convert.ToChar(Console.ReadLine().ToUpper());

and to get saved from the error of getting more charaters with Console.ReadLine() you can use

char grd = Convert.ToChar(Console.ReadKey().KeyChar.ToString().ToUpper());
2
  • @MohitShrivastava : Appreciate the comment for compelling user to accept a partial answer, With all respect let me add few notes here, Console.ReadLine() allows the user to input more than one character, in such cases Convert.ToChar() will give you errors :). Nov 3, 2016 at 4:10
  • 1
    @MohitShrivastava 1) Switch blocks are fully capable of taking a string value as a conditional, so there's no need to convert back to char; 2) The value of KeyChar is itself a char, so the conversion to string is unnecessary as you can just use char.ToUpper; 3) Working with char in general is unnecessary as you can use ConsoleKeyInfo.Key which is an enum value and is thus easily used as a switch conditional without having to worry about character case.
    – Abion47
    Nov 3, 2016 at 6:48
2

you can use like following also

   char grade = Convert.ToChar(Console.ReadLine().ToUpperInvariant());  

https://msdn.microsoft.com/en-us/library/system.string.toupperinvariant.aspx

1

Convert to uppercase before switch like below,

grade = Char.ToUpper(grade);
1

Change

char grade = Convert.ToChar(Console.ReadLine());

To

char grade = Convert.ToChar(Console.ReadLine().ToUpper());

https://msdn.microsoft.com/en-us/library/system.string.toupper(v=vs.110).aspx

0
1

Write Switch on grade.ToUpper() like this and don't change change it's value, may be you will need it after

    char grade = Convert.ToChar(Console.ReadLine());

    switch (grade.ToUpper())
    {
        case 'A':
            Console.WriteLine("Excellent Work!");
            break;
        case 'B':
            Console.WriteLine("Very Good Effort! Just a couple of     Errors =)");
            break;
        case 'C':
            Console.WriteLine("You Passed. Push Yourself Next Time");
            break;
        case 'D':
            Console.WriteLine("Better put in more effort next time. I know you can do better");
            break;
        default:
            Console.WriteLine("Invalid Grade.");
            break;
    }
3
  • @S.Petrosov : With all respect let me add few notes here, Console.ReadLine() allows the user to input more than one character, in such cases Convert.ToChar() will give you errors :). Nov 3, 2016 at 4:14
  • What would you recommend using instead? Nov 3, 2016 at 5:46
  • @MarcusBristow You should use Convert.ToChar(Console.Read()) instead of Console.ReadLine() if you want to read only 1 char. Nov 3, 2016 at 9:09
1

You may fall from one case to another like this

public static void Main(string[] args)
{
    Boolean validInputRead = false;
    Char grade;

    while(!validInputRead) 
    {
        validInputRead = true;

        Console.WriteLine("Please Input the Grade");
        grade = Convert.ToChar(Console.Read());

        switch (grade)
        {
            case 'A':
            case 'a':
                Console.WriteLine("Excellent Work!");
                break;

            case 'B':
            case 'b':
                Console.WriteLine("Very Good Effort! Just a couple of     Errors =)");
                break;

            case 'C':
            case 'c':
                Console.WriteLine("You Passed. Push Yourself Next Time");
                break;

            case 'D':
            case 'd':
                Console.WriteLine("Better put in more effort next time. I know you can do better");
                break;

            default:
                Console.WriteLine("Invalid Grade.");
                validInputRead = false;
                break;
        }

        Console.ReadKey();
    }
}

EDIT

  • Changed from Console.ReadLine() to Console.Read() as suggested
  • Added while(!validInputRead) as requested
9
  • This would involve a bunch of useless repetition... it's better to convert the input to uppercase and then deal with it.
    – nmg49
    Nov 3, 2016 at 4:15
  • What will happen if i enter A+ when the console prompt me "Please Input the Grade" Nov 3, 2016 at 4:16
  • it will crash because A+ is not a char
    – Benj
    Nov 3, 2016 at 4:21
  • throws a System.FormatException
    – Benj
    Nov 3, 2016 at 4:22
  • Could you give me an example of this while(true) wrapping? So this will stop the program for exiting after the input value has been entered? Nov 3, 2016 at 5:47
0

string letterGrade; int grade = 0;

        // This will hold the final letter grade
        Console.Write("Input the grade :");

        switch (grade)
        {

            case 1:
                // 90-100 is an A
                letterGrade = "A";
                Console.WriteLine("grade b/n 90-100");
                break;
            case 2:
                // 80-89 is a B
                letterGrade = "B";
                Console.WriteLine("grade b/n 80-89");
                break;
            case 3:
                // 70-79 is a C
                letterGrade = "C";
                Console.WriteLine("grade b/n 70-79");
                break;
            case 4:
                // 60-69 is a D
                letterGrade = "D";
                Console.WriteLine(" grade b/n 60-69 ");
                break;
            default:
                // point whic is less than 59
                Console.WriteLine("Invalid grade");

                break;
        }
1
  • How does this help? It doesn't do what the original code does. Dec 24, 2019 at 17:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.