D
dan
I am wondering if anyone can tell me how to add the totals city by
city clearing the screen after each, then putting the grand total of
all the city's on the final screen.
The input file has id, price, purchased, sold, returned, and then a
letter indicating a city. ex. 200 1.40 30 13 1 B
#include <iostream>
#include <cctype>
#include <iomanip>
#include <fstream>
#include <cstdlib>
using namespace std;
void Header();
void ReadInput(istream& ins, int& id, double& unitPrice, int&
purchased, int& sold,
int& returned, char& city);
void Print_page_headings(ostream& outfile);
void Process_this_record(ostream& outfile, int id, double unitPrice,
int purchased, int sold,
int returned, double invVal);
void Print_totals(int totalPurchased, int totalSold, int
totalReturned, double totalInvVal);
int main()
{
ifstream infile;
ofstream outfile;
infile.open("a:\\comics.txt");
if (infile.fail( )) {
cout << "Input file opening failed.\n";
system("pause");
exit(1);
}
outfile.open("a:\\inventory.txt");
if (outfile.fail( )) {
cout << "Output file opening failed.\n";
system("pause");
exit(1);
}
// format double on the screen to print with 2 decimals
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
// format doubles in the output file to print with 2 decimals
outfile.setf(ios::fixed);
outfile.setf(ios::showpoint);
outfile.precision(2);
int id, purchased, sold, returned;
double unitPrice;
int totalBooks = 0;
double invVal = 0;
char city;
int totalPurchased = 0, totalSold = 0, totalReturned = 0;
double totalInvVal = 0;
Header();
ReadInput(infile, id, unitPrice, purchased, sold, returned, city);
do
{
totalBooks = (purchased - sold + returned);
invVal = (unitPrice * totalBooks);
totalPurchased += purchased;
totalSold += sold;
totalReturned += returned;
totalInvVal += invVal;
switch(city)
{
case 'B':
cout << "Store: Bloomington\n\n";
Print_page_headings(outfile);
Process_this_record(outfile,id,unitPrice,purchased,sold,returned,invVal);
Print_totals(totalPurchased,totalSold,totalReturned,totalInvVal);
//system("cls");
break;
case 'N':
cout << "Store: Normal\n\n";
Print_page_headings(outfile);
Process_this_record(outfile,id,unitPrice,purchased,sold,returned,invVal);
Print_totals(totalPurchased,totalSold,totalReturned,totalInvVal);
//system("cls");
break;
case 'C':
cout << "Store: Champaign\n\n";
Print_page_headings(outfile);
Process_this_record(outfile,id,unitPrice,purchased,sold,returned,invVal);
Print_totals(totalPurchased,totalSold,totalReturned,totalInvVal);
//system("cls");
break;
case 'U':
cout << "Store: Urbana\n\n";
Print_page_headings(outfile);
Process_this_record(outfile,id,unitPrice,purchased,sold,returned,invVal);
Print_totals(totalPurchased,totalSold,totalReturned,totalInvVal);
//system("cls");
break;
case 'P':
cout << "Store: Peoria\n\n";
Print_page_headings(outfile);
Process_this_record(outfile,id,unitPrice,purchased,sold,returned,invVal);
Print_totals(totalPurchased,totalSold,totalReturned,totalInvVal);
//system("cls");
break;
}
cout << endl << endl;
}while(infile >> id >> unitPrice >> purchased >> sold >> returned >>
city);
cout << "Grand Total :\t" << totalPurchased << "\t "<< totalSold
<< "\t "
<< totalReturned << "\t " <<totalInvVal << "\n\n";
infile.close( );
outfile.close( );
cout << endl << endl;
system("pause");
return EXIT_SUCCESS;
}
void ReadInput(istream& ins, int& id, double& unitPrice, int&
purchased,
int& sold, int& returned, char& city)
{
ins >> id >> unitPrice >> purchased >> sold >> returned >> city;
//if(!ins.eof())
//ins >> unitPrice >> purchased >> sold >> returned >> city;
}
void Header()
{
using namespace std;
cout << "\t\tCosmic Comics Inventory Report by Store\n";
cout << "\t\t Prepared by Daniel Buenger\n";
cout << "\t\t\t Date: Oct/30/2003\n\n";
}
void Print_page_headings(ostream& outfile)
{
outfile << "ID\t" << "Price\t" << "Purchase " << "Sold " <<
"Returns "
<< "Inv. Value\n";
cout << "ID\t" << "Price\t" << "Purchase " << "Sold " <<
"Returns "
<< "Inv. Value\n";
}
void Process_this_record(ostream& outfile, int id, double unitPrice,
int purchased, int sold,
int returned, double invVal)
{
outfile << id << "\t" << unitPrice << "\t" << purchased << "\t "
<< sold << "\t "
<< returned << "\t " << invVal;
cout << id << "\t" << unitPrice << "\t" << purchased << "\t " <<
sold << "\t "
<< returned << "\t " << invVal;
cout << endl << endl;
}
void Print_totals(int totalPurchased, int totalSold, int
totalReturned, double totalInvVal)
{
cout << "Totals\t\t" << totalPurchased << "\t "<< totalSold <<
"\t " << totalReturned
<< "\t " <<totalInvVal << "\n";
}
Thanks,
Dan B
city clearing the screen after each, then putting the grand total of
all the city's on the final screen.
The input file has id, price, purchased, sold, returned, and then a
letter indicating a city. ex. 200 1.40 30 13 1 B
#include <iostream>
#include <cctype>
#include <iomanip>
#include <fstream>
#include <cstdlib>
using namespace std;
void Header();
void ReadInput(istream& ins, int& id, double& unitPrice, int&
purchased, int& sold,
int& returned, char& city);
void Print_page_headings(ostream& outfile);
void Process_this_record(ostream& outfile, int id, double unitPrice,
int purchased, int sold,
int returned, double invVal);
void Print_totals(int totalPurchased, int totalSold, int
totalReturned, double totalInvVal);
int main()
{
ifstream infile;
ofstream outfile;
infile.open("a:\\comics.txt");
if (infile.fail( )) {
cout << "Input file opening failed.\n";
system("pause");
exit(1);
}
outfile.open("a:\\inventory.txt");
if (outfile.fail( )) {
cout << "Output file opening failed.\n";
system("pause");
exit(1);
}
// format double on the screen to print with 2 decimals
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
// format doubles in the output file to print with 2 decimals
outfile.setf(ios::fixed);
outfile.setf(ios::showpoint);
outfile.precision(2);
int id, purchased, sold, returned;
double unitPrice;
int totalBooks = 0;
double invVal = 0;
char city;
int totalPurchased = 0, totalSold = 0, totalReturned = 0;
double totalInvVal = 0;
Header();
ReadInput(infile, id, unitPrice, purchased, sold, returned, city);
do
{
totalBooks = (purchased - sold + returned);
invVal = (unitPrice * totalBooks);
totalPurchased += purchased;
totalSold += sold;
totalReturned += returned;
totalInvVal += invVal;
switch(city)
{
case 'B':
cout << "Store: Bloomington\n\n";
Print_page_headings(outfile);
Process_this_record(outfile,id,unitPrice,purchased,sold,returned,invVal);
Print_totals(totalPurchased,totalSold,totalReturned,totalInvVal);
//system("cls");
break;
case 'N':
cout << "Store: Normal\n\n";
Print_page_headings(outfile);
Process_this_record(outfile,id,unitPrice,purchased,sold,returned,invVal);
Print_totals(totalPurchased,totalSold,totalReturned,totalInvVal);
//system("cls");
break;
case 'C':
cout << "Store: Champaign\n\n";
Print_page_headings(outfile);
Process_this_record(outfile,id,unitPrice,purchased,sold,returned,invVal);
Print_totals(totalPurchased,totalSold,totalReturned,totalInvVal);
//system("cls");
break;
case 'U':
cout << "Store: Urbana\n\n";
Print_page_headings(outfile);
Process_this_record(outfile,id,unitPrice,purchased,sold,returned,invVal);
Print_totals(totalPurchased,totalSold,totalReturned,totalInvVal);
//system("cls");
break;
case 'P':
cout << "Store: Peoria\n\n";
Print_page_headings(outfile);
Process_this_record(outfile,id,unitPrice,purchased,sold,returned,invVal);
Print_totals(totalPurchased,totalSold,totalReturned,totalInvVal);
//system("cls");
break;
}
cout << endl << endl;
}while(infile >> id >> unitPrice >> purchased >> sold >> returned >>
city);
cout << "Grand Total :\t" << totalPurchased << "\t "<< totalSold
<< "\t "
<< totalReturned << "\t " <<totalInvVal << "\n\n";
infile.close( );
outfile.close( );
cout << endl << endl;
system("pause");
return EXIT_SUCCESS;
}
void ReadInput(istream& ins, int& id, double& unitPrice, int&
purchased,
int& sold, int& returned, char& city)
{
ins >> id >> unitPrice >> purchased >> sold >> returned >> city;
//if(!ins.eof())
//ins >> unitPrice >> purchased >> sold >> returned >> city;
}
void Header()
{
using namespace std;
cout << "\t\tCosmic Comics Inventory Report by Store\n";
cout << "\t\t Prepared by Daniel Buenger\n";
cout << "\t\t\t Date: Oct/30/2003\n\n";
}
void Print_page_headings(ostream& outfile)
{
outfile << "ID\t" << "Price\t" << "Purchase " << "Sold " <<
"Returns "
<< "Inv. Value\n";
cout << "ID\t" << "Price\t" << "Purchase " << "Sold " <<
"Returns "
<< "Inv. Value\n";
}
void Process_this_record(ostream& outfile, int id, double unitPrice,
int purchased, int sold,
int returned, double invVal)
{
outfile << id << "\t" << unitPrice << "\t" << purchased << "\t "
<< sold << "\t "
<< returned << "\t " << invVal;
cout << id << "\t" << unitPrice << "\t" << purchased << "\t " <<
sold << "\t "
<< returned << "\t " << invVal;
cout << endl << endl;
}
void Print_totals(int totalPurchased, int totalSold, int
totalReturned, double totalInvVal)
{
cout << "Totals\t\t" << totalPurchased << "\t "<< totalSold <<
"\t " << totalReturned
<< "\t " <<totalInvVal << "\n";
}
Thanks,
Dan B