简体   繁体   中英

Issues with looping code and do-while loops (c#)

static double calculateTotals(double a)
    {
        double transfee = a * .01;
        double total = a + transfee;
        return total;
    }

    static void Main(string[] args)
    {
        Console.WriteLine("How many dontations to process?");
        int donations = Convert.ToInt16(Console.ReadLine());
        int[] count = new int[] { donations + 1 };
        int ct = 1;
        int i = -1;
        do
        {
            Console.WriteLine("Enter name: ");
            string name = Console.ReadLine();
            Console.WriteLine("Enter donation amount: ");
            double amount = Convert.ToDouble(Console.ReadLine());
            double transfee = amount * .01;
            i++;
            ct = count[i += 1];
            Console.WriteLine(name + "\t" + amount + "\t" + transfee);
        } while (i < donations);
        Console.WriteLine("TOTALS:" + "\t" + calculateTotals(amount) + "\t" + transfee);
        Console.ReadLine();
    }
}

Hello. I am a beginner at coding, so I apologize if this is a poor attempt.

I am trying to make an app that records the amount donated by an individual, calculates a transaction fee, and outputs the results for each person. At the end, I am creating a final row of output that will state the total donations and the total transaction fees.

I am currently unsure how to properly implement the array into my loop, and am unsure if the loop is optimized in general.

Again, I am a beginner. I apologize for such code, but I'd love some clarification on these things.

Thank you!

First, your array declaration syntax is wrong. See this link .

So it should be int[] count = new int[donations+1];

Second, you need to declare and instantiate your amount and transfee variables outside of your loop.

        double transfee = 0.0F;
        double amount = 0.0F;
        do
        {
            ...
            amount = Convert.ToDouble(Console.ReadLine());
            transfee = amount * .01;
            ...
        } while (i < donations);

This should be enough information to get you going again. Since you're learning, I don't think anyone would really unfold an answer for you that does the job you're trying to figure out :)

Your code :

        int i = -1;

        do
        {
            ...

            i++;
            ct = count[i += 1];
            ...

        } while (i < donations);

You are actually increase i two times , then get values from count[i] assign to ct variable

See this sample :

        int[] count = new int[3];
        count[0] = 0;
        count[1] = 1;
        count[2] = 2;

        int i = -1;
        do
        {
            i++;
            int x = count[i += 1];
            Console.WriteLine(x);
        } while (i < 3);

It will cause IndexOutOfRangeException

Explain :

First Loop :

i++;                   // i increased 1, so i = 0
int x = count[i += 1]; // i increased 1, so i = 1, then get count[1] assign to x, x is 1

Second loop:

i++;                   // i increased 1, so i = 2
int x = count[i += 1]; // i increased 1, so i = 3, then get count[3] assign to x

count[3] cause IndexOutOfRangeException

Something like count[i += 1] will make your code more difficult to maintain, in my opinion, you should avoid it if possible, try to write it explicity as you can

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM