Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Hi guys so i'm starting to learn C# and I came up with this problem when I was trying to mix things up

It says "Input string was not in correct format" at the n = Convert.ToInt32(Console.ReadLine());

Here's the whole code

namespace Exercise13
{
    class Program
    {
        static void Main(string[] args)
        {
            char choice;
            Console.Write("What operation would you like to use?");
            Console.WriteLine("
a. Addition 	b. Subtraction 	c.Multiplication 	d.Division");
            choice = (char)Console.Read();
            if (choice == 'a')
            {
                sumValues();
            }
            else if (choice == 'b') 
            {
                minusValues();
            }
            else if (choice == 'c') 
            {
                timesValues();
            }
            Console.ReadLine();

        }
        static void sumValues() 
        {


            int n = 0;
            int sum = 0;
            int i = 0,val = 0;
            Console.Write("How many numbers do you want calculate: ");
            n = Convert.ToInt32(Console.ReadLine());            

            for (i = 0; i < n; i++) 
            {
                Console.Write("
Input number: ");
                val = Convert.ToInt32(Console.ReadLine());
                sum += val;

            }
            Console.Write("
The Answer is: "+sum);

        }
        static void minusValues() 
        {
            int diff = 0, m, z, value;
            Console.Write("How many numbers do you want calculate: ");
            m = int.Parse(Console.ReadLine());
            for (z = 0; z < m; z++)
            {
                Console.Write("
Input number: ");
                value = int.Parse(Console.ReadLine());
                diff -= value;

            }
            Console.Write("
The Answer is: " + diff);

        }
        static void timesValues()
        {
            int prod = 0, e, i, val;
            Console.Write("How many numbers do you want to calculate: ");
            e = Convert.ToInt32(Console.ReadLine());
            for (i = 0; i < e; i++) 
            {
                Console.Write("
Input number: ");
                val = int.Parse(Console.ReadLine());
                prod *= val;
            }
            Console.Write("
The answer is: " + prod);
        }
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
318 views
Welcome To Ask or Share your Answers For Others

1 Answer

Use Integer.TryParse to handle the strings potentially not being numbers. Then prompt the user if the input is not parsable to enter valid input.

Convert and Parse both will throw exceptions if the string is not an exact number.

https://msdn.microsoft.com/en-us/library/f02979c7%28v=vs.110%29.aspx

        int n = 0;
        int sum = 0;
        int i = 0,val = 0;
        Console.Write("How many numbers do you want calculate: ");
        var isValidNumber = Int32.TryParse(Console.ReadLine(), out n);
        if(!isValidNumber) {
            Console.WriteLine("Invalid number entered!");
        }
        else {
           //Use the number
        }           

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...