could someone atleast point me in the right direction?
i'm new at this and
i've been working on this program and I just hit a brick wall. Its like my brain stopped working or something... :confused:
I have to make a "stop watch" program...have a class defined for stop_watch that stores the time in hours, minutes, and seconds. A constructor for defining the time, and one that will initialize the times to zero. Also, I need member functions to return the Hour, Minute, Second, display time as AM/PM, display time as military time. Another function for calculating the difference in time is needed also.

well this is kind of what i have so far... *although i believe it is completely wrong*

#include<iostream.h>


class StopWatch
{
public:
	StopWatch (int hours, int minutes, int seconds);
	StopWatch ();

	int get_hour(); 
	int get_minute();
	int get_second();
	int AM_PM();
	int military();
	void Time_difference(StopWatch Start_time, StopWatch End_time);
	int hr, min, sec;
	
private:
	int difference;
};

int main()
{
	cout << "This is a Stop Watch Program.\n";
	char chr;
	
	do
	{
		StopWatch start, end;
		cout << "Enter Start Time:\n"
			 << "Hour: ";
		cin >> start.hr;
		cout << "Minute: ";
		cin >> start.min;
		cout << "Second: ";
		cin >> start.sec;

		
		cout << "Enter End Time:\n"
			 << "Hour: ";
		cin >> end.hr;
		cout << "Minute: ";
		cin >> end.min;
		cout << "Second: ";
		cin >> end.sec;
	
		cout << "\nIn Military Time";
		cout << "\n\tStart Time Entered: " << start.hr << ":"<< start.min << ":"<<start.sec<<"\n";
		cout << "\tEnd Time Entered: " << end.hr << ":"<< end.min << ":"<<end.sec<<"\n";
		

		cout << "Do another time calculation? Enter Y or N:";
		cin >> chr;
	}while((chr != 'N') && (chr != 'n'));

	return 0;
}

StopWatch::StopWatch()
{
	hr = 0;
	min = 0;
	sec = 0;
}
StopWatch::StopWatch(int hours, int minutes, int seconds)
{
	hr = hours;
	min = minutes;
	sec = seconds;
	
}
int StopWatch::get_hour()
{
	return hr;
}
int StopWatch::get_minute()
{
	return min;
}
int StopWatch::get_second()
{
	return sec;
}
int StopWatch::AM_PM()
{
			
}
int StopWatch::military()
{
	
}
void Time_difference(StopWatch Start_time, StopWatch End_time)
{
	
}

Recommended Answers

All 11 Replies

You are not that far off the mark functionally with what you have, although I wouldn't really consider this a stopwatch program but rather a difference calculator with mulitple outputs.

For example to get start and stop times I would use

cout << "Press Enter to Start:"
time (&StartTime);
count << "Press Enter to Stop:"
time (&EndTime);

I realize that I'm mixing a "C" library with "C++", but the point being is your algorithm can be greatly simplified, and then using strftime you can establish any sort of display you want then.

I assume this is a school project so please give exact specification for a more qualified answer

Here's my contribution, on the attached file. I hope iit's of some use to you

Well i finally got it to display the times as it should, but i think i made a mistake with my member functions/constructor or something. I can't seem to figure out how to call the values to calculate the difference in times. The professor gave us a hint saying to think of a common denominator among hours, minutes, and seconds *which would be seconds...but i dont know how to use seconds to do it*

the void Time_difference(StopWatch Start_Time, StopWatch End_Time) function should calculate the result and display the time difference as
"Difference = nn hours, nn minutes, nn seconds"

#include<iostream.h>
#include<iomanip.h>

class StopWatch
{
public:
	StopWatch (int hours, int minutes, int seconds);
	StopWatch ();

	void get_hour(); 
	void get_minute();
	void get_second();
	int AM_PM();
	int military();
	void Time_difference(StopWatch Start_time, StopWatch End_time);
	int hr, min, sec;
	

private:
	int difference;
	
};

int main()
{
	cout << "This is a Stop Watch Program.\n";
	char chr;
	StopWatch Start_Time, End_Time;
	
	do
	{	
		cout << "Enter Start Time:\n"
			 << "Hour: ";
		Start_Time.get_hour();
		cout << "Minute: ";
		Start_Time.get_minute();
		cout << "Second: ";
		Start_Time.get_second();

		
		cout << "Enter End Time:\n"
			 << "Hour: ";
		End_Time.get_hour();
		cout << "Minute: ";
		End_Time.get_minute();
		cout << "Second: ";
		End_Time.get_second();
	
		cout << "In Military Time" << "\n\tStart Time Entered: ";
		Start_Time.military();
		cout << "\n\tEnd Time Entered: ";
		End_Time.military(); 
		cout << "\nIn Regular Time" << "\n\tStart Time Entered: ";
		Start_Time.AM_PM();
		cout << "\n\tEnd Time Entered: ";
		End_Time.AM_PM(); 
		cout << "\n";	

		cout << "Do another time calculation? Enter Y or N:";
		cin >> chr;
	}while((chr != 'N') && (chr != 'n'));

	return 0;
}

StopWatch::StopWatch()
{
	hr = 0;
	min = 0;
	sec = 0;
}
StopWatch::StopWatch(int hours, int minutes, int seconds)
{
	hr = hours;
	min = minutes;
	sec = seconds;
	
}
void StopWatch::get_hour()
{
	cin >> hr;
	
}
void StopWatch::get_minute()
{
	cin >> min;
}
void StopWatch::get_second()
{
	cin >> sec;
}
int StopWatch::AM_PM()
{
	if(hr > 12)
	{
		hr = hr - 12;
		cout << setfill('0') << setw(2) << hr << ":" << setfill('0') << setw(2) << min << ":" << setfill('0') << setw(2) << sec << " PM";
	
	}
	else 
	{
		cout << setfill('0') << setw(2) << hr << ":" << setfill('0') << setw(2) << min << ":" << setfill('0') << setw(2) << sec << " AM";
	}
	return 0;
}
int StopWatch::military()
{
	cout << setfill('0') << setw(2) << hr << ":" << setfill('0') << setw(2) << min << ":" << setfill('0') << setw(2) << sec;
	return 0;	
}
void Time_difference(StopWatch Start_time, StopWatch End_time)
{
	
}

Time after 12:00 in seconds = seconds + 60*minutes + 3600*hours. (but 12 o'clock becomes 0). For example, at 2:23:50 PM the time that has elapsed since noon is 50+23*60+2*60*60 seconds.

When taking the difference between 2 times you have to be careful about one being before noon and the other after noon. Military time takes care of that. You also have to worry about the two times being on different days.

well did you notice anything wrong with the way i had the member functions set up or anything like that? and when i try to call the values in the Time_difference function... i always get some sort of error. could you give me an example that might help me?

well did you notice anything wrong with the way i had the member functions set up or anything like that? and when i try to call the values in the Time_difference function... i always get some sort of error. could you give me an example that might help me?

I think I see a couple of problems. For one thing, you can't have time difference as a member function. The reason is that the time difference is not determined by the data in the object. That is, you can't use just the starting time or the stopping time to compute the difference. The time difference function should be just a function of two watches, not a member function.

Another thing I see is that you don't seem to have a way of getting data out of an object. You print out the values of hour, minute and second but you don't return their values.

I hope this was a little helpfull

thx. i'll go back and change the time difference function ...
ive fixed the functions that are supposed to return the hour/min/sec, but now i can't figure out how to get the start/ending times set...i have this constructor "StopWatch (int hours, int minutes, int seconds)" taking in the values... but i cant figure out how to get Start_Time and End_Time to refer to that? not sure if im asking that correctly.

i tried setting the times like...

Start_Time = StopWatch(int hr, int min, int sec);

but it doesn't seem to be working :( omg i feel so dumb right now lol

i tried setting the times like...

Start_Time = StopWatch(int hr, int min, int sec);

but it doesn't seem to be working :( omg i feel so dumb right now lol

Afew postings back I sent as an attachment a file called watch, which is a bare-boned program which would get you started. It doesn't do military time and it doesn't do the time difference. Have you looked at it?

Downloading is sort of tricky. For no reason I can guess it wanted to be opened as a microsoft publisher file! In spit of that if you loaded it into an editor it worked fine. It would be worth your while to look at it.

Afew postings back I sent as an attachment a file called watch, which is a bare-boned program which would get you started. It doesn't do military time and it doesn't do the time difference. Have you looked at it?

Downloading is sort of tricky. For no reason I can guess it wanted to be opened as a microsoft publisher file! In spit of that if you loaded it into an editor it worked fine. It would be worth your while to look at it.

yes i looked at it thanks :) . i have everything working now except figuring out the time difference calculations, which is kind of confusing. I get one of them correct, then other times i enter are wrong :/

how can you use the timer.h in implementing stop watch???

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.