You'll need to add option 5 and 6 yourself. We are throwing around variables, but that is the price of procedural programming. Once you learn the ways of classes, things become so much cleaner. Ask if you have questions:
Code:
//csi 245 C++
//William S. Huskey & Alex Bosman
//A way overboard version of assignment
//header files
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <iomanip>
#include <string>
#include <sstream>
#include <ios>
using std::cout;
using std::cin;
using std::endl;
using std::setw;
//Function Prototypes.
//Needs to pass to compiler options , for Gcc its -DUNIX or -DWIN32. VCC?
void clear_screen();
//User interaction functions.
unsigned prompt_user();
//output functions
void welcome_screen();
void menu_screen();
void add_sales_info();
void request_sales_info(std::string& ID, unsigned& sales);
void update_file(const std::string& ID, const unsigned& sales);
void star_bar();
void stats_graphical_out(int[], const int);
void print_stats(int[], const int, const std::string& StatType);
void reset_polldata(int* polldata, unsigned sz);
void process_data(std::ifstream& ifs, int* polldata, std::string& ID,
unsigned& Sales, double& Commision);
//Globals: we don't like them but this is the price for procedural coding.
//File where sales persons information will be stored.
const std::string FileName = "salesinfo.txt";
//GNU GPL target.
const std::string LicenseFile = "license.txt";
//Bonus provision.
static const double PROVISION = 0.09;
//Salaray groups.
static const int ArraySize = 9;
static const double SalaryRange[ArraySize] = {
200, 300, 400, 500, 600, 700, 800, 900, 1000 };
int main ()
{
int Polldata[ArraySize] = {0};
welcome_screen();
//The menu.
bool running = true;
do
{
menu_screen();
unsigned userChoice = prompt_user();
switch (userChoice)
{
case 1: add_sales_info(); break;
case 2: print_stats(Polldata, ArraySize, "numeric"); break;
case 3: print_stats(Polldata, ArraySize, "graphic"); break;
case 4: /* TODO: sales_dump */ break;
case 5: /* TODO: license */ break;
case 6: std::cout << "Exiting menu..." << std::endl; running = false;
}
} while(running);
return 0;
}
//---------------------------------------------------
void welcome_screen()
{
star_bar();
cout << setw(74) << "* Copyright (C) 2005 by William S. Huskey "
" *" << endl;
cout << setw(74) << "* www@iccaros-linux.org under the GPL "
" *" << endl;
cout << setw(74) << "* "
" *" << endl;
cout << setw(74) << "* Employee Sales Tracking Program "
" *" << endl;
cout << setw(74) << "* Please Choose From an Option Below "
" *" << endl;
cout << setw(74) << "* "
" *" << endl;
cout << setw(74) << "* "
" *" << endl;
cout << setw(74) << "* "
" *" << endl;
star_bar();
std::cout<<endl;
}
//-----------------------------------------------
void menu_screen()
{
star_bar();
cout << setw(74) << "* (1) Enter Sales representative information "
" *" << endl;
cout << setw(74) << "* "
" *" << endl;
cout << setw(74) << "* (2) Show chart of Salary Ranges "
" *" << endl;
cout << setw(74) << "* "
" *" << endl;
cout << setw(74) << "* (3) Show Graph of Salary range "
" *" << endl;
cout << setw(74) << "* "
" *" << endl;
cout << setw(74) << "* (4) List Sales People and Salary "
" *" << endl;
cout << setw(74) << "* "
" *" << endl;
cout << setw(74) << "* (5) Print License "
" *" << endl;
cout << setw(74) << "* "
" *" << endl;
cout << setw(74) << "* (6) Exit Program "
" *" << endl;
star_bar();
}
//---------------------------------------------------
//Clear screen function
void clear_screen()
{
#if defined WIN32
system("cls");
#elif defined UNIX
system("clear");
#endif
}
//---------------------------------------------------
//StarBar is a quick bar output function
void star_bar()
{
for (int x = 0; x <= 74; ++x)
{
cout << "*";
}
cout << endl;
}
//---------------------------------------------------
//This function has four roles:
//1) Prompt the user for input.
//2) Validate the user entered a positive number only.
//3) If it is a pos number: validate its range.
//4) If not positive number or range is not good, then ask again for new input.
//You could modify this function to pass ranges. That's perhaps better.
unsigned prompt_user()
{
std::cout<<"<Choice>: ";
std::string input;
std::getline(std::cin, input);
std::istringstream Iss( input );
unsigned Value;
//We want a strict unsigned integer or the (i)string stream will fail.
while( !( ( Iss >> Value >> std::ws ) && Iss.eof() ) )
{
std::cout<<"ERROR: no unsigned integer found."<<std::endl;
//No good stream: reset the stream state.
Iss.clear();
//Let the user try inputting again.
std::cout<<"<Choice>: ";
std::getline(std::cin, input);
Iss.str(input);
}
//So we have a positive number. But is it in range?
if(Value < 1 || Value > 6)
{
//Not in range.
std::cout<<"ERROR: selection out of range."<<std::endl;
//Recursion.
prompt_user();
}
//We hava a positive value, and it is also in range.
return Value;
}
//----------------------------------------------------
//Let this function merely call helper functions to do specific tasks.
void add_sales_info()
{
std::string ID;
unsigned sales;
request_sales_info(ID, sales);
update_file(ID, sales);
}
//----------------------------------------------------
void request_sales_info(std::string& ID, unsigned& sales)
{
std::cout<<"Add sales representative ID."<<std::endl;
std::cout<<"Example: John Doe = Doej"<<std::endl;
std::cout<<"<ID>: ";
std::cin>>ID;
std::cout<<std::endl;
std::cout<<"Enter number of sales for this representative."<<std::endl;
std::cout<<"<Sales>: ";
//Should add unsigned integer validation here. See function prompt_user().
std::cin>>sales;
std::cin.get();
}
//----------------------------------------------------
void update_file(const std::string& ID, const unsigned& sales)
{
std::ofstream ofs(::FileName.c_str(), std::ios_base::app);
if(!ofs)
{
std::cerr<<"ERROR: could not open/create file."<<std::endl;
std::cerr<<"Exiting Application..."<<std::endl;
//Non-zero exit status is a fail state.
exit(1);
}
ofs<<ID<<" "<<sales<<" "<<sales*PROVISION<<std::endl;
ofs.close();
}
//----------------------------------------------------
void print_stats(int* polldata, const int size, const std::string& StatType)
{
reset_polldata(polldata, size);
std::ifstream ifs(::FileName.c_str());
std::string ID;
unsigned Sales;
double Commision;
//Reads the file and processes the data.
process_data(ifs, polldata, ID, Sales, Commision);
ifs.close();
clear_screen();
std::cout<<"stats ("<<StatType<<")"<<std::endl;
double sr;
//You could (and maybe should) easely split this up in seperate functions.
//----numerical----
if(StatType=="numeric")
{
for (unsigned i = 0; i < size -1; ++i)
{
sr = SalaryRange[i];
std::cout << std::setw(10) << sr << " - " << sr + 99 <<
" " << setw(5) << polldata[i] <<endl;
}
std::cout << std::setw(10) << SalaryRange[8] << " - > " <<
" " << setw(5) << polldata[8] <<endl;
}
//----Graphic----
if(StatType=="graphic")
{
unsigned pdSize;
for (unsigned i = 0; i < size -1; ++i)
{
sr = SalaryRange[i];
std::cout<<sr<<" - "<<sr+99<<"\t";
//Draw the stars.
pdSize = polldata[i];
for (unsigned j = 0; j < pdSize; ++j)
{
std::cout<<"*";
}
std::cout<<std::endl;
}
std::cout<<SalaryRange[8]<<" - > "<<"\t";
for (unsigned j = 0; j < polldata[8]; ++j)
{
std::cout<<"*";
}
}
//Clean up.
std::cout<<endl;
std::cout<<"Please press <enter> to continue...";
std::string buf;
std::getline(std::cin, buf);
clear_screen();
}
//----------------------------------------------------
void reset_polldata(int* polldata, unsigned sz)
{
for(unsigned i = 0; i < sz; ++i)
{
polldata[i]=0;
}
}
//----------------------------------------------------
void process_data(std::ifstream& ifs, int* polldata, std::string& ID,
unsigned& Sales, double& Commision)
{
if(!ifs)
{
std::cerr<<"ERROR: could not open file."<<std::endl;
std::cerr<<"Exiting Application..."<<std::endl;
//You have to test if this is the proper action to do.
return;
}
double WeekSalary(0.0);
unsigned PollIndex;
while(ifs>>ID>>Sales>>Commision)
{
//SalaryRange[0] implies starting salary (without commision).
WeekSalary = SalaryRange[0] + Commision;
PollIndex = static_cast<unsigned>((WeekSalary/100)-2);
if(PollIndex < 9)
{
++polldata[PollIndex];
}
else
{
++polldata[9];
}
}
//Did read of stream stop for unknow reason?
if(!ifs.eof())
{
//Stream could not be read. Perhaps corrupt stream/locked/network problems.
std::cerr<<"ERROR: could not read stream (but file was opened)."<<std::endl;
ifs.close();
//You have to test if this is the proper action to do.
return;
}
}