write the program in c++ A program is required to read a series of product sales
ID: 3828324 • Letter: W
Question
write the program in c++
A program is required to read a series of product sales records from an input file specified by the user. Each line of the file contains an item name (CANNOT HAVE SPACES), quantity sold, cost of item, and price sold:
Item_name qty_sold cost price
Calculate and display the revenue and profit on each item, as well as the total revenue generated by all sales. Write this information to an output file. Your program should prompt for names for both the input and output file.
Here is a sample file to test with, but your program should work with any file that is in this format.
keyboard 20 54.95 75.00
mouse 10 22.50 30.00
output file with this input file
Product Revenue Profit
keyboard $1500.00 $401.00
mouse $300.00 $75.00
Totals $1800.00 $476.00
Explanation / Answer
#include<iostream>
#include<fstream>
#include<iomanip>
using namespace std;
int main() {
string input, output;
cout<<"Enter the input file: ";
cin >> input;
cout<<"Enter the output file: ";
cin >> output;
ifstream inputFile;
inputFile.open(input.c_str());
ofstream outputFile;
outputFile.open (output.c_str());
string product;
double productCost, priceCost;
int quantity;
double totalPrice = 0, profitTotal = 0;
if (inputFile.is_open()) {
while (!inputFile.eof()) {
inputFile >> product >> quantity>>productCost>>priceCost;
outputFile <<fixed<<setprecision(2)<<product<<" $"<<(quantity*priceCost)<<" $"<<(quantity*priceCost -quantity*productCost )<<endl;
totalPrice = totalPrice+ (quantity*priceCost);
profitTotal =profitTotal + (quantity*priceCost -quantity*productCost );
}
}
outputFile <<"Totals"<<" $"<<totalPrice<<" $"<<profitTotal<<endl;
inputFile.close();
outputFile.close();
return 0;
}
Output:
h-4.2$ g++ -o main *.cpp
sh-4.2$ main
Enter the input file: data.txt
Enter the output file: output.txt
output.txt
keyboard $1500.00 $401.00
mouse $300.00 $75.00
Totals $1800.00 $476.00
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.