My Receipt Program

This is my receipt program finished. It runs and everything seems to be ok with every option that you select. What do you think? Is their a simpler way to do it than what I did?

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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include <iostream>
#include <fstream>

using namespace std;

int paymentsel();
int paymentfunc(int x, double y);
int receipt(int x, double y);
int cash(double y);
int endprogram();
int receipt2(int x, double y, double z, double a);

int main()
{
    paymentsel();

    return 0;
}


int paymentsel()
{
    cout << "ENTER TOTAL: ";
    double total;
    cin >> total;

    cout << "1 - CASH" << endl;
    cout << "2 - DEBIT" << endl;
    cout << "3 - CREDIT" << endl << endl;
    cout << "PAYMENT TYPE: ";
    int paymenttype;
    cin >> paymenttype;
    paymentfunc(paymenttype, total);



}

int paymentfunc(int x, double y)
{

    switch (x)
    {
    case 1 :
        cash(y);
        break;
    case 2 :
        receipt(x, y);
        break;
    case 3 :
        receipt(x, y);
        break;
    default :
        cout  << "ERROR" << endl;
        paymentsel();

    }

}

int cash(double y)
{
    double tax;
    tax = y * 0.065;
    double check = tax + y;
    cout << "\nFINAL TOTAL - $" << check << endl;
    cout << "Enter in amount given: ";
    double amount;
    cin >> amount;

    if (amount < check )
    {
        cout << "Error. Insufficient Funds." << endl << endl;
        cash(y);
    }
    receipt2(amount, y, tax, check);

}

int receipt(int x, double y)
{

    ofstream receipt;
    receipt.open ("receipt.txt");
    receipt << "THANK YOU FOR SHOPPING AT S1L3NT" << endl << endl;
    receipt << "================================" << endl << endl;
    receipt << "Your total is - $" << y << endl;
    double tax;
    tax = 0.065 * y;
    receipt << "Tax - $" << tax << endl << endl;
    receipt << "FINAL TOTAL - $" << y + tax << endl << endl;
    receipt << "Total has been charged to card." << endl << endl;
    receipt.close();
    endprogram();
}

int receipt2(int x, double y, double z, double a)
{
    ofstream receipt;
    receipt.open ("receipt.txt");
    receipt << "THANK YOU FOR SHOPPING AT S1L3NT" << endl << endl;
    receipt << "================================" << endl << endl;
    receipt << "Your total is - $" << y << endl;

    receipt << "Tax - $" << z << endl << endl;
    receipt << "FINAL TOTAL - $" << y + z << endl << endl;
    receipt << "Amount Cash Given - $" << x << endl;
    double change = x - a;
    receipt << "Change - $" << change << endl << endl;
    receipt.close();
    endprogram();

}



int endprogram()
{
    cout << "Transaction finished." << endl;
    cout << "Enter 1 to restart: ";
    int one;
    cin >> one;
    if (one == 1)
    {
        paymentsel();
    }
}
A couple of things, first you have functions that you told the compiler would be returning values, yet you don't return anything. Look at the last function, endprogram() you indicate the function will return an int yet you don't return anything. If you don't want your function to return any value then you should be using void for the return type. If you really want the function to return something, return something.

Next in several functions you open a file, but never check to insure the file actually opened. You should always check that your file opens correctly.

Lastly you don't seem to understand how functions return to their calling functions. For example look at these two snippets:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
int paymentsel()
{
    cout << "ENTER TOTAL: ";
...
   paymentfunc(paymenttype, total);



}

int paymentfunc(int x, double y)
{

    switch (x)
    {
...
    default :
        cout  << "ERROR" << endl;
        paymentsel();

    }

}


In the paymentfunction() you call paymentsel() instead of just allowing your function to return naturally to it's calling function. There is no need to call paymetsel() in this case. You are doing things like this in most of your functions. I recommend instead of always calling other functions you investigate ways of letting your functions return to their calling functions instead.

Topic archived. No new replies allowed.