A Lil point of sales

During the past few days I've been trying to write a small point of sales program and at the moment i got it to work kinda :\ but it has one little error the total keeps being 18. i think this is because my purchase is initialized to 10. i am trying to make the program just ask which items are being purchased ( how ever many of them) and then total it and then ask for how much cash the person has and subtract that and give the change. This program is part of a project i am trying to finish. Any help would be much appreciated.
This is the program:

#include<stdio.h>
#include<conio.h>
#include<string.h>
#define Next "thank you and come again"
int main()
{
int purchase = 10;
char cj[6] = { 'a', 'b', 'c', 'd', 'e', 'f' };
int a, b, c, d, e, f, j;
int cash, total, change;
a = 7; b = 3; c = 4; d = 5; e = 1; f = 10;
printf("Sal The Vender\n");
printf("\n\n (a) vitamalt---7");
printf("\n\n (b) caprison---3");
printf("\n\n (c) gatorade---4");
printf("\n\n (d) orange juice---5");
printf("\n\n (e) water---1");
printf("\n\n (f) tropicana---10");
for (j = 0; j < purchase; j++){
switch (purchase){
case 'a':
printf("\n (a) vitamalt---7");
break;
case 'b':
printf("\n (b) caprison---3");
break;
case 'c':
printf("\n (c) gatorade---4");
break;
case 'd':
printf("\n (d) orange juice---5");
break;
case 'e':
printf("\n (e) water---1");
break;
case 'f':
printf("\n (f) tropicana---1");
break;

}
total = j + j;
}
printf("\n total=%d", total);
printf("\n cash:");
scanf_s("%d", &cash);
change = cash - total;
printf("\n change=%d", change);
printf("\n\n%s", Next);
_getch();

}



I am trying to make the program do something like this
2 waters , 1 vitamalt and a caprison and total that and get me the change once i enter the cash value
When posting code, please put it in code tags. Highlight the code and click the "<>" button on the right side of the edit window. Here is your code indented and in code tags. More comments below:
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
#include<stdio.h>
#include<conio.h>
#include<string.h>
#define Next "thank you and come again"
int 
main()
{
    int purchase = 10;
    char cj[6] =
    {'a', 'b', 'c', 'd', 'e', 'f'};
    int a, b, c, d, e, f, j;
    int cash, total, change;
    a = 7;
    b = 3;
    c = 4;
    d = 5;
    e = 1;
    f = 10;
    printf("Sal The Vender\n");
    printf("\n\n (a) vitamalt---7");
    printf("\n\n (b) caprison---3");
    printf("\n\n (c) gatorade---4");
    printf("\n\n (d) orange juice---5");
    printf("\n\n (e) water---1");
    printf("\n\n (f) tropicana---10");
    for (j = 0; j < purchase; j++) {
	switch (purchase) {
	case 'a':
	    printf("\n (a) vitamalt---7");
	    break;
	case 'b':
	    printf("\n (b) caprison---3");
	    break;
	case 'c':
	    printf("\n (c) gatorade---4");
	    break;
	case 'd':
	    printf("\n (d) orange juice---5");
	    break;
	case 'e':
	    printf("\n (e) water---1");
	    break;
	case 'f':
	    printf("\n (f) tropicana---1");
	    break;

	}
	total = j + j;
    }
    printf("\n total=%d", total);
    printf("\n cash:");
    scanf("%d", &cash);
    change = cash - total;
    printf("\n change=%d", change);
    printf("\n\n%s", Next);
    _getch();
    return 0;
}


What's supposed to happen in the loop at lines 26-49? This is what it actually does:
- the switch statement does nothing because purchase is 10 throughout the loop. So the loop is equivalent to:
1
2
3
    for (j = 0; j < purchase; j++) {
	total = j + j;
    }

And since that overwrites total at each pass, the only pass that matters is the last one. So it's the same as :
1
2
j = purchase - 1;
total = j + j;

Since purchase = 10, that means total gets set to 18.
Oh okay , so how can i fix that issue because trust me I've been trying to fix that but i don't know what to do so that the switch function can execute.
I don't see any code to input the purchase.
- add code after line 25 to ask the user to enter a letter and then get the input from them.
- Then you can execute the switch statement. In addition to printing their selection, you'll want to set "total" to the value of the item purchased.
Topic archived. No new replies allowed.