Search code examples
c#exceptionunhandled

C# An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll


Not sure what I'm doing wrong here. I have a program that asks for an input, 1 of 2 options, and then displays the cost of the associated option.

I am getting the error message "An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll

Additional information: String must be exactly one character long."

What have I done wrong with my code? See below:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;


namespace computerPackage
{
    class Program
    {
        static void Main(string[] args)
        {
            char computerPackage;
            const decimal DELUXE_PACKAGE = 1500;
            const decimal SUPER_PACKAGE = 1700;
            Console.Write("Input the Computer Package D or S: ");
            computerPackage = char.Parse(Console.ReadLine());
            computerPackage = Char.ToUpper(computerPackage);
            if (computerPackage == 'D')
            {
                Console.WriteLine("Cost of Deluxe Computer Package is " + DELUXE_PACKAGE.ToString("D"));
            }
            else if (computerPackage == 'S')
            {
                Console.WriteLine("Cost of Deluxe Computer Package is " +
                SUPER_PACKAGE.ToString("S"));
            }
            else
            {
                Console.WriteLine("Package D or S not entered");
            }
            Console.WriteLine("Press any key to continue...");
            Console.ReadKey();          // pause
        }
    }
}


Solution

  • Bro the reason why errors is Console.WriteLine("Cost of Deluxe Computer Package is " + DELUXE_PACKAGE.ToString(D));is telling the runtime environment to set it to datetime format. change it to Console.WriteLine("Cost of Deluxe Computer Package is " + DELUXE_PACKAGE.ToString());