How to sum up the numbers in loop?

Hi, I'm new here, may I know how to sum up numbers in loop?
Example, for quantity of product A, I inserted 1, then loop, the second time, I also inserted 1, so total of the quantity of product A is 2.
This is my code but for the output I got:
DAILY SALES SUMMARY REPORT
PACKAGE Quantity Sold Sales Amount
A 1 24.50
instead of
DAILY SALES SUMMARY REPORT
PACKAGE Quantity Sold Sales Amount
A 2 49.00

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include<stdio.h>
#include<stdlib.h>
#pragma warning(disable:4996)
#define packageA 24.50
#define deliveryfees 5.00

void main()
{   
	int qtyA, QsA = 0;
	float pA, SaA, PACKAGECharges, TotaltoPay, delivery, discounts;
	char yesno;

	for (qtyA = 0; qtyA <= 0; ++qtyA)
	{
		QsA = QsA + qtyA;
		SaA = QsA * packageA;
		do {
			printf("  Enter order quantity of package \n");
			printf("  Package A RM24.50 : ");
			scanf(" %d", &qtyA);

			pA = qtyA * packageA;
			PACKAGECharges = pA;
			if (PACKAGECharges < 80) {
				delivery = deliveryfees;
				discounts = 0.00;
				TotaltoPay = PACKAGECharges + delivery;
			}
			else {
				delivery = 0.00;
				discounts = PACKAGECharges * 0.15;
				TotaltoPay = PACKAGECharges + delivery - discounts;
			}

			printf("               CUSTOMER ORDERS             \n");
			printf("     PACKAGE A %d @ 24.50 = RM %6.2f \n", qtyA, pA);
			printf("\n");
			printf("     PACKAGE Charges = RM %6.2f \n", PACKAGECharges);
			printf("     Delivery Fees   = RM %6.2f \n", delivery);
			printf("     Discounts       = RM %6.2f \n", discounts);
			printf("     Total to Pay    = RM %6.2f \n", TotaltoPay);
			printf("\n");



			printf("Next customer (Y=yes?) : ");
			scanf(" %c", &yesno);

		} while (yesno == 'Y' || yesno == 'y');

		if (yesno = 'N' || yesno == 'n')
		{
			QsA = QsA + qtyA;
			SaA = QsA * packageA;
			printf("           DAILY SALES SUMMARY REPORT      \n");
			printf("PACKAGE\t\tQuantity Sold\tSales Amount\n");
			printf("   A\t\t%d\t\t%-0.2f\n", QsA, SaA);

		}


	}
	system("pause");
}

Thank you for helping :)
Last edited on
Hello AkiraC,

Still going over the code, but what do expect from: for (qtyA = 0; qtyA <= 0; ++qtyA)? How many times do you think this will loop?

Andy
Shouldn't L15, 16 be after L20?

L53,54 aren't needed.
Last edited on
@Handy. Note that qtyA is being changed within the loop body on L20
@seeplus,

Yes I saw that after running the program.

Andy
Hello AkiraC,

Going over your code I still have no idea what "QsA" and "SaA" are for. I am thinking that they are for a total quantity and total sales?

This is not completely fixed, but a start:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#pragma warning(disable:4996)

#include<stdio.h>
#include<stdlib.h>


#define PACKAGE_A 24.50f
#define DELIVERY_FEES 5.00f

void main()
{
    int qtyA = 0, QsA = 0, totalQty = 0;  // <--- ALWAYS initialize all your variables.
    float 
        pA = 0.0, 
        SaA = 0.0, 
        PACKAGECharges = 0.0, 
        TotaltoPay = 0.0, 
        delivery = 0.0, 
        discounts = 0.0, 
        totalPackageCharge = 0.0;
    char yesno = '\0';

    for (qtyA = 0; qtyA <= 0; ++qtyA)
    {
        QsA = QsA + qtyA;
        SaA = QsA * PACKAGE_A;

        do
        {
            printf(
                "\n  Enter order quantity of package \n"
                "  Package A RM24.50 : ");
            scanf(" %d", &qtyA);  // <--- Return value ignored.

            pA = qtyA * PACKAGE_A;
            PACKAGECharges = pA;

            totalQty += qtyA;
            totalPackageCharge += pA;

            if (PACKAGECharges < 80)
            {
                delivery = DELIVERY_FEES;
                discounts = 0.00;
                TotaltoPay = PACKAGECharges + delivery;
            }
            else
            {
                delivery = 0.00;
                discounts = PACKAGECharges * 0.15f;
                TotaltoPay = PACKAGECharges + delivery - discounts;
            }

            printf(
                "\n               CUSTOMER ORDERS             \n"
                "     PACKAGE A %d @ 24.50 = RM %6.2f \n"
                "\n"
                "     PACKAGE Charges = RM %6.2f \n"
                "     Delivery Fees   = RM %6.2f \n"
                "     Discounts       = RM %6.2f \n"
                "     Total to Pay    = RM %6.2f \n", qtyA, pA, PACKAGECharges, delivery, discounts, TotaltoPay);
            //printf("\n");

            printf("\n Next customer (Y=yes?) : ");
            scanf(" %c", &yesno);  // <--- Return value ignored.

        } while (yesno == 'Y' || yesno == 'y');

        if (yesno = 'N' || yesno == 'n')
        {
            QsA = QsA + qtyA;
            SaA = QsA * PACKAGE_A;

            printf(
                "\n           DAILY SALES SUMMARY REPORT      \n"
                "PACKAGE\t\tQuantity Sold\tSales Amount\n"
                "   A\t\t%d\t\t%-0.2f\n", totalQty, totalPackageCharge);

        }
    }

    printf("\n\n");  // <--- Added.

    system("pause");
}

For lines 7 and 8 you are making 2 constant variables. These tend to work better using capital letters. Also "24.50" is considered a "double" with my IDE, but it is more set up for C++ not C. If your compiler is for C programs adding the "f" after the number may not be required. For me it just eliminated some warnings going from a "double to a "float".

It may just be a language difference, but I added the variables "totalQty" and "totalPackageCharge", so I could understand it better.

The for loop only loops 1 time and since all the work is done in the do/while loop there is not much point in it.

As seeplus noted lines 25 and 26 are in the wrong place to be of any use.

You do not need a "printf" statement for every line. Most lines should be able to be combined, as I have demonstrated.

In my IDE MSVS 2019 each string in double quotes is considered as just 1 big string.

Lines 71 and 72 I did not do anything with because mostly I did not use those variables. As seeplus noted I do not believe they do any good there.

The code is an idea of what could be done. Feel free to change it to use your original variables if you want.

The output I get:

  Enter order quantity of package
  Package A RM24.50 : 1

               CUSTOMER ORDERS
     PACKAGE A 1 @ 24.50 = RM  24.50

     PACKAGE Charges = RM  24.50
     Delivery Fees   = RM   5.00
     Discounts       = RM   0.00
     Total to Pay    = RM  29.50

 Next customer (Y=yes?) : y

  Enter order quantity of package
  Package A RM24.50 : 2

               CUSTOMER ORDERS
     PACKAGE A 2 @ 24.50 = RM  49.00

     PACKAGE Charges = RM  49.00
     Delivery Fees   = RM   5.00
     Discounts       = RM   0.00
     Total to Pay    = RM  54.00

 Next customer (Y=yes?) : y

  Enter order quantity of package
  Package A RM24.50 : 3

               CUSTOMER ORDERS
     PACKAGE A 3 @ 24.50 = RM  73.50

     PACKAGE Charges = RM  73.50
     Delivery Fees   = RM   5.00
     Discounts       = RM   0.00
     Total to Pay    = RM  78.50

 Next customer (Y=yes?) : n

           DAILY SALES SUMMARY REPORT
PACKAGE         Quantity Sold   Sales Amount
   A            6               147.00


Press any key to continue . . .



Andy
Hello AkiraC,

I believe that I have the program revised to what it needs. I am a little rust with C, so the last "printf" may be improved upon.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
//#pragma warning(disable:4996)  // <--- Not needed if you use "scanf_s". Notice difference in line 61.

#include<stdio.h>
#include<stdlib.h>

#define PACKAGE_A     24.50f
#define DELIVERY_FEES  5.00f
#define DISCOUNT_AMT   0.15f

void main()
{
    char yesno = '\0', c = '\0';  // <--- ALWAYS initialize all your variables.
    int qtyA = 0, totalQty = 0;
    float 
        packageCharges = 0.0, 
        totaltoPay = 0.0, 
        delivery = 0.0, 
        discounts = 0.0, 
        totalPackageCharge = 0.0;

    do
    {
        while (printf(
            "\n  Enter order quantity of package \n"
            "  Package A RM%.2f: ", PACKAGE_A) && !scanf_s(" %d", &qtyA))  // <--- Changed.
        {
            printf("\n     Input Failure! Must be a number. Try again\n\n");

            clearerr(stdin);

            while ((c = getchar()) != '\n' && c != EOF) {}
        }

        packageCharges = qtyA * PACKAGE_A;  // <--- Changed.

        totalQty += qtyA;
        totalPackageCharge += packageCharges;

        if (packageCharges < 80)
        {
            delivery = DELIVERY_FEES;
            totaltoPay = packageCharges + delivery;
            //discounts = 0.00;  // <--- Not needed if variable is initialized when defined.
        }
        else
        {
            discounts = packageCharges * DISCOUNT_AMT;
            totaltoPay = packageCharges + delivery - discounts;
            //delivery = 0.00;  // <--- Not needed if variable is initialized when defined.
        }

        printf(
            "\n               CUSTOMER ORDERS\n"
            "     PACKAGE A %d @ %.2f = RM %.2f \n"
            "\n"
            "     PACKAGE Charges = RM %8.2f \n"
            "     Delivery Fees   = RM %8.2f \n"
            "     Discounts       = RM %8.2f \n"
            "     Total to Pay    = RM %8.2f \n", qtyA, PACKAGE_A, packageCharges, packageCharges, delivery, discounts, totaltoPay);

        while (printf("\n Next customer (Y = yes / N = No?): ") && !scanf_s(" %c", &yesno, sizeof(yesno)))  // <--- Changed.
        {
            printf("\n     Input Failure!\n\n");  // <--- This may not be reached because imput is to a "char".

            while ((c = getchar()) != '\n' && c != EOF) {}
        }

    } while (yesno == 'Y' || yesno == 'y');

    printf(
        "\n         DAILY SALES SUMMARY REPORT\n"  // <--- Spaces at end of string are not needed.
        "--------------------------------------------\n\n"  // <--- Added.
        "PACKAGE        Quantity Sold    Sales Amount\n"
        "--------------------------------------------\n"  // <--- Added.
        "   A                %d           %.2f\n\n\n", totalQty, totalPackageCharge);

    system("pause");
}

Line 25 makes use of the constant "PACKAGE_A", so if that price ever changes it will change in the program. It eliminates the need for having to search through the code to find magic numbers to change. The same is true for "DISCOUNT_AMT" should it ever change.

The while loop at line 23 needs some work to deal with a failed "scanf_s", which it does for now, and a "qtyA" of less than 1.

See what you think.

Andy
Hello AkiraC,

I finally figured it out
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int qtyA = 0, totalQty = 0, response = 0;


while (printf(
    "\n  Enter order quantity of package \n"
    "  Package A RM%.2f: ", PACKAGE_A) && !(response = scanf_s(" %d", &qtyA)) || qtyA < 1)  // <--- Changed.
{
    if(!response)
    {
        printf("\n     Input Failure! Must be a number. Try again\n");

        clearerr(stdin);

        while ((c = getchar()) != '\n' && c != EOF) {}
    }
    else if (qtyA < 1)
    {
        printf("\n     Can not have a quantity of 0 or less.\n");
    }
}

There may still be something better I am just not sure what it is at the moment.

Andy
@Handy @seeplus
I had change the code according to your codes, and it works. I had found out my mistakes and I'll take note when doing future codes. Thank you for helping! :)
Topic archived. No new replies allowed.