Hello, I have written this program in C++ that is supposed to create a food menu
ID: 3554673 • Letter: H
Question
Hello,
I have written this program in C++ that is supposed to create a food menu and calculate the amount that is supposed to be paid as well as your change.The program as of now is working perfectly fine, but I would like some help on creating this program in functions. Below is the program that is supposed to be converted into function and I appreciate any kind of help in advance:
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdlib>
using namespace std;
int main()
{
ofstream oFile;
int choice=0, quantity;
double subtotal=0, tax=.1, total, change, amount;
oFile.open("Rec.txt");
cout << fixed << showpoint;
cout << setprecision(2);
oFile << "Taco Hut" << endl;
oFile << "Keep Receipt for your records" << endl;
oFile << "Items Purchased detailled below" << endl;
oFile << "**********************************" << endl;
while(choice!=4)
{
system("CLS");
cout << "Welconme to Taco Hut!" << endl;
cout << "May I take your order" << endl;
cout << "1. Taco" << endl;
cout << "2. Burrito" << endl;
cout << "3. Coke" << endl;
cout << "Subtotal: $" << subtotal << endl;
cout << "Enter Selection (4 to Complete Sale): ";
cin >> choice;
if(choice!=4)
{
cout << "Quantity: ";
cin >> quantity;
if(choice==1)
{
subtotal=subtotal+(quantity*1.99);
oFile << quantity << " Taco @ 1.99 = $" << quantity * 1.99 << endl;
}
if(choice==2)
{
subtotal=subtotal+(quantity*2.99);
oFile << quantity << " Burrito @ 2.99 = $" << quantity * 2.99 << endl;
}
if(choice==3)
{
subtotal=subtotal+(quantity*1.50);
oFile << quantity << " Coke @ 1.50 = $" << quantity * 1.50 << endl;
}
}
}
oFile << "**********************************" << endl;
cout << "Subtotal: $" << subtotal << endl;
oFile << "Subtotal: $" << subtotal << endl;
cout << "Tax: $" << tax*subtotal << endl;
oFile << "Tax: $" << tax*subtotal << endl;
total = subtotal + (subtotal*tax);
cout << "Total: $" << total << endl << endl;
oFile << "Total: $" << total << endl << endl;
cout << "Enter Amount Paid: $";
cin >> amount;
oFile << "Enter Amount Paid $" << amount << endl;
change = amount - total;
cout << "Your Change is: $" << change << endl << endl;
oFile << "Your Change is: $" << change << endl << endl;
cout << "Thank You! Come Again!" << endl;
oFile << "Thank You! Come Again!";
oFile.close();
system("pause");
return 0;
}
Explanation / Answer
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdlib>
using namespace std;
void taco(int quantity,double &subtotal,ofstream &oFile)
{
subtotal=subtotal+(quantity*1.99);
oFile << quantity << " Taco @ 1.99 = $" << quantity * 1.99 << endl;
}
void burrito(int quantity,double &subtotal,ofstream &oFile)
{
subtotal=subtotal+(quantity*2.99);
oFile << quantity << " Burrito @ 2.99 = $" << quantity * 2.99 << endl;
}
void coke(int quantity,double &subtotal,ofstream &oFile)
{
subtotal=subtotal+(quantity*1.50);
oFile << quantity << " Coke @ 1.50 = $" << quantity * 1.50 << endl;
}
int main()
{
ofstream oFile;
int choice=0, quantity;
double subtotal=0, tax=.1, total, change, amount;
oFile.open("Rec.txt");
cout << fixed << showpoint;
cout << setprecision(2);
oFile << "Taco Hut" << endl;
oFile << "Keep Receipt for your records" << endl;
oFile << "Items Purchased detailled below" << endl;
oFile << "**********************************" << endl;
while(choice!=4)
{
system("CLS");
cout << "Welconme to Taco Hut!" << endl;
cout << "May I take your order" << endl;
cout << "1. Taco" << endl;
cout << "2. Burrito" << endl;
cout << "3. Coke" << endl;
cout << "Subtotal: $" << subtotal << endl;
cout << "Enter Selection (4 to Complete Sale): ";
cin >> choice;
if(choice!=4)
{
cout << "Quantity: ";
cin >> quantity;
if(choice==1)
{
taco(quantity,subtotal,oFile);
}
if(choice==2)
{
burrito(quantity,subtotal,oFile);
}
if(choice==3)
{
coke(quantity,subtotal,oFile);
}
}
}
oFile << "**********************************" << endl;
cout << "Subtotal: $" << subtotal << endl;
oFile << "Subtotal: $" << subtotal << endl;
cout << "Tax: $" << tax*subtotal << endl;
oFile << "Tax: $" << tax*subtotal << endl;
total = subtotal + (subtotal*tax);
cout << "Total: $" << total << endl << endl;
oFile << "Total: $" << total << endl << endl;
cout << "Enter Amount Paid: $";
cin >> amount;
oFile << "Enter Amount Paid $" << amount << endl;
change = amount - total;
cout << "Your Change is: $" << change << endl << endl;
oFile << "Your Change is: $" << change << endl << endl;
cout << "Thank You! Come Again!" << endl;
oFile << "Thank You! Come Again!";
oFile.close();
system("pause");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.