I have just started to learn looping, and I don't understand how to accumulate more than one total in my output using a for
or while
statement.
My code is meant to represent three families (A, B and C) having a garage sale. When they enter their initials, they can enter an amount of a sale. They can add as many amounts as they want, and when they want to see their total, they press Z and it will display each family's total, as well as a grand total made at the garage sale.
I can't figure out how to show all the totals though. How should I go about this?
Here is what I have so far:
static void Main(string[] args)
{
double totalFamilyA;
double totalFamilyB;
double totalFamilyC;
double grandTotal = 0;
double saleAmount;
string familyInitial;
string inputSale;
Console.Write("Enter your family initial A, B, or C (uppercase/lowercase accepted) ");
familyInitial = Console.ReadLine();
Console.Write("Enter sale amount $");
inputSale = Console.ReadLine();
saleAmount = Convert.ToDouble(inputSale);
while (familyInitial != "Z" && familyInitial != "z")
{
totalFamilyA = Convert.ToDouble(inputSale);
totalFamilyB = Convert.ToDouble(inputSale);
totalFamilyC = Convert.ToDouble(inputSale);
if (familyInitial == "A" || familyInitial == "a")
{
totalFamilyA += saleAmount;
Console.Write("Enter next sale amount or press 'Z' or 'z' to see the grand total $");
inputSale = Console.ReadLine();
saleAmount = Convert.ToDouble(inputSale);
totalFamilyA = Convert.ToDouble(saleAmount);
}
else if (familyInitial == "B" || familyInitial == "b")
{
totalFamilyB += saleAmount;
Console.Write("Enter next sale amount or press 'Z' or 'z' to see the grand total $");
inputSale = Console.ReadLine();
saleAmount = Convert.ToDouble(inputSale);
totalFamilyB = Convert.ToDouble(saleAmount);
}
else if (familyInitial == "C" || familyInitial == "c")
{
totalFamilyC += saleAmount;
Console.Write("Enter next sale amount or press 'Z' or 'z' to see the grand total $");
inputSale = Console.ReadLine();
saleAmount = Convert.ToDouble(inputSale);
totalFamilyC = Convert.ToDouble(saleAmount);
}
}
Console.WriteLine("Your total is {0}", grandTotal.ToString("C"));
}