473,395 Members | 1,936 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

need help totaling one city at a time

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,totalReturne d,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,totalReturne d,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,totalReturne d,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,totalReturne d,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,totalReturne d,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
Jul 19 '05 #1
1 2137
dan wrote:
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,totalReturne d,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,totalReturne d,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,totalReturne d,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,totalReturne d,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,totalReturne d,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,totalReturne d,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

Jul 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Megha Vishwanath | last post by:
Hi, We have an application where my clients may be at different timezones. I need to validate time across the timezones with javascript. The date object in JavaScript does not allow me to get...
3
by: parksch2 | last post by:
I have been trying and trying to properly sort records through a mySQL query. I have hour, minute and AM/PM values stored separately in a database as varchars. I'm trying to concat those, cast them...
2
by: Frank Rocco | last post by:
Hello, Are there any free or inexpensive date and time picker controls that validate? I need to be able to have the control to just display a date or just display a time for editing. Thanks...
7
by: jack | last post by:
Hello, I need to get the time down the the 0.1 second in a number string. Example 09-06-03 13:45 23.4 Seconds To "0906031345234"
5
by: Sinan Nalkaya | last post by:
hello, i need a function like that, wait 5 seconds: (during wait) do the function but function waits for keyboard input so if you dont enter any it waits forever. i tried time.sleep() but when...
5
by: Jumbo | last post by:
I need a script for my site that shows my servertime minus 7 hours. Anyone who can help?
5
by: Ming Yeung | last post by:
I was wondering if .NET had the equivalent of Frames, Global Objects, and most importantly DataModules like in Delphi?
1
by: Michele1 | last post by:
Hi, Im having problems and need some help!, I'm a Nana, and new to this computor age. I've tried and clicked away and most likely clicked were I should'nt have. Problem # 1 Fatal Execution Error...
4
by: joaotsetsemoita | last post by:
hello everyone. Im trying to time out a loot after a certain time. Probably 5 to 10 minutes. I have the following function Private Sub processFileCreation(ByVal source As Object, ByVal e As...
1
by: jisrar | last post by:
Hi, I have written two algorithms and need to be compare them in terms of how long each take. I have tried clock() before/after each algorithm but my CLOCKS_PER_SEC variable is defined as 1000...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.