Stuck on this computer science assignment Here\'s what the items.txt file has: b
ID: 3761536 • Letter: S
Question
Stuck on this computer science assignment
Here's what the items.txt file has:
buster_sword 100.0
keyblade 15.65
master_sword 92.72
roses_sword 8.25
red_potion 20.3
blue_potion 2.32
green_potion 1.12
Here's what we are starting in:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
const int MAXPURCHASES = 5;
const double SALESTAX = 0.05;
void displayMenuFromFile();
double findCostOfOneItemFromFile(string itemNameToFind);
double CalculateTotalCostIncludingTax(double purchase_prices[], int number_of_purchases);
void addNewItemToFileFromUserInput();
void PrintReceipt(double purchase_prices[], string purchase_names[], int number_of_purchases);
int main(){
}
Here's an example of the solution:
Enter an item name to purchase, or "Check_Out", or "Add_Item":Check_Out
You purchased
buster_sword 100
keyblade 15.65
For a total of:121.433
Explanation / Answer
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
const int MAXPURCHASES = 5;
const double SALESTAX = 0.05;
void displayMenuFromFile();
double findCostOfOneItemFromFile(string itemNameToFind);
double CalculateTotalCostIncludingTax(double purchase_prices[], int number_of_purchases);
void addNewItemToFileFromUserInput();
void PrintReceipt(double purchase_prices[], string purchase_names[], int number_of_purchases);
int main()
{
int numberOfPurchases = 0, quantity;;
string choice, purchase_name[MAXPURCHASES];
double purchase_prices[MAXPURCHASES], price;
while(numberOfPurchases < MAXPURCHASES) //While number of purchases are less than 5.
{
displayMenuFromFile(); //Display Menu.
cout<<"Enter your choice: "<<endl;
cout<<"Enter item name to purchase it."<<endl;
cout<<"Enter Add_Item to add a new item to catalogue."<<endl;
cout<<"Enter Check_Out to check out."<<endl;
cout<<"Enter your choice: "; //Read user choice.
cin>>choice;
if(choice == "Add_Item") //If the choice is Add_Item
{
addNewItemToFileFromUserInput(); //Go to relavent function.
}
else if(choice == "Check_Out") //Else if choice is Check_Out
{
break; //Get out of the loop.
}
else //Else, i.e., purchase the item.
{
cout<<"Enter the quantity to purchase: ";//Read the quantity to purchase.
cin>>quantity;
price = findCostOfOneItemFromFile(choice); //Find the price of that item.
if(price != -1) //If price is NOT negative, which means product does'nt exist.
{
purchase_name[numberOfPurchases] = choice; //Add that product to pruchase_name array.
purchase_prices[numberOfPurchases] = findCostOfOneItemFromFile(choice) * quantity; //Add that price to purchase_prices array.
numberOfPurchases++; //Increment the product count.
}
}
}
PrintReceipt(purchase_prices, purchase_name, numberOfPurchases);
}
void displayMenuFromFile() //Display the menu, from file..
{
string itemName;
double itemPrice;
ifstream ipFile;
ipFile.open("items.txt");
cout<<"Menu: "<<endl;
while(!ipFile.eof())
{
ipFile>>itemName;
ipFile>>itemPrice;
cout<<itemName<<" "<<fixed<<setprecision(2)<<itemPrice<<endl;
}
}
//Find the cost of the product, given the name.
//If the product doesn't exist, returns -1 as the cost, else returns the cost of that item.
double findCostOfOneItemFromFile(string itemNameToFind)
{
string itemName;
double itemPrice;
ifstream ipFile;
ipFile.open("items.txt"); //Open the file for reading.
while(!ipFile.eof()) //While not reached endOffile.
{
ipFile>>itemName; //Read the itemName.
ipFile>>itemPrice; //Then its price.
if(itemName == itemNameToFind) //If this is what you are searching from.
return itemPrice; //Return the relavent price.
}
return -1; //If the item doesn't exist, return -1.
}
//Calculates the total amount to be paid by the customer, given the purchase_prices array.
double CalculateTotalCostIncludingTax(double purchase_prices[], int number_of_purchases)
{
double totalPurchase = 0.0;
for(int i = 0; i < number_of_purchases; i++) //For each element.
totalPurchase += purchase_prices[i]; //Add the price to summation.
totalPurchase = totalPurchase + totalPurchase * SALESTAX; //Add the SALESTAX.
return totalPurchase; //Return the final amount.
}
//Adds a new item to the MenuFile.
void addNewItemToFileFromUserInput()
{
string itemNameToAdd, itemName;
double itemPriceToAdd, itemPrice;
cout<<"Enter the name of the item to add: "; //Reads the name of the item to add.
cin>>itemNameToAdd;
cout<<"Enter the price: "; //Reads the price of the item to add.
cin>>itemPriceToAdd;
while(itemPriceToAdd < 0) //Assures the price is not negative.
{
cout<<"Price cannot be negative. Enter a valid price.";
cout<<endl<<"Enter the price: ";
cin>>itemPriceToAdd;
}
ifstream ipFile; //Opens the file to check whether the item already exists.
ipFile.open("items.txt");
bool exists = false;
while(!ipFile.eof())
{
ipFile>>itemName; //Reads the name of the item.
ipFile>>itemPrice; //Reads the price of the item.
if(itemName == itemNameToAdd) //If the name already exists.
{
exists = true; //Set the variable exists to true.
break;
}
}
ipFile.close(); //Closes the file from readmode.
if(exists == true) //If the file already exists.
cout<<"This item already exists."; //Print relavent message.
else //Else
{
ofstream opFile; //Open the file in append mode.
opFile.open("items.txt", ios::app);
opFile<<itemNameToAdd<<" "<<fixed<<setprecision(2)<<itemPriceToAdd<<endl; //Add the name and price.
}
}
//Print the receipt of the purchases.
void PrintReceipt(double purchase_prices[], string purchase_names[], int number_of_purchases)
{
double totalAmount;
cout<<"You purchased"<<endl;
for(int i = 0; i < number_of_purchases; i++)
cout<<purchase_names[i]<<" "<<purchase_prices[i]<<endl;
totalAmount = CalculateTotalCostIncludingTax(purchase_prices, number_of_purchases);
cout<<"For a total of: "<<fixed<<setprecision(2)<<totalAmount<<endl;
}