简体   繁体   中英

C# — Loop won't accept non-numeric input after Convert.ToInt32(…)

When I try to enter the quit string, xxx, why does it throw an exception? It works in a while loop, but not with do...while loop.

After I convert to integer, the string variable only lets me use numeric characters (like -999) to exit the do...while loop. But I want to make the loop control variable a word like "quit" and not numbers. How can I do that?

Here is my code.

using System;
namespace StringInputPractice
{
    class StringInputPractice
    {
        static void Main()
        {
            // Declarations

            string inValue;
            int first;
            int second;
            int sum;

            do
            {
                Console.Write("\nEnter the first number. (Type \"xxx\" to exit):   ");
                inValue = Console.ReadLine();

                first = Convert.ToInt32(inValue);   //@@@@@
                Console.Write("\nEnter the second number. (Type \"xxx\" to exit):   ");
                inValue = Console.ReadLine();
                second = int.Parse(inValue);

                sum = first + second;

                Console.WriteLine("\nThe sum of {0} and {1} is {2}.", first,
                    second, sum);

                /* Things I've tried inside do { } and that don't work */

                //inValue = "";
                //inValue = null;
                //inValue = inValue.ToString();
                //inValue = first.ToString();
                //inValue = second.ToString();
            }

            while (inValue != "xxx");   /*If you enter a non-numeric string,
                                         * an exception is thrown at
                                         * @@@@@ above.
                                         */

            Console.ReadLine();
        }
    }
}

Try this: use int.TryParse instead of Convert.ToInt32

public void myfun()
        {
            string inValue;
            int first;
            int second;
            int sum;

            do
            {
                Console.Write("\nEnter the first number. (Type \"xxx\" to exit):   ");
                inValue = Console.ReadLine();

                if (int.TryParse(inValue, out first))
                {
                   // first = Convert.ToInt32(inValue);   //@@@@@
                    Console.Write("\nEnter the second number. (Type \"xxx\" to exit):   ");
                    inValue = Console.ReadLine();
                    if(int.TryParse(inValue, out second))
                    {
                   // second = int.Parse(inValue);

                    sum = first + second;

                    Console.WriteLine("\nThe sum of {0} and {1} is {2}.", first,
                        second, sum);
                    }
                }
                /* Things I've tried inside do { } and that don't work */

                //inValue = "";
                //inValue = null;
                //inValue = inValue.ToString();
                //inValue = first.ToString();
                //inValue = second.ToString();
            }

            while (inValue != "xxx");   /*If you enter a non-numeric string,
                                     * an exception is thrown at
                                     * @@@@@ above.
                                     */

            Console.ReadLine();
        }

What program are you using to code? Most programs allow you to debug your application. This means you can freeze the program and run it line by line. On each line, you can check the values of variables, and if there is an exception - you'll be able to see it.

Your error is caused by this line: first = Convert.ToInt32(inValue);

You need to first check that you're not converting letters to numbers (because that will cause an exception). You can try Int32.TryParse(), or other alternatives.

How do you expect xxx to be converted to a number ? That's impossible!

What I would do is:

Right after getting the inValue make an if statement that check if It's xxx, like that in your while() , if true then break; the loop

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM