|
-
Oct 5th, 2009, 10:56 PM
#1
Thread Starter
New Member
[RESOLVED] Display total hours worked as weeks, days, hours worked.
I need to write the code for a program that will allow a user to enter their name and the total hours they worked in a given month and press enter to return the amount of weeks, days, hours worked (using an 8 hour day and 40 hour week). I need to know how to do the calculation of the hours to weeks, days, hours...using a modulus arithmetic formula. For example, if user enters 49 hours and presses enter, it would return 1 week, 1 day , 1 hour.
Any help would be greatly appreciated.
-
Oct 6th, 2009, 02:23 AM
#2
Re: Display total hours worked as weeks, days, hours worked.
Just a guess...
Code:
Private Sub Command1_Click()
Dim Weeks As Double, Days As Double, Hours As Double
Const aWeek = 40 ' 1 week = 40 hours
Const aDay = 8 ' 1 day = 8 hours
' set hours input for testing...
Text1.Text = 49
' get user hours input
Hours = CDbl(Text1.Text)
' how many 40 hour weeks?
Do Until Hours < aWeek
Weeks = Weeks + 1
Hours = Hours - aWeek
Loop
' how many 8 hour days?
Do Until Hours < aDay
Days = Days + 1
Hours = Hours - aDay
Loop
' display result as weeks, days, hours.
Print Weeks & " Week(s), " & Days & " Day(s), " & Hours & " Hour(s)."
End Sub
-
Oct 6th, 2009, 02:25 AM
#3
Re: Display total hours worked as weeks, days, hours worked.
vb Code:
hrsw = 49 wks = hrsw \ 40 hrsleft = hrsw mod 40 dys = hrsleft \ 8 hrs = hrsleft mod 8 msgbox "weeks = " & wks & ": days = " & dys & ": hours = " & hrs
too slow
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Oct 6th, 2009, 02:33 AM
#4
Re: Display total hours worked as weeks, days, hours worked.
What happens when the employee works overtime, more then 8 hours in a day? 
First you need the number of working weekdays in the given month. Then take that number and multiply by 8. This should be the maximum number of hours entered for validation if no overtime is the case.
Then the basics, xxx/40 gives the weeks. Save the remander and process that for number of left over whole days which is xx/8. Save that remainder for processing as left over hours which is simply the result.
Code:
Dim iTotalHoursWorked As Single
Dim iNumberOfWeeksWorked As Single
Dim iNumberOfDaysWorked As Single
Dim iNumberOfHoursWorked As Single
iTotalHoursWorked = 49
iNumberOfWeeksWorked = Fix(iTotalHoursWorked / 40)
iNumberOfDaysWorked = Fix((iTotalHoursWorked Mod 40) / 8)
iNumberOfHoursWorked = (iTotalHoursWorked Mod 40) Mod 8
MsgBox "Total Time Worked: " & iNumberOfWeeksWorked & " Week(s), " & iNumberOfDaysWorked & " Day(s), " & iNumberOfHoursWorked & " Hour(s)"
Edit: Guess I shouldnt have typed so much or got up to get a midnight snack
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Oct 6th, 2009, 12:06 PM
#5
Thread Starter
New Member
Re: Display total hours worked as weeks, days, hours worked.
Thank you, thank you, thank you. I will come back and post what I used and if it worked. I really appreciate all of the replies... and I'm glad that you typed so much and got up for a midnight snack...lol.
-
Oct 7th, 2009, 04:02 PM
#6
Thread Starter
New Member
Re: Display total hours worked as weeks, days, hours worked.
Thanks for all of the help everyone. I think my problem was trying to over-complicate an easy problem. Here is what I finally did...
//Ch4AppE07 calculates and displays the employees name and weeks, hours, and days worked.
//Created/revised by Jana Bland on October 7, 2009
#include <iostream>
#include <string>
using std::cin;
using std::cout;
using std::endl;
using std::string;
int main()
{
//declare variables
string name = "";
int hoursWorked = 0;
int remainHours = 0;
int weeks = 0;
int days = 0;
int hours = 0;
//enter input functions
cout << "Enter the name: ";
getline(cin, name);
cout << "Enter the hours worked: ";
cin >> hoursWorked;
//calculate the weeks, days, and hours
weeks = hoursWorked/40;
remainHours = hoursWorked % 40;
days = remainHours/8;
hours = remainHours % 8;
//display the output items
cout << "Name: " << name << endl;
cout << "Weeks: " << weeks << endl;
cout << "Days: " << days << endl;
cout << "Hours: " << hours << endl;
return 0;
} //end of main function
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|