Calculator help

I'm getting this error when calculating the total bill.

https://prnt.sc/mqf5n6

I kinda know what is the issue, it means if an user does not select one of the options, the value of that said "tp" would be undefined because the user did not select any quantity.

Problem is, i do not know how to fix this. How do i make it so that "tp1,2,3,4" will be defined no matter if the user chooses it or not?

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
86
87
88
89
90
  #include <stdio.h>



int main(void) {                        
                int quantity1, quantity2, quantity3, quantity4;
                double tp1, tp2, tp3, tp4;


                printf("\n\n    Product Code        Price(USD)  \n");
                printf("        [1]              20.20     \n");
                printf("        [2]              14.50     \n");
                printf("        [3]              5.45      \n");
                printf("        [4]              2.80      \n");
                printf("Enter the product code you wish to purchase : ");
                scanf("%d", &choice);
                switch (choice)
                {
                case 1:


                {
                    printf("You have selected product code 1.");
                    printf("\nPlease Enter your Quantity :");
                    scanf("%d", &quantity1);


                    tp1 = quantity1 * 20.20;


                }
                break;


                case 2:
                {
                    printf("You have selected product code 2.");
                    printf("\nPlease Enter your Quantity :");
                    scanf("%d", &quantity2);


                    tp2 = quantity2 * 14.50;


                }
                break;


                case 3:
                {
                    printf("You have selected product code 3.");
                    printf("\nPlease Enter your Quantity :");
                    scanf("%d", &quantity3);


                    tp3 = quantity3 * 5.45;
                }
                break;


                case 4:
                {
                    printf("You have selected product code 4.");
                    printf("\nPlease Enter your Quantity :");
                    scanf("%d", &quantity4);


                    tp4 = quantity4 * 2.80;


                }
                break;


                default:
                {
                    printf("\nInvalid Choice");
                    break;
                }


                }


                double totalbill;
                totalbill = tp1 + tp2 + tp3 + tp4;
                printf("totalbill = %.2f", totalbill);

    return 0;
}
Last edited on
Initialize tp1, tp2, tp3, and tp4 all to 0?
1
2
3
4
5
double tp1, tp2, tp3, tp4;
tp1 = 0;
tp2 = 0;
tp3 = 0;
tp4 = 0;


Or just have one variable, called like "tp" or something, since you never seen to use more than one variable at a time. You can also just have one "quantity" variable.
1
2
3
4
5
6
7
8
9
10
11
12
13
               case 1:

                    scanf("%d", &quantity);
                    tp = quantity * 20.20;
                    break;


                case 2:
                    scanf("%d", &quantity);
                    tp = quantity * 14.50;
                    break;

    .....

...
totalbill = tp;

Last edited on
@Ganado, thank you for the fast reply.

this isn't the full code so the second part is not possible as there is a part in the code that i did not put here where the user is allowed to choose additional product from the list.

1
2
printf("\nDo you want to add-on?(1-Yes, 2-No) : ");
scanf("%lf", &r);


smth like this, but with a loop


sorry for not mentioning earlier
Last edited on
@Ganado, I tried your first recommendation and it worked (thanks alot).

I have another question, at the moment it is just calculating the total bill one by one but that is not what I want when there is an "add on", I want it so that the current selected product price is stored already + the total price of the added on products.

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
86
87
88
89
90
91
92
93
94
do
{                        
                int quantity1, quantity2, quantity3, quantity4;
                double tp1, tp2, tp3, tp4;


                printf("\n\n    Product Code        Price(USD)  \n");
                printf("        [1]              20.20     \n");
                printf("        [2]              14.50     \n");
                printf("        [3]              5.45      \n");
                printf("        [4]              2.80      \n");
                printf("Enter the product code you wish to purchase : ");
                scanf("%d", &choice);
                switch (choice)
                {
                case 1:


                {
                    printf("You have selected product code 1.");
                    printf("\nPlease Enter your Quantity :");
                    scanf("%d", &quantity1);


                    tp1 = quantity1 * 20.20;


                }
                break;


                case 2:
                {
                    printf("You have selected product code 2.");
                    printf("\nPlease Enter your Quantity :");
                    scanf("%d", &quantity2);


                    tp2 = quantity2 * 14.50;


                }
                break;


                case 3:
                {
                    printf("You have selected product code 3.");
                    printf("\nPlease Enter your Quantity :");
                    scanf("%d", &quantity3);


                    tp3 = quantity3 * 5.45;
                }
                break;


                case 4:
                {
                    printf("You have selected product code 4.");
                    printf("\nPlease Enter your Quantity :");
                    scanf("%d", &quantity4);


                    tp4 = quantity4 * 2.80;


                }
                break;


                default:
                {
                    printf("\nInvalid Choice");
                    break;
                }


                }


                double totalbill;
                totalbill = tp1 + tp2 + tp3 + tp4;
                printf("totalbill = %.2f", totalbill);

                double r;

                printf("\nDo you want to add-on?(1-Yes, 2-No) : ");
                scanf("%lf", &r);

}while (r == 1);

    return 0;
}


if you know what i mean?
Last edited on
If you need to accumulate a total bill, you need to keep track of it with a "sum" or "total" variable.

Define double totalbill = 0; outside of the do-while loop, and then do
totalbill += (tp1 + tp2 + tp3 + tp4); within the loop.

Also, are you re-defining "r"? I can't tell. If you are, don't re-define it on line 86.
Last edited on
@Ganado

Yes!! thank you very much. works good now.
really appreciate it

btw how did you know i was re-defining r? i did not even put that part of the code in (i redefined it like you said before the "do"), thanks for letting me know too

http://prntscr.com/mqgq2y
Last edited on
What I mean is, in your loop, you define "double r;", but then the condition for your loop is "while (r == 1);". If you wish to use r as the condition for the while loop, it has to be defined outside of the while loop itself.

For example, the following will not compile:
1
2
3
4
5
6
7
8
int main()
{
    do
    {
        double r = 3;   
    }
    while (r == 2);
}

7:12: error: 'r' was not declared in this scope


The following will compile:
1
2
3
4
5
6
7
8
9
10
11
12
13
// Example program
#include <stdio.h>

int main()
{
    double r = 3;
    do
    {
        printf("loop entered\n");
        r--;
    }
    while (r == 2);
}


Edit: Sounds like you found the issue. Nice job.
Last edited on
@Ganado

I just realized when using the function you gave me
totalbill += (tp1 + tp2 + tp3 + tp4);, it seems like the program is adding itself again whenever i select a different add .on

Here's the gif of what i mean
https://gph.is/g/aXkOqpE

i think it is adding the tp1 again on top of the new add on
Last edited on
If you're running an issue like that, then you need to split up that summation code.
Still keep a "totalbill" variable, but remove the totalbill += tp1 + tp2 + tp3 + tp4 line, and you should do something like this instead:
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
                 case 3:
                {
                    printf("You have selected product code 3.");
                    printf("\nPlease Enter your Quantity :");
                    scanf("%d", &quantity3);


                    tp3 = quantity3 * 5.45;
                    totalbill += tp3;
                }
                break;


                case 4:
                {
                    printf("You have selected product code 4.");
                    printf("\nPlease Enter your Quantity :");
                    scanf("%d", &quantity4);


                    tp4 = quantity4 * 2.80;
                    totalbill += tp4;

                }
                break;

etc.

The other alternative is to make sure you reset tp1, tp2, tp3, and tp3 for 0, EACH loop iteration.
Last edited on
@Ganado

http://prntscr.com/mqx96u

i split up the summation code but i still have to keep the last line right? (ps: i removed the + from the "+=" that was there before)
totalbill = (tp1 + tp2 + tp3 + tp4);


tried running it, it works now and it doesnt add itself everytime.

thank you
Last edited on
And what happens if you do the same product code twice? Does it still correctly add? If so, then yep sounds good.
@Ganado yes it correctly added.

thankyou!
Topic archived. No new replies allowed.