Using Files and Structures You have been asked to create a structure that will h
ID: 3553161 • Letter: U
Question
Using Files and Structures
You have been asked to create a structure that will hold the following information regarding a product inventory item.
Field Contents/Name Type of data in the field
Product an alphanumeric field 10 in length
Retail Price float
Beginning Inventory double
Number Sold This Quarter integer
Remaining Inventory double
Value of Goods Sold double
Cumulative Value of Goods Sold double
As part of the programming team, you have also been asked to generate a report that will read from an input file the following information (excluding the two lines that make up the heading and the blank line below the heading):
Product Retail Price Beginning Inventory Number Sold
This Qtr
Pencils .11 15824 1827
Pens 1.99 756 478
Erasers .59 1911 935
And produce the following inventory report on your computer screen and also on an output file.
Product
Retail
Price
Beginning
Inventory
Number Sold This Quarter
Remaining
Inventory
Value of
Goods Sold
Cumulative
Value of
Goods
Sold
Pencils
.11
15824
1827
13997
200.97
200.97
Pens
1.99
756
478
278
951.22
1152.19
Erasers
.59
1911
935
976
551.65
1703.84
You may want to output the data in fields no more than 11 wide, this will allow you to use 77 columns of an 80 column screen and also fit on 8
Product
Retail
Price
Beginning
Inventory
Number Sold This Quarter
Remaining
Inventory
Value of
Goods Sold
Cumulative
Value of
Goods
Sold
Pencils
.11
15824
1827
13997
200.97
200.97
Pens
1.99
756
478
278
951.22
1152.19
Erasers
.59
1911
935
976
551.65
1703.84
Explanation / Answer
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
using namespace std;
struct inventory
{
string product;
float retail_price;
double beginning_inventory;
int no_sold_this_quarter;
double remainging_inventory;
double value_of_goods_sold;
double cumm_value_of_goods_sold;
};
void iprint(inventory inv)
{
cout<<inv.product<<" "<<" ";
cout<<inv.retail_price<<" "<<" ";
cout<<inv.beginning_inventory<<" "<<" ";
cout<<inv.no_sold_this_quarter<<" "<<" ";
cout<<inv.remainging_inventory<<" "<<" ";
cout<<inv.value_of_goods_sold<<" "<<" ";
cout<<inv.cumm_value_of_goods_sold<<" ";
}
int main(){
string s;
string prod;
float ret_pr;
double beg_inv;
int no_sold;
int cum_sum=0;
getline(cin,s);
cout<<"Product"<<" "<<" ";
cout<<"Retail"<<" "<<" ";
cout<<"Beginning"<<" ";
cout<<"Number Sold"<<" ";
cout<<"Remaining"<<" ";
cout<<"Value of"<<" ";
cout<<"Cummulative"<<" ";
cout<<" "<<" ";
cout<<"Price"<<" "<<" ";
cout<<"Inventory"<<" ";
cout<<"This Quarter"<<" ";
cout<<"Inventory"<<" ";
cout<<"Goods sold"<<" ";
cout<<"Value of goods sold"<<" ";
cout<<" ";
while(true){
getline(cin,s);
std::istringstream iss(s);
if ((iss>>prod >> ret_pr >> beg_inv>>no_sold)){
inventory inv;
inv.product = prod;
inv.retail_price = ret_pr;
inv.beginning_inventory = beg_inv;
inv.no_sold_this_quarter = no_sold;
inv.remainging_inventory = beg_inv - no_sold;
inv.value_of_goods_sold = no_sold*ret_pr;
inv.cumm_value_of_goods_sold = no_sold + cum_sum;
cum_sum += no_sold;
iprint(inv);
break;
}
}
while(getline(cin,s))
{
std::istringstream iss(s);
if (!(iss>>prod >> ret_pr >> beg_inv>>no_sold)) break;
inventory inv;
inv.product = prod;
inv.retail_price = ret_pr;
inv.beginning_inventory = beg_inv;
inv.no_sold_this_quarter = no_sold;
inv.remainging_inventory = beg_inv - no_sold;
inv.value_of_goods_sold = no_sold*ret_pr;
inv.cumm_value_of_goods_sold = no_sold + cum_sum;
cum_sum += no_sold;
iprint(inv);
}
return 0;
}
compile as "g++ <fiilename.cpp>"
and run as "./a.out < inputfilename > outputfilename
A sample input file is
-----------------------
Product Retail Price Beginning Inventory Number Sold
This Qtr
Pencils .11 15824 1827
Pens 1.99 756 478
Erasers .59 1911 935
---------
Good Luck.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.