0

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"));
}
Qantas 94 Heavy
  • 15,750
  • 31
  • 68
  • 83
  • 12
    Welcome to [so]. Have you started creating your code yet? If so, it'd be good to see what you've done so far so we can see what you've done wrong. – Qantas 94 Heavy Feb 10 '14 at 05:31

2 Answers2

1

Sounds like you want something like this:

int familyACount = 0;
int familBCount = 0;
int familyCCount = 0;

while(_inputString.toLower()!="z")
{
    //Parse input text here to work out the initials and amount entered
    string initials = //Something
    int amount = //Something

    if(initials == familyAInitials) familyACount += amount;
    //Repeat for other two families
}

//Z has been entered, so output the totals

Debug.Print("Family A:"+familyACount);
//Repeat for other families

Debug.Print("Total:" + (familyACount + familyBCount + familyCCount));

You'll want to work out the best way of getting the value off the end of the input (I'm assuming initials and total are entered as one string) and you might want doubles over ints, as you're dealing with money.

You could also output running totals as amounts are added if you wanted too, or a prompt saying "Z to quit" if the input text is not a set of initials etc.

Good luck!

ISeeSharp
  • 315
  • 1
  • 9
1

you doesnt require a looping statements you can simply write the following lines to achieve your goal

 public enum PersonIntial
    {
        Person1,
        Person2,
        Person3,
        All
    }
    public int Person1Acnt { get; set; }
    public int Person2Acnt { get; set; }
    public int Person3Acnt { get; set; }
    public int TotalSales { get; set; }

    private void ProcessInput(PersonIntial pInitail, int amount)
    {
        switch (pInitail)
        {
            case PersonIntial.Person1:
                {
                    Person1Acnt += amount;
                }
                break;

            case PersonIntial.All:
                {
                    TotalSales = Person1Acnt + Person2Acnt + Person3Acnt;
                }
                break;
        }
    }

convert your input to enum....... and call ProcessInput()

Thanks

Srikanth
  • 980
  • 3
  • 16
  • 30