When I put a number larger than 1 in my binary-to-decimal converter, I get the error startIndex cannot be larger than length of string

Blake

I'm trying to create a program that converts a 5 digit binary number to decimal. As it stands, the conversion works perfectly, but I'm having trouble with giving an error message if the user inputs a number larger than 1.

for (int i = 4; i>=0; i--)
{
    digit = txt_input.Text.Substring(i,1);
    num = Convert.ToInt32(digit);

    //If a digit is 1 or 0
    if (num <= 1)
    {
        total += num * (Math.Pow(2, x));
        x += 1;
        goahead = 1;
    }

    //If a digit is not 1 or 0
    if (num > 1)
    {
        lst_output.Items.Add("All digits must be either 1 or 0.");
        i = 10;
        goahead = 0;
    }
}

When the user inputs 1's or 0's the program works as intended, but when a number larger than 1 is inputted, I get the error "startIndex cannot be larger than length of string" on line 3.

If a number with more or less than 5 digits is inputted, the user gets a message saying that the number must be 5 digits long. So as far as I can tell, the problem isn't the size of the startIndex. Especially since, no matter what the user inputs, startIndex remains unchanged.

Sach

Imagine your input is 10103.

Now pay attention to this part of the code:

if (num > 1)
{
    lst_output.Items.Add("All digits must be either 1 or 0.");
    i = 10;
    goahead = 0;
}

Why are you making i = 10 here?

So if you input is the above string, in the first iteration you'd go into the if statement above, add the message to lst_output, then set i to 10. Then you go back to the for loop and the i >= 0 condition is still true so you go inside the for loop once again. Now your i = 1, but your string is of length 5.

So;

digit = txt_input.Text.Substring(i,1);

Here now you're trying to take a substring of length 1 that starts at the index = 10, from a string that is only 5 characters long.

Of course it would throw

startIndex cannot be larger than length of string. Parameter name: startIndex.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How is it possible in this code: "ArgumentOutOfRangeException: startIndex cannot be larger than length of string"?

"Exception calling "Substring" with "2" argument(s): "startIndex cannot be larger than length of string" Using Powershell

Why do I not get a literal out of range error when storing a number larger than an i32 into a variable?

Error: Replication factor: 1 larger than available brokers: 0, when I create a Kafka topic

Why can I represent an int larger than MAXINT in hexadecimal, but not decimal?

If index is larger than length

Capturing numbers that are larger than 1 decimal place

check if string contains number larger than a number

Why is my frame larger than what I set it to be?

How do I return a specific number of rows (larger than 1), for each of the joined rows from the other table

check if string contains number larger than

What happens when I assign value larger than byte?

Inconsistent Results When threadpool larger than 1

PrintScreen contents are larger than what I see

Swift project is larger than I expect

Why is the UIButton larger than I set it

Binary Search a non-existing number in a sorted array, return a larger negative number than -1

Why is my text file larger than my binary file?

Why am I getting numbers larger than 1000 when I %1000 a number generated by 64 bit mersenne twister engine?

In Laravel migration, using string length larger than 255

mmap fails when length is larger than 4GB

case_when() in R returns a larger length vector than is expected

My Web Service throws an System.AggregateException when I upload files larger than 2MB to SharePoint Online

VLOOKUP string that is larger than options

index of closest a number in array, closest to x, that cannot be larger than x

Python cannot decode XORed string with a key larger than 127

Values larger than 1/3 of a buffer page cannot be indexed Django error

Numpy cannot store matrix larger than 1GB in memory

Getting wrong output when finding factorial of number larger than 12