In this C++ Practice Assignment, the task is to write a salary program in c++. Then it will generate the salary of an employee as per the sales made by the employee. The requirements for generating sales are:

  • Week 1 – User inputs the totals sales for week 1
  • Week 2 – Call a function to generate a random number between 1000-5000
  • Week 3 – User again manually inputs the total sales for week 3
  • Week 4 – Generate a random number between 1000-5000 to get the total sales for week 4

After the generation of sale numbers for weeks 1 to 4, the total sales are then calculated. It then generates the total salary. The salary program in c++ then displays a pay slip showing the week number, weekly sales, weekly salary, which are formatted in columns, and the total salary for the month.

//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
// 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;
 
}
Share this post