My program is almost complete!
But why does it keep totaling the fifth product's total sales calculation as 32?
Please find my source code below for your perusal:
Code:
#include <iostream>
#include <iomanip>
using namespace std;
const int employees = 4;
const int items = 5;
int validateItemNumber(); // enure only numbers within the acceptable range are passed
int validateEmployeeNumber(); // enure only numbers within the acceptable range are passed
void zeroArray( int[][items], int ); // wipe all data from the array
void sale( int[][items], int, int ); // increment sale counter dependent on item sold
int main()
{
int sales[employees][items];
int employeeNumber = 0;
int salesPerEmployee = 0;
int salesPerProduct = 0;
zeroArray( sales, employees);
while ( employeeNumber != -1 )
{
employeeNumber = validateEmployeeNumber();
if ( employeeNumber > -1 )
{
sale( sales, employees, employeeNumber );
}
}
cout << "\nEmployee" << setw( 8 ) << "Item 1" << setw( 8 ) << "Item 2" << setw( 8 ) << "Item 3" << setw( 8 ) << "Item 4" << setw( 8 ) << "Item 5" << setw( 8 ) << "Employee sales" << endl;
for ( int i = 0; i < employees; i++ )
{
cout << setw( 4 ) << i + 1;
for ( int j = 0; j < items; j++ )
{
cout << setw( 8 ) << sales[i][j];
salesPerProduct += sales[i][j];
}
cout << setw( 12 ) << salesPerProduct;
salesPerProduct = 0;
cout << "\n";
}
cout << "Total ";
for ( int i = 0; i < items; i++ )
{
for ( int j = 0; j < employees; j++ )
{
salesPerEmployee += sales[i][j];
}
cout << setw( 8 ) << salesPerEmployee;
salesPerEmployee = 0;
}
} // end main
// enure only numbers within the acceptable range are passed
int validateItemNumber()
{
int a;
cout << "\nWhich item was sold?\nItem no: ";
cin >> a;
while ( a < 1 || a > 5 )
{
cout << "Your input was not recognised!\nWhich item was sold?\nItem no: ";
cin >> a;
}
return a - 1;
} // end function validateItemNumber
// increment sale counter dependent on item sold
void sale( int sales[][items], int employees, int employeeNumber )
{
int itemNumber;
itemNumber = validateItemNumber();
sales[employeeNumber][itemNumber]++;
} // end function sale
// wipe all data from the array
void zeroArray( int sales[][items], int employees )
{
for ( int i = 0; i < employees; i++ )
{
for ( int j = 0; j < items; j++ )
{
sales[i][j] = 0;
}
}
} // end function zeroArray
// enure only numbers within the acceptable range are passed
int validateEmployeeNumber()
{
int empNo;
cout << "\nEnter employee number ( or 0 to show summary ): ";
cin >> empNo;
while ( empNo < 0 || empNo > 4 )
{
cout << "Your input was not recognised!\nEnter employee number ( or 0 to show summary ): ";
cin >> empNo;
}
return empNo - 1;
} // end function validateEmployeeNumber