Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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 '-')

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading