Using try-catch to care for letters

I wrote a program that will emulate the work of a cash register.

I need help on how to make the program take care if, for example, the user enters letters instead of numbers.

Then I would like the letters entered by the user to be lost, and the user will have a new opportunity to start from scratch.

I wrote code for it using try and catch, but not sure how it should be written.

class Program
{
    static void Main(string[] args)
    {
        int cash = 0;                    
        double totalAmount = 0;                     
        uint subTotal;                  
        int exchange;                   
        double roundingOffAmount;               

        Console.Write("Please enter a total amount for the cash register    : ");
        totalAmount = double.Parse(Console.ReadLine());

        if (totalAmount < 1)
        {
            Console.BackgroundColor = ConsoleColor.Red;
            Console.WriteLine("\nTotalamount needs to be more\n");
            Console.ResetColor();
            Environment.Exit(0);
        }

        try
        {
            Console.Write("Please enter cash for the cash register: ");
            cash = int.Parse(Console.ReadLine());
            if (cash < totalAmount)
            {
                Console.BackgroundColor = ConsoleColor.Red;
                Console.WriteLine("\nCash needs to be more than totalAmount\n");
                Console.ResetColor();
                Environment.Exit(0);
                Console.WriteLine();
            }
            else
            {
                // Do nothing
            }
        }
        catch (FormatException)
        {
           Console.Write("\nSorry you typed in a letter you need to type in a number");
            Console.WriteLine();
            Console.BackgroundColor = ConsoleColor.Red;
            Console.WriteLine("\nSomething went wrong, please try again");
            Console.ResetColor();
            Console.WriteLine();               
            Main(args);
        }

        subTotal = (uint)Math.Round(totalAmount);
        roundingOffAmount = subTotal - totalAmount;

        exchange = cash - (int)totalAmount;

        Console.WriteLine("\n Receipt"
                        + "\n ------------------------------------"
                        + "\n Totalt \t: \t {0:c}", totalAmount);
        Console.WriteLine(" RoundingOffAmount\t: \t {0:f2}", roundingOffAmount);
        Console.WriteLine(" To pay \t: \t {0:c}", subTotal);
        Console.WriteLine(" Cash \t: \t {0:c}", cash);
        Console.WriteLine(" Exchange \t:\t {0:c}", exchange
                        + "\n ------------------------------------");
        Console.WriteLine();
    }
}

Any help gets warm.

+4
source share
4 answers

- - - decimal, double. , , (double float) "" , , . . .

, , decimal.TryParse - , . , try/catch, , . :

decimal value;
while (!decimal.TryParse(Console.ReadLine(), out value))
{
    Console.WriteLine("Sorry, that wasn't a valid number");
}
+10

int.TryParse. , false.

false, .

, decimal .

+4

:

decimal totalAmount;
bool ok =  decimal.TryParse(outConsole.ReadLine(), out totalAmount);
if(!ok){
//Bad input. Do something
}else{
//input ok, continue
}

.

+1

totalAmount = double.Parse(Console.ReadLine());

do

 boolean isDouble = double.TryParse(Console.ReadLine(), out totalAmount);

if(isDouble)...
0

All Articles