You must Sign In to post a response.
  • Category: Visual Studio

    C# invalid character

    ok so I decided my best option for my code is do a loop for figuring out whether a character is valid or not. What I'm trying to do is the user inputs data which should be a number and based off that number is whether the character is valid or not however if it is then to stop executing the loop. BTW its a string variable. My code is below and it works great but I need to incorporate a loop. Any ideas?

    string menuSelection = "";
    string input = "";
    int number = 0;

    double numberOne = 0;
    double numberTwo = 0;
    bool entryIsValid = true;



    Console.WriteLine("Simple Caluclator");
    Console.WriteLine("\t 1) Add");
    Console.WriteLine("\t 2) Subtract");
    Console.WriteLine("\t 3) Multiple");
    Console.WriteLine("\t 4) Divdie");
    Console.WriteLine("\t 5) Quit");

    Console.Write("\n Enter Selection: ");

    menuSelection = Console.ReadLine();
    number = Convert.ToInt32(menuSelection);

    switch (menuSelection)
    {
    case "1":
    number = 0;
    break;
    case "2":
    number = 5;
    break;
    case "3":
    menuSelection = "*";
    break;
    case "4":
    menuSelection = "/";
    break;
    case "5":
    break;
    default:
    Console.WriteLine("Invalid Selection. Try Again");
    break;
    }


    if (number >= 1 && number < 5)
    {



    Console.Write("Enter Number 1: ");
    input = Console.ReadLine();
    numberOne = Convert.ToDouble(input);

    Console.Write("Enter Number 2: ");
    input = Console.ReadLine();
    numberTwo = Convert.ToDouble(input);

    if (numberOne == 0 || numberTwo == 0)
    {
    Console.WriteLine("Can't Divide by Zero. Try again");
    }
    else
    {
    Console.WriteLine("\t Result = ");
    }
    }

    else
    {
    Console.WriteLine("Press any key...");
    }



    Console.ReadKey();
  • #752483
    You want to identify invalid characters right ? you may take use of 'char.isdigit' method which Indicates whether the character at the specified position in a specified string is categorized as a decimal digit.
    or you can identify does the characters is letter or not using 'char.isLetter' method
    see below snippet

    using System;

    public class IsLetterSample {
    public static void Main() {
    char ch = '8';

    Console.WriteLine(Char.IsLetter(ch)); // False
    Console.WriteLine(Char.IsLetter("sample string", 7)); // True
    }
    }

    hope it helps

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #752502
    Hai ,
    You can use the below code which will check that if the character is invalid, then end the program:

    char menuSelection = ' ';
    string input = "";
    int number = 0;
    double numberOne = 0;
    double numberTwo = 0;
    Console.WriteLine("Simple Caluclator");
    Console.WriteLine("\t 1) Add");
    Console.WriteLine("\t 2) Subtract");
    Console.WriteLine("\t 3) Multiple");
    Console.WriteLine("\t 4) Divdie");
    Console.WriteLine("\t 5) Quit");
    Console.Write("\n Enter Selection: ");
    menuSelection =Convert.ToChar(Console.ReadLine());
    number = (Char.IsNumber(menuSelection) == true) ? Convert.ToInt32(menuSelection) : 9;

    switch (menuSelection)
    {
    case '1': number = 0;
    break;
    case '2': number = 5;
    break;
    case '3': menuSelection = '*';
    break;
    case '4': menuSelection = '/';
    break;
    case '5': break;
    default: Console.WriteLine("Invalid Selection. Try Again");
    break;
    }
    if (number >= 1 && number < 5)
    {
    Console.Write("Enter Number 1: ");
    input = Console.ReadLine();
    numberOne = Convert.ToDouble(input);
    Console.Write("Enter Number 2: ");
    input = Console.ReadLine();
    numberTwo = Convert.ToDouble(input);

    if (numberOne == 0 || numberTwo == 0)
    {
    Console.WriteLine("Can't Divide by Zero. Try again");
    }
    else
    {
    Console.WriteLine("\t Result = ");
    }
    }
    else
    {
    Console.WriteLine("Press any key...");
    }
    Console.ReadKey();

    Hope it will be helpful to you.

    Regards,
    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)
    pawansoftit@gmail.com

  • #752506
    Doing the validation before accepting the value will be good option rather that going through the loop.

    so Char.IsNumber or char.isdigit will work fine for you.


    Regards,
    Asheej T K

  • #752517
    Thank you guys for the char tip I completely forgot about using char!


  • Sign In to post your comments