Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

compute the earnings per shift of security guard #104

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
compute the earnings per shift of security guard
Output1--

Input starting hours: 7
Input starting minutes: 0
Input ending hours: 2
Input ending minutes: 0

Total charge: 115
Total hours of work: 7

Output 2--

Input starting hours: 9
Input starting minutes: 0
Input ending hours: 5
Input ending minutes: 30

Total charge: 155
Total hours of work: 8.5
  • Loading branch information
Akshat111999 committed Oct 10, 2021
commit f8a5a6858eb27ef8cd2c64d2d8456a1403dbdc0c
80 changes: 80 additions & 0 deletions CPP/ShiftEarning.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
//using c++ and please explain to me the calculation.
//Write a program that computes the earnings per shift for a security guard. A security guard charges $15 per hour before midnight and $20 after midnight. The program reads the starting time in hours and minutes and the ending time in hours and minutes. All times are between 6:00 pm, and 5:59 am, using a 12-hour clock. For example, you should consider hour 8 as 8 pm and hour 2 as 2am.
//The program should check the validity of the inputs as follows.
//1) Hours are from 0-11 (0 for midnight).
//2) Minutes are from 0-59.
//3) The start time must be before midnight.
//4) The end time must be after the start time.
//The program should display specific warnings regarding the above when the user submits invalid input; the program should prompt the user to re-enter the times again. The program should output the total hours worked and the total earnings per shift.
//Example: >7h 0m to 2h 0m You have worked 7 hours in this shift, earning $115.

#include <iostream> // standard input and output library
using namespace std;
//main function
int main(){
int hour, mints, endhour, endmints; //taking user input from user about shift time
cout<<"Input starting hours: ";
cin>> hour;
cout<<"Input starting minutes: ";
cin>> mints;

cout<<"\nInput ending hours: ";
cin>> endhour;
cout<<"Input ending minutes: ";
cin>> endmints;

int hourpm, houram, mintspm, mintsam;
//hours
//calculating number of hours of shift before midnight
if(hour>=6 && hour<12){
if(mints>0){
hourpm = 12 - (hour+1);
}
else{
hourpm = 12-hour;
}
}
else{
cout<<"Input correct time";
}
//calculating number of hours of shift after midnight
if(endhour>=0 && endhour<6){
houram = endhour-0;
}
else{
cout<<"Input correct time";
}

//minutes
//calculating number of minutes of shift before midnight
if(mints>0 && mints<60){
mintspm= 60-mints;
}
else{
mintspm=mints;
}
//calculating number of minutes of shift after midnight
if(endmints>0 && endmints<60){
mintsam= endmints-0;
}

// converting minutes into hours
float mintoHourAM, mintoHourPM;
mintoHourPM = (float)mintspm/60;
mintoHourAM = (float)mintsam/60;

// calculating shift charge before midnight and after midnight separately
float chargeAM, chargePM, totalCharge;
chargePM = ((float)hourpm + mintoHourPM) * 15;
chargeAM = ((float)houram + mintoHourAM) * 20;

// calculating total shift charge
totalCharge = chargePM + chargeAM;
cout<<"\nTotal charge: "<<totalCharge;

// calculating total shift hour
float totalhours;
totalhours = hourpm + mintoHourPM + houram + mintoHourAM;
cout<<"\nTotal hours of work: "<<totalhours;

}