tell user to press only +
i am trying to ask user to press only '+' key and if user gave a wrong input tell him that is not an operation; for example if user inputs numbers or any other keys, tell user "you have to input an operation" i have tried codes below but does not get fixed!
Console.WriteLine("press '+' to add\npress '-' to delete");
//these are the ways i have tried:
//1:
char add = '+';
if (Console.ReadKey() == add) ;
//2:
string input = Console.ReadLine();
char inputChar = Convert.ToChar(input);
//if user give number the program shows error to me
if (inputChar == add) ;
//3:
if (Console.ReadKey().Key == ConsoleKey.+) ;
Solution 1:
You can try implementing do .. while
loop:
char op;
do {
Console.WriteLine("press '+' to add\npress '-' to delete");
op = Console.ReadKey().KeyChar;
}
while (op != '+' && op != '-');
we keep asking user until valid operation (op
) is provided (either '+'
or '-'
)