-1

I'm creating a donation application that reads the input in a textbox, converts it to a double. Then using the method operatingCost, it should take that converted double and divide it by 17% (operating fees). Currently in the method, I have the variable dontationBFees coming in and then being divided by 17 and creating a new variable afterFees. Everything is working fine but I need to create a running total that will save all of the donations. It should display the total amount raised for the charity (that is, the total amount donated less all operating costs) for all donations up to that point. I know I need a while loop or do while loop so that the app runs and keeps adding the data. I just don't see why this code isn't producing the running total. I'm looking for help. Is there something I'm overlooking.

   private decimal donationBFees = 0;

    void deductOperatingCost(ref decimal afterFeesParam)
    {
        afterFeesParam = afterFeesParam - (afterFeesParam  / 100 * 17);
    }


    private void Button_Click(object sender, RoutedEventArgs e)
    {
        Boolean set = true;
        do
        {

            String donationBeforeFees;
            decimal totalDonationRaised;

            donationBeforeFees = donationBox.Text;
            donationBFees = System.Convert.ToDecimal(donationBeforeFees);


            decimal afterFees = donationBFees;
            deductOperatingCost(ref afterFees);
            afterFeesBox.Text = afterFees.ToString("$###, ##0.00");

            //This is the for loop I'm using to get the running total
            for (int i = 0; i < afterFees; i++)
            {
                decimal total = 0;
                total += afterFees;
                totalDonationRaised = total;
                totalDonationsBox.Text = totalDonationRaised.ToString("$###, ##0.00");
            }

        } while (set == false);
    }

}

}

Jeff Ryan
  • 19
  • 4
  • If you divide by 17, thats not 17% – Ňɏssa Pøngjǣrdenlarp Oct 19 '18 at 00:16
  • Yes, I know that's why in the code its donation *100 / 17. That gives you a true number to multiple and then divide against to get the percentage. – Jeff Ryan Oct 19 '18 at 00:19
  • You are (trying to) loop inside a button click, which doesn't yield to the UI. You also re-initialize `total` each time in a inner `for` loop. Why do you have the `do` loop that doesn't actually loop? Move the `decimal total = 0` outside the `for` loop and get rid of the `do...while` loop. – Ron Beyer Oct 19 '18 at 01:01
  • It seems like the for loop is locking the system because when I have it in the code the other parts don't even work. – Jeff Ryan Oct 19 '18 at 14:56
  • Ok, I'm now using the TextChanged event to handle part of the running total. Now its displaying the total but its changing after every entry not adding to it. Any thoughts? – Jeff Ryan Oct 19 '18 at 21:27

2 Answers2

0
    private decimal donationBFees = 0;
    private decimal total = 0;
    private decimal afterFees = 0;
    private decimal totalDonationRaised;

    void deductOperatingCost(ref decimal afterFeesParam)
    {
        afterFeesParam = afterFeesParam - (afterFeesParam  / 100 * 17);
    }



    private void Button_Click(object sender, RoutedEventArgs e)
    {


            String donationBeforeFees;


            donationBeforeFees = donationBox.Text;
            donationBFees = System.Convert.ToDecimal(donationBeforeFees);


            decimal afterFees = donationBFees;
            deductOperatingCost(ref afterFees);
            afterFeesBox.Text = afterFees.ToString("$###, ##0.00");

            total = afterFees;
            totalDonationRaised = total;
            totalDonationsBox.Text = totalDonationRaised.ToString("$###, ##0.00");

    }

    private void donationBox_TextChanged(object sender, TextChangedEventArgs e)
    {
        total += afterFees;
        totalDonationRaised = total;
        totalDonationsBox.Text = totalDonationRaised.ToString("$###, ##0.00");
    }
}
Jeff Ryan
  • 19
  • 4
  • Ok, I'm now using the TextChanged event to handle part of the running total. Now its displaying the total but its changing after every entry not adding to it. Any thoughts? – Jeff Ryan Oct 19 '18 at 21:30
0

I'm trying to use this method for the running total but its adding number when I delete them.

    private decimal donationBFees = 0;
    private decimal total = 0;
    private decimal afterFees = 0;
    private decimal totalDonationRaised;

    void deductOperatingCost(ref decimal afterFeesParam)
    {
        afterFeesParam = afterFeesParam - (afterFeesParam  / 100 * 17);
    }

    void runningTotal(ref decimal runningTotalParam)
    {
        runningTotalParam = runningTotalParam + runningTotalParam;
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {


            String donationBeforeFees;


            donationBeforeFees = donationBox.Text;
            donationBFees = System.Convert.ToDecimal(donationBeforeFees);


            decimal afterFees = donationBFees;
            deductOperatingCost(ref afterFees);
            afterFeesBox.Text = afterFees.ToString("$###, ##0.00");

            total = afterFees;
            totalDonationRaised = total;
            totalDonationsBox.Text = totalDonationRaised.ToString("$###, ##0.00");

    }

    private void donationBox_TextChanged(object sender, TextChangedEventArgs e)
    {
        //total += afterFees;

        runningTotal(ref total);
        totalDonationRaised = total;
        totalDonationsBox.Text = totalDonationRaised.ToString("$###, ##0.00");
    }

}
Jeff Ryan
  • 19
  • 4