You are using a version without Ads of this website. Please, consider donating:

### 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?

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127`` ``````#include #include 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:
 ``1234567891011121314151617181920212223`` ``````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.