Click here to Skip to main content
15,906,463 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How to code if Character is NOT 'A','B','C','D' or 'E' from choice
and work if capital 'A' or Small 'a'

What I have tried:

C#
static void EnterAnswer(char[] CorrectAnswer)
        {
            char question=' ';
            for (int i = 0; i < 20; i++)
            {                
                Console.WriteLine("Question: {0}/20", question++);
                Console.Write("--------------");
                Console.WriteLine("A)");
                Console.WriteLine("B)");
                Console.WriteLine("C)");
                Console.WriteLine("D)");
                Console.WriteLine("E)");
                question = Console.ReadKey().KeyChar;
                Console.Clear();
            }
            while (!char.TryParse(Console.ReadLine(), out question) || !question.Equals('a') && !question.Equals('b') && !question.Equals('c') && !question.Equals('d') && !question.Equals('e'))
{
       Console.Write("Invalid entry, Please enter again : ");
}
Posted
Updated 2-Apr-18 16:56pm
v2
Comments
CHill60 2-Apr-18 16:43pm    
Use .Upper()
j snooze 2-Apr-18 17:09pm    
Are you sure you coded this correctly? .Upper() should work.
Your new line check should be
while (!question.Upper().Equals('A') || !question.Upper().Equals('B') && !question.Upper().Equals('C') && !question.Upper().Equals('D') && !question.Upper().Equals('E'))
an0ther1 2-Apr-18 17:23pm    
The char datatype does not have a method 'ToUpper' or 'Upper' - refer; https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/char

Kind Regards
j snooze 2-Apr-18 17:39pm    
https://msdn.microsoft.com/en-us/library/7d723h14(v=vs.110).aspx
This suggests that it does have ToUpper.
an0ther1 2-Apr-18 19:13pm    
There you go, learn something every day!!
EDIT: I took another look at this & yep, char does have a ToUpper method, but it cannot be called from a char like you would do with a string, you need to use Char.ToUpper(myCharVariable) - I will add in a note on the solution I added.
Cheers & Thanks

As an alternative solution, you could do something like:
C#
char[] validInput = new[] { 'A', 'B' };
char inKey;
bool isValid = false;

// loop until valid...
while (!(isValid = (validInput).Contains(char.ToUpper(inKey = Console.ReadKey().KeyChar))))
{
    Console.WriteLine("\nInvalid entry. Please try again");
}
Console.WriteLine($"\nValid entry: {inKey}");
 
Share this answer
 
v2
Console.ReadKey returns a ConsoleKeyInfo object - refer; Console.ReadKey Method (System)[^]
The above reference will also give you an example but the short answer is as follows;

C#
// Wait unit user enters a value
ConsoleKeyInfo ck = Console.ReadKey();
// I am just checking for A, B & C but extend as required
if(ck.Key.ToString().ToUpper() != "A" && ck.Key.ToString().ToUpper() != "B" && ck.Key.ToString.ToUpper() != "C")
{
    Console.WriteLine("Invalid entry. Please try again");
}

You can also use Regular expressions for this - refer; Regex Class (System.Text.RegularExpressions)[^] Which would be faster.

Using chars is more difficult but still possible, but you need to either convert it to a string, or use the 16 bit numeric value when comparing - (a = 97, A = 65 etc) as this is what is actually stored - refer; Ascii Table - ASCII character codes and html, octal, hex and decimal chart conversion[^]

EDIT: In the above comments it was pointed out that there is a method ToUpper available for the char data type, but you cannot use char.ToUpper(), you need to pass in the char for the Framework to convert it.
If you do want to use this method the above code would be changed as follows;
C#
char question = Console.ReadKey().KeyChar;
if(char.ToUpper(question) != 'A' && char.ToUpper(question) != 'B' && char.ToUpper(question) != 'C')
{
    Console.WriteLine("Invalid entry. Please try again");
}


Kind Regards
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900