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!

There are really two ways of handling this problem.

The first, more along the lines of what you are asking, is to check each character one at a time for it to be in one of the three following groups:

  1. Characters that are equal to '0'
  2. Characters that are equal to '1'
  3. Characters that are not equal to '1' or '0' (or... anything not belonging to the first 2 groups)

Your solution, however, only accounts for there to be two groups:

  1. Characters that are equal to '0'
  2. Characters that are not equal to '0'

As such your program looks for a 0 and if it doesn't see a 0, it simply assumes it is a 1. However you cannot make this assumption because, as you said, the user may enter a 'P' or any other character.

Now. The second way to achieve this is to add more event handlers to your gui. In this case add a keypress event handler that will check what the user typed and if it is not a 0 or a 1, it will not allow the character to be added to the text box. This isn't exactly what you had asked for, but it does provide for a way to ensure that the user's data consists only of 0's and 1's.


Hope this helps!

It is very simple to check if an input is a valid numeric data type. In your case you're using int you just need to use Int.Parse( textbox value ) or Int.TryParse( textbox value , out var ). Int.TryParse method will return a boolean value if the conversion is successful, this will be your trigger to notify the user that the input is valid/invalid. If you use Int.Parse then you need to Catch the error for it.

Awesome thanks to you both, these were both a great help, not sure which one I will use yet, but both very helpful. Thanks for being knowledgeable on these things and helping students such as myself!

public partial class Form1 : Form
    {
        int n; //input
        string binary; //input string
        string bad=""; //unknown symbol in binary string

        public Form1()
        {
            InitializeComponent();
        }

        private void d2bButton_Click(object sender, EventArgs e)
        {
            
            string result; //output

            result = "";

            try
            {
                n = int.Parse(decimalBox.Text);
            }

            catch
            {
                MessageBox.Show("Incorrect data format for decimal!");
                decimalBox.Focus();
                decimalBox.SelectAll();
                return;
            }


            if (n >= 0)
            {
                while (n > 0)
                {
                    if (n % 2 == 0)
                        result = '0' + result;
                    else
                        result = '1' + result;

                    n = n / 2;
                }
            }

            else
            {
                MessageBox.Show("Negative decimal values not allowed!");
                decimalBox.Focus();
                decimalBox.SelectAll();
                return;
            }

            binaryBox.Text = result;

        }

        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 if (binary[i] == '1')
                    n = n * 2 + 1;
                else
                {
                    bad = bad + binary[i];
                    MessageBox.Show("Unknown symbol"  + bad +  "in binary string!");
                    binaryBox.Focus();
                    binaryBox.SelectAll();
                    return;
                }

                i++;
            }


            decimalBox.Text = n.ToString();

        }


    }
}

To fix this in a simplistic way, I initialized a string bad to null, then changed my if-else statement at the bottom of the program to read the bad string character and then output it into a message box for the user. I know this isn't the most practical way to resolve the issue I was having, but it does work and this way I won't confuse the hell outta my professor! Anyhow thanks for the guidance!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.