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;

price = Convert.ToDouble(inputPrice);

Console.Write("Please enter the tax rate ");
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");
}

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);
}

}
2
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by Ionelul

Hi,

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