Click here to Skip to main content
15,894,249 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C#
Console.WriteLine("Type in 1,2 or x");
            char c = Console.ReadLine()[0];
            c = char.ToUpper(c);
            if (c== '1')
            {
                Console.WriteLine("Your option is {0}", c);
                Console.ReadKey();
            }//(c == '1')
            else if (c== '2')
            {
                Console.WriteLine("Your option is {0}", c);
                Console.ReadKey();
            }//(c == '2')
            else if (c.Equals('x'))
            {
                Console.WriteLine("Your option is {0}", c);
                Console.ReadKey();
            }//(c == 'x')
            else
            {
                Console.WriteLine("1,2 or x Only !");
                Console.ReadKey();
            }//else
            Console.WriteLine("Goodbye");
            Console.ReadKey();


What I have tried:

C#
(c== 'x')
i change it to
C#
(c.Equals('x'))
.
Posted
Updated 7-Dec-16 3:29am

Use the debugger to see what your code is doing. It allow you to execute lines 1 by 1 and to inspect variables as it execute.

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 
Always change the case while comparing the values, either ToUpper[^] or ToLower[^]

C#
if ("X".ToLower() == "x") // this will work irrespective of the case which the user types
          { }
 
Share this answer
 
Comments
Philippe Mori 7-Dec-16 10:05am    
As there was already a call to char.ToUpper just after reading the value, it does not make much sense to now convert it to lowercase. Instead, he should either compare with 'X' or change the original function cal.
You have to change you Code
else if (c.Equals('x'))

to
else if ("X".ToLower() == "x")

or
C#
else if ("x".ToUpper() == "X"

this will work if the user enters caps X or small x...

One Suggestion Its better to use switch-case-default statement if you want to make a decision from the number of choices..

Hope this will hep you.
 
Share this answer
 
Comments
Philippe Mori 7-Dec-16 10:09am    
Nice suggestion to use a switch statement. However, this answer does not bring enough value to the existing 3 answers. A comment to the question or another answer would have been enough.
I was putting little when i should have putting big X.
 
Share this answer
 
Comments
Patrice T 7-Dec-16 11:22am    
Use Accept answer to close the question.

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