I am in a basic programming class and am having difficulty figuring out how to account for a non-numeric value being placed in for a binary number that will be converted to decimal form. If there is to be say a P typed into the binary input number, I want the program to warn the user of said issue and include the "P" in an output string. Such as, ("Unknown symbol P in binary input.") I have the current code listed below, I removed all of my if-else statements and try-catch statements as they were not helping with this problem.
private void b2dButton_Click(object sender, EventArgs e)
{
int n; //output
int i; //counter
binary = binaryBox.Text;
i = 0;
n = 0;
while (i < binary.Length)
{
if (binary[i] == '0')
n = n * 2;
else
n = n * 2 + 1;
i++;
}
decimalBox.Text = n.ToString();
}
I have tried using a try catch but am unsure as to how to obtain the letter or unknown symbol from the string input from the user interface. If anyone can help, it would be greatly appreciated!
Can also respond to my email, <EMAIL SNIPPED> with answers. Thanks again!