need help totaling one city at a time

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
 
T

Thomas Matthews

dan said:
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;

Be consistent on your variable declarations.
One variable per line is industry practice.

Header();
ReadInput(infile, id, unitPrice, purchased, sold, returned, city);

How does one know if ReadInput succeeded or failed?
This one isn't tested.

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;

1. Don't use tabs when posting to Usenet.
The indentation doesn't show up correctly.
Tab distance may vary: 2, 3, 4 and 8 are popular lengths.

2. Looks like this switch statement can be simplified:
cout << "Store: ";
switch (city)
{
case 'B':
cout << "Bloomington";
break;
case 'N':
cout << "Normal";
//...
}
cout << "\n\n";
Print_page_headings(outfile);
Process_this_record(outfile,id,
unitPrice,purchased,sold,returned,invVal);
Print_totals(totalPurchased,totalSold,totalReturned,totalInvVal);
//system("cls");

This can also be further simplified by using a map or an
array-based table to expand the city name given the letter.

}
cout << endl << endl;
}while(infile >> id >> unitPrice >> purchased >> sold >> returned >>
city);

I don't understand why you use ReadInput above and a different method
in the expression of the while loop.

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;

What happens if any of the extractions fail?

}
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";

This could be simplified by:
const char * const header_text[] =
"\t\tCosmic Comics Inventory Report by Store\n"
"\t\t Prepared by Daniel Buenger\n"
"\t\t\t Date: Oct. 30, 2003\n\n";
cout.write(header_text, sizeof(header_text));
}
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



--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,539
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top