A fruit shop wants to take inventory. Write a C++ program to read the data from
ID: 665817 • Letter: A
Question
A fruit shop wants to take inventory. Write a C++ program to read the data from the .txt file which has the inventory data. Read the data from the file and calculate the value for each items inventory(20*8.99=179.80).Sum all values and output totals to a .txt file
DATA
orange 20 8.99 Apple 10 0.99 Banana 2000 0.82 Apricot 5000 1.21
EXPECTED OUTPUT
----------------------------------------------------------------------------------------------------------
Fruit Co's Inventory Report
----------------------------------------------------------------------------------------------------------
ITEM NUMBER OF UNITS UNIT COST TOTAL VALUE
_______________________________________________________________
Orange 20 8.99 179.80
_______________________________________________________________
Inventory Total 500.34
Explanation / Answer
//fruitshop.cpp ,c++ code read input from inventory.txt and output in total.txt
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
int main()
{
FILE *rptr=fopen("inventory.txt","r"); //reading from inventory.txt
FILE *wptr=fopen("total.txt","wb"); //writing into total.txt
char item[20][20]; //considering maximum string length of item as 20 and 20 as limit to number of items
float total_value;
float total=0;
float number_of_units;
float unit_cost;
fprintf(wptr," Fruit Co's Inventory Report"); //writing into file
fprintf(wptr," ");
fprintf(wptr,"ITEM NUMBER OF UNITS UNIT COST TOTAL VALUE"); //writing into file
fprintf(wptr," ");
int i=0;
while(fscanf(rptr," %s %f %f ",item[i],&number_of_units,&unit_cost)==3) //reading from file
{
total_value=number_of_units*unit_cost; //total_value of each items
total=total+total_value; //summing all value as described in problem
fprintf(wptr," %s %f %f %f ",item[i],number_of_units,unit_cost,total_value);
fprintf(wptr," "); //writing into file
i++;
}
fprintf(wptr," ");
fprintf(wptr," Inventory Total %f ",total);
fprintf(wptr," "); //writing into file
return 0;
}
//inventory.txt input file
orange 20 8.99
Apple 10 0.99
Banana 2000 0.82
Apricot 5000 1.21
//total.txt ,output file
Fruit Co's Inventory Report
ITEM NUMBER OF UNITS UNIT COST TOTAL VALUE
orange 20.000000 8.990000 179.799988
Apple 10.000000 0.990000 9.900000
Banana 2000.000000 0.820000 1640.000000
Apricot 5000.000000 1.210000 6050.000000
Inventory Total 7879.700195
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.