hi im new to programing and im trying to make a program that would calculate the tax on a given price but that would accept the tax rate as either an integer or a double but i get this error

Index (zero based) must be greater than or equal to zero and less than the size of the argument list

my code looks like this

using System;
public class TaxCalculation
{
    public static void Main()
    {
       
        int rateI = 0;
        double rateD;
        double price;
        string inputRate;
        string inputPrice;
        

        Console.Write("Please enter the price ");
        inputPrice = Console.ReadLine();
        price = Convert.ToDouble(inputPrice);

        Console.Write("Please enter the tax rate ");
        inputRate = Console.ReadLine();
        rateD = Convert.ToDouble(inputRate);

        if (rateD > 0)

            rateI = Convert.ToInt32(rateD);

        if (rateI != 0)

            Calculation(price, rateI);

        else

            Calculation(price, rateD);
         

        Console.Write("Press enter to exit");
        Console.ReadLine();
    }


    public static void Calculation(double price, int rateI)
    {
        double taxrate;
        double tax;
        double total;

        taxrate = rateI / 100;
        tax = price * taxrate;
        total = price + tax;
        Console.WriteLine("The tax on {0}$ is {1}$ and The total is {3}$", price, tax, total);
    }

    public static void Calculation(double price, double rateD)
    {
        double total;
        double tax;

        tax = price * rateD;
        total = price + tax;
        Console.WriteLine("The tax on {0}$ is {1}$ and The total is {3}$", price, tax, total);
    }

}

Recommended Answers

All 3 Replies

Hi,

Instead of

Console.WriteLine("The tax on {0}$ is {1}$ and The total is {3}$", price, tax, total);

use

Console.WriteLine("The tax on {0}$ is {1}$ and The total is {2}$", price, tax, total);

price, tax and total represent an array of strings that has the length 3. With {3} you ask the writeln function to display the 4th element of that array, item that doesn't exist.

Cheers,
Ionut

you know i knew it was gonna be something dumb thx a bunch

If you got your answer, please mark the thread as solved.

Cheers,
Ionut

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.