C++ Assignment: Developing an Employee Salary Calculator Program

Verified

Added on  2022/10/09

|8
|1084
|42
Homework Assignment
AI Summary
This assignment presents a C++ program designed to calculate employee salaries based on user input. The program takes inputs for hours worked, rate of pay, and overtime hours, then computes the net pay. The solution utilizes double variables to store input data and integer pointers to manage these values, demonstrating pointer usage. The program addresses potential security vulnerabilities, such as double overflows and mismatching pointer types, and provides solutions like boundary checks and dereferencing pointers to valid types. It also discusses formatted output methods like setw and setprecision, highlighting their advantages over printf. The program's code is provided, along with a screenshot, and a conclusion that emphasizes C++'s security and features. References to external resources on C++ are also included. The assignment showcases the implementation of an employee salary calculator and discusses important programming concepts like pointers and formatted output in C++.
Document Page
Running head: EMPLOYEE SALARY CALCULATOR 1
Assignment
Student
Course
Instructor
Date
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
EMPLOYEE SALARY CALCULATOR 2
Employee Salary Calculator
Introduction
C++ is a general-purpose programming language which is widely used for competitive
programming. It supports imperative, object-oriented, low-level and generic features of
programming. It is available for various platforms like Mac, Windows and Linux. This
assignment revolves around Pointers and input-output in CPP. An Employee Salary calculator is
created in this assignment. ("GeeksforGeeks", 2019) ("Tutorialspoint", 2019)
Discussion
In this assignment, employee salary will be calculated based on the inputs entered by the user.
The program will ask for hours worked, rate of pay and overtime hours from the user and then
calculate the net pay. This input data will also be stored using pointers. Finally, the pointer
values and the variable values will be displayed with the calculated salary.
Analysis
The possible security vulnerabilities that may exist:
Double error:
In this case, double value is used to store the input values for the user. Each double variable has a
fixed size, there is a fixed maximum size associated with it. When an attempt is made to store
anything with a greater value than the value it can store, a double overflow can occur. This can
cause a program to fail or become vulnerable. As a solution, boundary checks must be
implemented while working on any program to check that the value entered is well within the
range.
Document Page
EMPLOYEE SALARY CALCULATOR 3
Mismatching pointer
In this case, a double value is typecasted to an integer pointer. When any inappropriate pointer
type is used, the correct amount of memory is not assigned to the pointer. In this case, double
will need 8 bytes of memory but an int requires only 4 bytes of memory. So, incorrect results
will be printed while dereferencing the integer pointer. As a solution, The pointers must be
dereferenced to only the valid types.
("checkmarx", 2019)
Possible problems that can result in errors when using formatted output.
1. Most of the data formatting methods stick to the stream even after the line is finished. So, the
data on the next lines will also appear formatted.
2. Setw, setprecision and other format specifiers are much safer for formatting output as
compared to printf method. In printf method, if a format string is submitted as an input string, it
can result in a program crash.
("docs.microsoft”, 2019)
Document Page
EMPLOYEE SALARY CALCULATOR 4
Program
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double hoursWorked;
double rateOfPay;
double overtimeHours;
cout << "Standard Hours Worked: ";
cin >> hoursWorked;
cout << "Rate Of Pay: ";
cin >> rateOfPay;
cout << "Overtime Hours: ";
cin >> overtimeHours;
double pay = hoursWorked * rateOfPay;
double overTimePay = overtimeHours * 1.5 * rateOfPay;
int * pHours = new int;
int * prateOfPay = new int;
int * pOverTime = new int;
delete pOverTime;
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
EMPLOYEE SALARY CALCULATOR 5
delete prateOfPay;
delete pHours;
pHours = (int*)& hoursWorked;
prateOfPay = (int*)& rateOfPay;
pOverTime = (int*)& overtimeHours;
cout << "\nStandard Hours Worked: \n";
cout << setw(-30) <<"Variable value:"<<setw(6) << setprecision(2) <<
hoursWorked << setw(20) <<"\t\tPointer value: "<<setw(6) << setprecision(2)
<< *pHours;
cout << "\n\nRate of pay: \n";
cout << setw(-30) <<"Variable value:"<<setw(6) << setprecision(2) <<
rateOfPay << setw(20) <<"\t\tPointer value: "<<setw(6) << setprecision(2) <<
*prateOfPay;
cout << "\n\nOvertime Hours Worked: \n";
cout << setw(-30) <<"Variable value:"<<setw(6) << setprecision(2) <<
overtimeHours << setw(20) <<"\t\tPointer value: "<<setw(6) << setprecision(2)
<< *pOverTime;
cout <<"\n\nStandard Pay: " << fixed<< setprecision(2) << pay;
cout <<"\nOvertime Pay: " << fixed<< setprecision(2) << overTimePay;
cout <<"\nTotal Pay: " << fixed<<setprecision(2) << pay + overTimePay;
cout<<"\n";
}
Document Page
EMPLOYEE SALARY CALCULATOR 6
Screenshot
Document Page
EMPLOYEE SALARY CALCULATOR 7
Conclusion
C++ is an object-oriented language that also supports the libraries of C. In this program, double
values are being used to read data from the user and then Employee salary is calculated based on
the data entered by the user. Integer pointers are being used in this assignment to store these
values and this is calling issues in the application. C++ provides various methods for formatting
the output like setw and setprecision. These methods are more secure than the formatting
specifiers used in C. After completing this assignment, it is concluded that c++ is more secure
and advanced language as compared to C and anyone can utilize its paradigm to build secure and
modern applications.
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
EMPLOYEE SALARY CALCULATOR 8
References
GeeksforGeeks. (2019) C++ Programming Language. Retrieved from
https://www.geeksforgeeks.org/c-plus-plus/
checkmarx. (2019) C++ Security Vulnerabilities and Language Overview. Retrieved from
https://www.checkmarx.com/sast-supported-languages/c-plus-security-vulnerabilities-
and-language-overview/
Tutorialspoint. (2019) C++ Tutorial. Retrieved from https://www.tutorialspoint.com/cplusplus/
docs.microsoft. (2019) String and I/O Formatting. Retrieved from https://docs.microsoft.com/en-
us/cpp/cpp/string-and-i-o-formatting-modern-cpp?view=vs-2019
chevron_up_icon
1 out of 8
circle_padding
hide_on_mobile
zoom_out_icon
[object Object]