Please check and fix my C++ program. I don't know why my output is different with the
Question:
Please check and fix my C++ program. I don't know why my output is different with the expected output.
Here is my program or coding:
#include
#include
#include
using namespace std;
// Function prototypes
void welcome();
void displayMenu();
void readOption(int &option);
void readInt(string prompt, int &num);
void readDouble(string prompt, double &num);
void placeOrder(double &cost);
double tipDiscount(double &tip, double &discount, double cost);
int main() {
welcome();
int option;
do {
displayMenu();
readOption(option);
if (option == 1) {
double totalCost = 0.0;
placeOrder(totalCost);
double tip, discount;
double finalTotal = tipDiscount(tip, discount, totalCost);
cout << fixed << setprecision(2);
cout << "\nYour final total is: $" << finalTotal << endl;
if (discount > 0.0) {
cout << "You get a " << (discount == 0.10 ? "10%" : "5%") << " discount!" << endl;
cout << "Your discount is $" << discount << endl;
}
cout << "Your total after tip and discount is: $" << finalTotal - discount << endl;
} else if (option != 2) {
cout << "Invalid Option! Please choose 1 or 2!" << endl;
}
} while (option != 2);
cout << "\nThank you for using my program!\n";
return 0;
}
// Function implementations
void welcome() {
cout << "Welcome to my Food Cart Program!\n";
}
void displayMenu() {
cout << "\nWhat would you like to do today?\n";
cout << "Pick an option from below:\n";
cout << "1. Place an order\n";
cout << "2. Quit\n";
}
void readOption(int &option) {
do {
readInt(">> ", option);
if (option != 1 && option != 2) {
cout << "Invalid Option! Please choose 1 or 2!\n";
}
} while (option != 1 && option != 2);
}
void readInt(string prompt, int &num) {
cout << prompt;
cin >> num;
while (!cin || num < 1 || num > 2) {
cout << "Invalid Optione! Please choose 1 or 2: ";
cin.clear();
cin.ignore(100, '\n');
cin >> num;
}
cin.ignore(10, '\n');
}
void readDouble(string prompt, double &num) {
cout << prompt;
cin >> num;
while (!cin || num < 0.0) {
cout << "Invalid data! Please enter a non-negative number: ";
cin.clear();
cin.ignore(100, '\n');
cin >> num;
}
cin.ignore(10, '\n');
}
void placeOrder(double &cost) {
char anotherItem;
do {
string itemName;
double itemCost;
cout << "Enter the name of your item: ";
getline(cin, itemName);
readDouble("Enter the cost of your item $: ", itemCost);
cost += itemCost;
do {
cout << "Do you want another item? (y/n): ";
cin >> anotherItem;
cin.ignore(10, '\n');
if (tolower(anotherItem) != 'y' && tolower(anotherItem) != 'n') {
cout << "Invalid Option! Please choose y or n!\n";
}
} while (tolower(anotherItem) != 'y' && tolower(anotherItem) != 'n');
} while (tolower(anotherItem) == 'y');
}
double tipDiscount(double &tip, double &discount, double cost) {
readDouble("Enter the amount of tip you want to add $: ", tip);
double total = cost + tip;
if (total > 50.0) {
discount = total * 0.10;
} else if (total > 35.0 && total <= 50.0) {
discount = total * 0.05;
} else {
discount = 0.0;
}
return total - discount;
}
Here is my output ( the one that I highlighted is where are the difference)
Building Java Programs A Back To Basics Approach
ISBN: 9780135471944
5th Edition
Authors: Stuart Reges, Marty Stepp