im newbie in C++
i have a project about
how to get the Total Number of HOurs Worked
From inputed Tine in and Time out
Time in Format: 08:00
Time Out Format:12:00

Input From Monday to Friday DTR
Output must be Display the Total no. of Hours worked: 102:59 --->Example

anyone?Thanks!and GOd Bless!

Code or Ideas to help me Build the code.

Recommended Answers

All 7 Replies

If the data you recieve is in the format XX:YY then your code should be something like:

Get user data (using CIN) for each day in the week
So you have startTime[4], endTime[4] (two arrays)
Determine the dayTime by subtracting endTime from startTime and save it as an array - dayTime[4]

So your code would look something like

for(int i=0;i<4;i++){
cin >> startTime[i];
cin >> endTime[i];

dayTime[i] = endTime[i]-startTime[i];
}

totalTime = 0;
for(int j=0;j<4;j++){
totalTime += dayTime[j];
}

Of course that is rough code as I dont have a platform to do it just now.

#include "stdio.h"
#include "string.h"
#include "iostream.h"
#include "conio.h"
void main(){
clrscr();

for(int i=0;i<4;i++){
	cin >> startTime[i];
	cin >> endTime[i];

	dayTime[i] = endTime[i]-startTime[i];
	}

int totalTime = 0;
for(int j=0;j<4;j++){
	totalTime += dayTime[j];
	}


getche();
}

Theres an Error Undefined Starttime,endtime,daytime,totaltime... do i have to use INterger?how?Thanks for the Reply!

Standard headers are routinely enclosed in angled brackets rather than double quotes.

main() should have return type of int, not void. Using the void return type isn't standard and is one more keystroke than int to type.

You need to declare all variables before they are used. You did so for totalTime and i, but not for any of the arrays. Hopefully that's because you aren't sure what variable type to use for the arrays. The type to use for the time data is up to you. If you assume that start times and end times will be in the same day, then using military time (hours ranging from 00-23) would be a bit easier. If you use AM/PM for input then you could convert to military time without much difficulty. Many implementations of variables representing time will use a struct like this:

struct time
{
int hour;
int min;
};

but if you aren't familiar with struct/class declarations yet, then parallel arrays, one to represent hours and one to represent minutes will work, too. You will need some way to get in the input. You could ask for input for hours and mins separately, or you could enter them as a single string and parse the string to get the individual values. You also need some way to subtract one from the hour if the mins of the stop time are smaller than the min of the start time.

reading the time in is probably the most complicated part. I would use stringstream. Give this a shot:

#include <iostream> // for accessing the console
#include <sstream>
#include <string>
using namespace std;
int main()
{
	//variables for holding the user-inputted time in the format "00:00pm"
	char startTime[7];
	char finishTime[7];

	cout << "Enter start time: ";
	cin >> startTime;
	cout << "Enter finish time: ";
	cin >> finishTime;

	
	int hoursIn;
	int minsIn;
	int hoursOut;
	int minsOut;
	string ampmIn;
	string ampmOut;
	char semicolon;

	//initialize a stringstream with the user's input so we can separate it
	stringstream ssTimeIn(startTime, ios_base::in);

	//separate the string into its components
	ssTimeIn >> hoursIn >> semicolon >> minsIn >> ampmIn;

	//same for finish time
	stringstream ssTimeOut(finishTime, ios_base::in);

	//..
	ssTimeOut >> hoursOut >> semicolon >> minsOut >> ampmOut;

	cout << "Employee started at " << hoursIn << " hours, and " << minsIn << " minutes, " << ampmIn << "." << endl;
	cout << "Employee finished at " << hoursOut << " hours, and " << minsOut << " minutes, " << ampmOut << "." << endl;
	system("pause");
	return 1;
}

it give me an idea but Some Syntax are not applicable to my C++ IDE because its Version 3.0.

i got an another idea From this
but i have to do some modifications...
i need some suggestion regarding this.
that fits to my Project. get the total working hours from 5 consecutive days.
thank you and GOD Bless to all user and Friends who replied to my Post.

#include<stdio.h>
#include<conio.h>

typedef struct
{
    int hh,mm,ss;
}time;

time add(time f,time s);
time sub(time f,time s);
time input(void);

void main()
{
    time f,s,ans;
    int ch;
    clrscr();
    do
    {
        printf("\n <1> enter first operand ");
        printf("\n <2> enter second operand ");
        printf("\n <3> time addition ");
        printf("\n <4> time subtraction ");
        printf("\n <5> Exit \n\n");
        do
        {
            printf("enter your choice ");
            scanf("%d",&ch);
        }while(ch<1 || ch>5);
        switch (ch)
        {
            case 1:
                printf("\n enter the time(hh mm ss) ");
                f=input();
                break;
            case 2:
                printf("\n enter the time(hh mm ss) ");
                s=input();
                break;
            case 3:
                ans=add(f,s);
                printf("\n time addition is %d %d %d",ans.hh,ans.mm,ans.ss);
                break;
            case 4:
                ans=sub(f,s);
                printf("\n time subtraction is %d %d %d",ans.hh,ans.mm,ans.ss);
                break;
        }
    }while(ch!=5);
}

time input(void)
{
    time temp;
    do
    {
        printf("\n enter the hours : ");
        scanf("%d",&temp.hh);
    }
    while(temp.hh<0 || temp.hh>12);
    do
    {
        printf("\n enter the minutes : ");
        scanf("%d",&temp.mm);
    }
    while(temp.mm<0 || temp.mm>=60);
    do
    {
        printf("\n enter the seconds : ");
        scanf("%d",&temp.ss);
    }
    while(temp.ss<0 || temp.ss>=60);
    return temp;
}
time add(time x,time y)
{
    time a;
    if(x.ss+y.ss>60)
        ++x.mm,a.ss=(x.ss+y.ss)%60;
    else
        a.ss=x.ss+y.ss;
    if(x.mm+y.mm>60)
        ++x.hh,a.mm=(x.mm+y.mm)%60;
    else
        a.mm=x.mm+y.mm;
    if(x.hh+y.hh>12)
        a.hh=(x.hh+y.hh)%12;
    else
        a.hh=x.hh+y.hh;
    if(a.hh==0)
        a.hh=12;
    return a;
}

time sub(time x,time y)
{
    time a;
    if(x.ss-y.ss<0)
        --x.mm,a.ss=60+x.ss-y.ss;
    else
        a.ss=x.ss-y.ss;
    if(x.mm-y.mm<0)
        --x.hh,a.mm=60+x.mm-y.mm;
    else
        a.mm=x.mm-y.mm;
    if(x.hh-y.hh<0)
        a.hh=12+x.hh-y.hh;
    else
        a.hh=x.hh-y.hh;
    if(a.hh==0)
        a.hh=12;
    return a;
}

Hmm, a day ago you didn't know that you have to declare a variable before using them. And now that!? Wow, you must be a really fast learner or a really fast at Ctrl + C & Ctrl + V. Very Interesting.

Yup your right!!! i'm very Good in Copy pasting!!!!LOL!coz thats my talent!THANKS!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.