Program must be written in C++: Your favorite shoe store is having a sale – all
ID: 3840023 • Letter: P
Question
Program must be written in C++:
Your favorite shoe store is having a sale – all pairs of shoes are 35% off! Write a program that reads shoe prices in stock at the local store from an input file, shoe.txt. Each row in the file contains a shoe serial number and price. Calculate the sale price of every pair of shoes, and what the final cost of the shoes is when the sales tax is applied to the discounted price. The calculated data must be saved in an output file, shoeSale.txt. You MUST use a named constant for both the tax rate (8.5%) and discount rate (35%). Since the results displayed are monetary values, your output must be displayed with two decimal places of precision. Be sure decimals “line up” when you output the information. Sample input file: 234019 75.00 234490 124.99 347269 50.00 239801 149.99 487241 99.99 982111 175.00 Sample output file: 234019 52.89 234490 88.15 347269 35.26 239801 105.78 487241 70.51 982111 123.41
Explanation / Answer
shoe.txt (Save this file under D Drive.Then the path of the file pointing to it is D:\shoe.txt)
234019 75.00
234490 124.99
347269 50.00
239801 149.99
487241 99.99
982111 175.00
____________________
#include <fstream>
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
// Declaring constants
const double discount_rate = 0.35;
const double tax_rate = 0.085;
// Declaring variables
int id;
double price;
double finalPrice;
// defines an input stream for the data file
ifstream dataIn;
// Defines an output stream for the data file
ofstream dataOut;
// Opening the input file
dataIn.open("D:\shoe.txt");
if (dataIn.fail())
{
cout << "** File Not Found **";
return 1;
}
else
{
// creating and Opening the output file
dataOut.open("D:\shoeSale.txt");
// Setting the precision
dataOut << setprecision(2) << fixed << showpoint;
/* reading data from file and apply discount rate
* and add tax rate and write to output file
*/
while (dataIn >> id >> price)
{
price -= price * discount_rate;
price += price * tax_rate;
dataOut << id << " " << price << endl;
price = 0.0;
}
}
// Closing the intput file
dataIn.close();
// Closing the output file.
dataOut.close();
return 0;
}
_____________________
Output file
D:\shoeSale.txt(We can See this file under D Drive.As we specified the path of the output file as D:\shoeSale.txt)
234019 52.89
234490 88.15
347269 35.26
239801 105.78
487241 70.52
982111 123.42
_____________Thank You
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.