point of sale problem

i want to make my program compute for the change of the customer.
for example if the total amout due is $90 and the customer paid $100. the program should answer $10. if the customer paid less the program should tell invalid.


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
95
96
97
#include <iostream>
#include <iomanip>
#include <string>
#include <conio.h>

using namespace std;

void display_menu();
void tax(float);
float total = 0;

int main() {
    char choice;
    bool more = true;
    cout << setiosflags(ios::showpoint) << setiosflags(ios::fixed) << setprecision(2);

    while(more) { //loop until the user wants more
        cout << "-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n";
        cout << "Welcome to Jimmy's Snack Bar!" << endl;
        display_menu();
        cout << "Please enter the letter of your selection: ";
        cin >> choice;
        switch (choice) {
        case 'S':
        case 's':
            cout << "Sandwich - P30.00" << endl;
            total+=30;
            break;
        case 'C':
        case 'c':
            cout << "Chips - P50.00" << endl;
            total+=50;
            break;
        case 'P':
        case 'p':
            cout << "Pansit - P20.00" << endl;
            total+=20;
            break;
        case 'H':
        case 'h':
            cout << "Hotdog - P25.00" << endl;
            total+=25;
            break;
        case 'D':
        case 'd':
            cout << "Coke - P15.00" << endl;
            total+=15;
            display_menu();
            break;
        case 'B':
        case 'b':
            cout << "Burger - P25.00" << endl;
            total+=25;
            break;
        case 'X' :
        case 'x':
            cout << "Canceled, please start over." << endl;
            total = 0;
            break;
        case 'T' :
        case 't':
            tax(total);
            more = false; //stop the loop
            return 0; //end the program
            break;
        default:
            cout << "Ooops! something went wrong.. :(\n\n";
            return 0; //end the program
        }
        cout << "Your current total is: $ "<< total << "\n\n";
    }
    return 0;
}




void display_menu() {
    cout << endl;
    cout << "S - Sandwich P30.00" << endl;
    cout << "C - Chips P50.00" << endl;
    cout << "P - Pansit P20.00" << endl;
    cout << "H - Hotdog P25.00" << endl;
    cout << "D - Coke P15.00" << endl;
    cout << "B - Burger P25.00" << endl;
    cout << "X - Cancel sale and start over" << endl;
    cout << "T - Total the sale" << endl;
    cout << "All items have additional 12% tax." << endl;
}


void tax(float total) {
    cout << endl;
    cout << "Sub-total: " << total << endl;
    cout << "+ Tax : " << total*0.12 << endl;
    cout << "Total : " << total + (total*0.12) << endl;
}
printf("enter amount \n");
     scanf("%f", &cash);
     cout<<"change:" <<cash-total+(total*0.0825);

After you calculate the tax on line 62, you can ask the user to enter how much they want to pay (use the standard input stream cin). To handle invalid input, you could use cin.fail() as described in this thread: http://www.cplusplus.com/forum/beginner/2957/

Once the input has been verified and you have your amount, you'd need a conditional statement to ensure that the payment amount is greater than or equal to the total with tax. If it isn't, handle it how you want (i.e. print invalid). If it is, calculate the change as the difference between the payment amount and the total, and print it or something.

Hope that helps.

P.S. What currency is "P25.00"?
its pesos.

if i am right cin fail only tells if you input the value correctly. but does not actually subtract the cash given by the customer and the actual cost. to tell you frankly i don't understand that codes quite well. i am just a noob here in c++ prgoramming
philippine peso? just wondering..
yup.
hey ya its you again!! mind if you help me here again?
yeah i could help you by pointing you to this awesome tutorial http://cplusplus.com/doc/tutorial/control/ but not solving the problem again for you..
closed account (iw0XoG1T)
This is one way of many---probably not the best. Am I wrong but haven't you posted this before?
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
#include <iostream>
using namespace std;

class Not_valid_choice{};

void display_menu()
{
    cout << endl;
    cout << "S - Sandwich P30.00" << endl;
    cout << "C - Chips P50.00" << endl;
    cout << "P - Pansit P20.00" << endl;
}

bool choice()
{
    display_menu();
    cout << "Make a choice or enter q to quit\n";
    char choice;
    cin >> choice;
    switch (choice)
    {
    case 'S':
    case 's':
        cout << "Sandwich - P30.00" << endl;
        break;
    case 'C':
    case 'c':
        cout << "Chips - P50.00" << endl;
        break;
    case 'P':
    case 'p':
        cout << "Pansit - P20.00" << endl;
        break;
    case 'H':
    case 'h':
        cout << "Hotdog - P25.00" << endl;
        break;
    case 'q':
    case 'Q':
        cout << "good bye\n";
        return true;
    default:
        throw Not_valid_choice{};
    }
    return false;
}

void perform()
{
    try
    {
        while (true)
        {
            if (choice()) break;
        }
    }
    catch (Not_valid_choice)
    {
        cout << "not a valid choice" << endl;
        perform();
    }
}

int main()
{
    perform();
    return 0;
}


edit:
I would delete this post out of embarrassment, but it is considered bad form.
Please ignore -- I misread your question and confused it with another similar post.
Last edited on
Topic archived. No new replies allowed.