Please help. Im stuck :(

I am new to C++ and working on an assignment. Below is what I have come up with so far and seems to be working. I am at the last part and have spent many hours trying to figure this out without any luck. I need to add the total hours and minutes at the bottom of my time sheet. The minutes need to add up properly with the hours. This meaning I can not have over 59min in the minutes column. I am not sure how to tell the program to calculate the data from the file. Any help would be greatly appreciated. This is driving me bonkers!! :) Here are the instructions and .txt file I am working with. I thank you greatly for any help.
www.tier1studio.com/dowloads/TimesheetSpring12.pdf
www.tier1studio.com/dowloads/Timesheet.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// Program name: Timesheet.cpp
// Date: 4/10/2012

#include <iostream>
#include <iomanip>
#include <fstream>
#include <ctime>
using namespace std;

int main ()
{
	
	char name[25];
	char date[10];
	char currentDate[9];
	float hours=0;
	float minutes=0;
	float grossTime=0;
	float totalHours=0;
	float totalMin=0;
		ifstream inFile;

	cout << fixed << showpoint << setprecision(2);
	inFile.open("timesheet.txt");
	if(!inFile)

	{
		cout << "\a ***unable to open file*** \n";
		system ("pause");
		exit(1);
	}
	_strdate(currentDate);
	inFile.getline(name, 25);
	cout << name <<setw(32) << "Timesheet Report"<< setw(23) << currentDate << endl << endl << endl;
	cout << "DATE" << setw(36)  << "HOURS" << setw(31) << "MINUTES\n\n";

	inFile >> date >> hours >> minutes;
	while(inFile)
	{
		
		cout << left << setw(35) << date <<setw(25) << hours << right << setw(7) << minutes << endl;
		inFile >> date >> hours >> minutes;
		
	}
	inFile.close();
	




	system("pause");
		return 0;
}
Last edited on
I just cant seem to get it working correctly. Should I use temp variables?
Hmmm...this was difficult. It took me a good hour and a half:P But here is is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <iostream>
#include <iomanip>
#include <fstream>
#include <ctime>
#include <math.h>        //include for the rounding function
using namespace std;

int main ()
{
	
	char name[25];
	char date[10];
	char currentDate[9];
	float hours=0;
	float minutes=0;
	float grossTime=0;
	float totalHours=0;
	float totalMin=0;
	float overflowhours;  //use this if minutes > 59
		ifstream inFile;

	cout << fixed << showpoint << setprecision(2);
	inFile.open("timesheet.txt");
	if(!inFile)

	{
		cout << "\a ***unable to open file*** \n";
		system ("pause");
		exit(1);
	}
	_strdate(currentDate);
	inFile.getline(name, 25);
	cout << name <<setw(32) << "Timesheet Report"<< setw(23) << currentDate << endl << endl << endl;
	cout << "DATE" << setw(36)  << "HOURS" << setw(31) << "MINUTES\n\n";

	inFile >> date >> hours >> minutes;
	while(inFile)
	{
		if (minutes>59)  //detects if there are more than 59 minutes
		{
                       hours = minutes / 60;  //convert all minutes to hours
                       overflowhours = floor(hours);  //round hours down
                       minutes = (hours - overflowhours) * 60;   //get the difference that was lost with rounding and convert it to minutes
                       hours = overflowhours; //set hours to a whole number
                       }
		cout << left << setw(35) << date <<setw(25) << hours << right << setw(7) << minutes << endl;
		inFile >> date >> hours >> minutes;
		
	}
	inFile.close();
	




	system("pause");
		return 0;
}


I'm pretty sure this works, although it is probably not the most efficient; I'm only a beginner, after all:P

Thank you for your help. How do I add a total to the bottom of the list using the same method? So it looks something like this:

Total Hours: XX Total Minutes: XX
Kinison, I happened to see this...... looks like we both are in the same class. Here is what I did to get the hours/minutes portion:-

while(inFile)
{
inFile >> hours >> minutes;
cout << left << setw(34) << dateWorked
<< left << setprecision(1) << setw(22) << hours
<< right << setprecision(1) << setw(10) << minutes << endl;

totalHours += hours;
totalMinutes += minutes;

actualMinutes = totalMinutes/60;
remainingMinutes = totalMinutes % 60;
remainingHours = totalHours + actualMinutes;

inFile >> dateWorked;

}
inFile.close();
cout << "\nTOTAL TIME: " << setw(23) << remainingHours << setw(30) << remainingMinutes << endl << endl;
cout << "\nThe file EmployeeTimeSheet.txt has been created." << endl;
cout << "End of program." << endl << endl;

system("pause");
return 0;
}

One way you could do that is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
inFile >> date >> hours >> minutes;
	while(inFile)
	{
		if (minutes>59)  //detects if there are more than 59 minutes
		{
                       hours = minutes / 60;  //convert all minutes to hours
                       overflowhours = floor(hours);  //round hours down
                       minutes = (hours - overflowhours) * 60;   //get the difference that was lost with rounding and convert it to minutes
                       hours = overflowhours; //set hours to a whole number
                       }
		cout << left << setw(35) << date <<setw(25) << hours << right << setw(7) << minutes << endl;
                totalmin = totalmin + minutes;  //add minutes to a total each time this statement runs
                totalhours = totalhours + hours;   //add hours to an overall total each time this statement runs
		inFile >> date >> hours >> minutes;
		
	}
	inFile.close();
	

The part in bold is what you would add, of course you would declare the variables and set them = 0 at the top. And then you would output the final total after your while statement is finished.
Thank you very much everyone. I appreciate your help! You guys rock! :)
Dhiyakutty,

I found that moving these calculations outside of the loop worked best and move inFile >> date >> hours >> minutes to the end of the loop.
1
2
3
actualMinutes = totalMinutes/60;
remainingMinutes = totalMinutes % 60;
remainingHours = totalHours + actualMinutes;


Thanks again for your help everyone.
Topic archived. No new replies allowed.