Question

I have this question that I have to do for an assignment for school. I am having troubles making it work with decimals. If someone could help me get the program to accept decimals. It will output a decimal but it won't let me input one.

using System;

    public class Bank_Account
    {
        public static void Main()
        {
            char input, C, D, P, Q;
            double balance;

            Console.WriteLine("Please enter C for Cheque, D for Deposit, P for Print or Q to quit");
            input = Convert.ToChar(Console.ReadLine());
            do
            {
                switch (input)
                {
                    case 'D':
                        Deposit(ref balance);
                        break;
                    case 'P':
                        Print(balance);
                        break;
                    case 'C':
                        Cheque(ref balance);
                        break;
                    default:
                        Console.WriteLine("{0} is not a valid choice", input);
                        break;
                }
            } while (input != 'Q');

        }

        public static void Deposit(ref double balance)
        {
            double deposit;

            Console.WriteLine("Enter the amount you want to deposit: ");
            deposit = Convert.ToInt32(Console.ReadLine());

            if (deposit > 1000)
            {
                deposit = deposit*1.02;
            }
            else
            {
                deposit = deposit;
            }

            balance = +deposit;
        }


        public static void Cheque(ref double balance)
        {
            var tempBalance = balance;
            double deduction;
            const double fee = 1;


            do
            {
                Console.WriteLine("Enter the amount of the cheque: ");
                deduction = Convert.ToInt32(Console.ReadLine());
            } while (deduction < 0);


            tempBalance = balance - deduction - fee;
            if (tempBalance < 0)
            {
                Console.Write("Not enough money in the account");
            }
            else
            {
                balance = tempBalance;
            }
        }


        public static void Print(double balance)
        {
            Console.WriteLine("Your current balance is {0.00}", balance);
        }
    }
Was it helpful?

Solution

Change this:

deposit = Convert.ToInt32(Console.ReadLine());

to:

deposit = Convert.ToDouble(Console.ReadLine());

since you're not reading an int, but a double. Using TryParse like suggested in another answer would be even better. Also, maybe you should be using decimal and not double if you need exactness, but double if accuracy is less of a concern.

OTHER TIPS

You are converting to Int, change like this,

        do
        {
            Console.WriteLine("Enter the amount of the cheque: ");
            deduction = Convert.ToDouble(Console.ReadLine());
        } 

Try This:

double deposit;
double.TryParse(Console.ReadLine(),out deposit);
deposit = Double.Parse(Console.ReadLine());
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top