C++ Practice Assignment #2 - Salary generator program in C++

In this C++ Practice Assignment, the task is to write a Salary generator program in C++ that will generate the salary of an employee with the respect to the sales the employee has obtained. The requirements for generating the sales are

  • Week 1 – The user inputs the totals sales for week 1
  • Week 2 – A function is called to generate a random number between 1000-5000
  • Week 3 – The user again manually inputs the the total sales for week 3
  • Week 4 – A random number between 1000-5000 is generated to get the total sales for week 4

After the sale numbers have been generated for weeks 1 to 4, the total sales is then calculated which then generates the total salary. The program then displays a play slip showing the week number, weekly sales, weekly salary, which are formatted in columns and the total salary for the month.

</pre>
<pre>//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
// Sales salary generator
// Author: Sahil Bora
// Programmed in Visual Studio 2010
// File:main.cpp
//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−

#include <iostream>
#include <ctime>
#include <cstdlib>
#include <iomanip>

using namespace std;
//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
// Function prototypes of the program
//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
double weeklySalary(double& weeklySales);
double randomSales1();
double randomSales2();
void printSignature();
//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
// Constant global variables
//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
int const MAX_SALES = 5000, MIN_SALES = 1000;
double const COMMISSIONS = 0.20;
double normalPay = 200;
double gen_randomsale1, gen_randomsale2; // These variables will be used to calculate the weekly salary

double salesinput1, salesinput2;
double week1Salary, week2Salary, week3Salary, week4Salary;
double totalSalary;
int main()
{

// calls function weekly salary to calculate salary for week 1
cout << "Week 1 Sales \n";
cout << "Enter in the total sales for week 1\n";
cin >> salesinput1;
week1Salary = weeklySalary(salesinput1);
cout << "\n";

// Use the function randomSales to get sales for
// week 2 and then use the function weeklySalary to calculate the salary for week 2
randomSales1();
cout << "Week 2 Sales \n" << gen_randomsale1 << "\n";
week2Salary = weeklySalary(gen_randomsale1);
//Week 3 is user entry salary
cout << "\nWeek 3 Sales\n";
cout << "Enter in the total sales for week 3\n";
cin >> salesinput2;
week3Salary = weeklySalary(salesinput2);
cout << "\n";

// Week 4 is random generator
randomSales2();
cout << "Week 4 Sales \n" << gen_randomsale2;
week4Salary = weeklySalary(gen_randomsale2);
cout << "\n";

// The program calculates monthly salary by adding all 4
// weekly salaries
totalSalary = week1Salary + week2Salary + week3Salary + week4Salary;

// The program displays a pay slip showing a week number 1 − 4
// weekly sales, weekly salary, formatted in columns and a
// total salary for the month
cout << "\nWeek";
cout << setw(20) << "Weekly Sales ";
cout << setw(30) << "Weekly Salary " << setw(20) << "\n\n";
// Outputs the numbers set in formatted columns
cout << "1" << setw(20) << salesinput1 << setw(30) << week1Salary << "\n";
cout << "2" << setw(20) << gen_randomsale1 << setw(30) << week2Salary << "\n";
cout << "3" << setw(20) << salesinput2 << setw(30) << week3Salary << "\n";
cout << "4" << setw(20) << gen_randomsale2 << setw(30) << week4Salary << "\n\n";
// Displays calculated total salary at the end of the program
cout << "Total Salary for the month $ " << totalSalary << "\n\n";
}
//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
// Weekly Salary function implementation
//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
double weeklySalary(double& weeklySales)
{
// Return the weekly salary
return (weeklySales * COMMISSIONS) + normalPay;
}
//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
// Random sales function implementation
//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
double randomSales1()
{
// Seed the time
srand(time(NULL ));

// Generates and returns a random floating point number in the range
// 1000 and 5000 and return the value of the rand function
return gen_randomsale1 = rand()%(MAX_SALES − MIN_SALES + 1 ) + MIN_SALES;

}</pre>
<pre>
Share this post