Since you asked I took a look at it.
Nice program, love the comments.
I could not get it to compile because of Line 9. I wonder what program you wrote this with? I tried with DevC++ and Codeblocks.
I found a bug, looks like you handle non numbers ok for hours and mins but not for seconds.
c:\temp>test125
Please enter the hour, if there is one: 1
Please enter the minute, if there is one: 2
Please enter the second: a
You have inputted: 01:02:00
|
I made some other changes to fit my style, nothing important, since we are all learning here i'll post so you can see.
I have a similar project, mine is designed to count down and tell you how long a program runs : What I found most interesting is that yours take 2-3 seconds to finish even from zero seconds. To test that I hard coded the time, I also tested this for 5 mins and it added 3 seconds. So it looks like your program takes about 3 seconds to run no matter what. That really isn't a problem, but you might find a way to improve it now that you know about it.
Modification to your code below:
Only major change is I took out your sleep function and used the built in Sleep.
I moved Main() to the end, and relocated a couple of your comments to make it more readable for me.
Then I hard coded in the time so that it would run without user input.
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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
|
#include <iomanip> //library to set up leading 0s.
#include <unistd.h> //library to let me delay
#include <iostream> // for cin, cout and cerr
#include <windows.h> // for Sleep
using namespace std; //
int time (int time1, int time2, int time3);
// unsigned int sleep(unsigned int second); //function to allow me to delay
int time(int second, int minute, int hour) //function that makes the stop watch work.
{
int i, j, k;
i = second;
j = minute;
k = hour;
for (i = second; i >= 0; i--) //seconds is under a for loop
{
Sleep(1000); //delay of stopwatch by one second (duh)
//prints out time. first, it prints out the inputted time, goes through the loop, and prints out the approriate values based on the loops function.
cout << setfill('0') << setw(2) << k << ":" << setfill('0') << setw(2) << j << ":" << setfill('0') << setw(2) << i;
cout << " \n";
//if second is a 0 while the minute isn't, a minute is subtracted and the second is set to 60. this then send the values back to the for loop and goes through it again.
if ((i == 0) && (j >0)){
j--;
i = 60;
}
//if the minute hits 0 while hour is greater than 0, hour is subtracted by 1, minute goes back to 59, and second goes back to 60, and it goes through the loop again.
if (( j == 0) && (k > 0)){
k--;
j = 59;
i = 60;
}
}
Sleep(1000);
cout << "DONE!"; //prints done when the loop is over.
return 1;
}
int main ()
{
// Changed the below to hard code the time to test actual run time.
int hour=0, second=10, minute=0;
/*
cout << "Please enter the hour, if there is one: ";
cin >> hour;
cout << "Please enter the minute, if there is one: ";
cin >> minute;
cout << "Please enter the second: ";
cin >> second;
*/
// Changed the above to hard code the time to test actual run time.
if (hour < 0){
cout << "You have inputted an invalid value for hours. Please restart the program. \n";
}
if (minute > 59 || minute < 0){
cout << "You have inputted an invalid value for minutes. Please restart the program. \n";
}
if (second > 59 || second < 0){
cout << "You have inputted an invalid value for seconds. Please restart the program. \n";
}
else
{
cout << endl << "You have inputted: " << setfill('0') << setw (2) << hour << ":" << setfill('0') << setw(2) << minute << ":" << setfill('0') << setw(2) << second << endl; //puts up the time you have inputted followed with leading 0s.
time(second, minute, hour); //inputted files go to function.
}
return 0;
}
|
My program to test runtime.
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 59 60 61 62 63 64 65 66 67 68 69 70 71
|
#include <fstream> // for ifstream
#include <iostream> // for cin, cout and cerr
#include <string> // for the string datatype
#include <cstdlib> // needed for the exit function
#include <sstream> //
#include <math.h> //
#include <stdio.h> //
#include <time.h> //
#include <conio.h> //
#include <dos.h> //
#include <stdlib.h> //
#include <time.h> //
#include <windows.h> //
#include <sys/stat.h> //
#include <cstring> //
using namespace std; //
int RunMe()
{
cout << "|";
Sleep (125);
cout << "\b";
cout << "/";
Sleep (125);
cout << "\b";
cout << "-";
Sleep (125);
cout << "\b";
cout << "\\";
Sleep (125);
cout << "\b";
cout << "|";
Sleep (125);
cout << "\b";
cout << "/";
Sleep (125);
cout << "\b";
cout << "-";
Sleep (125);
cout << "\b";
cout << "\\";
}
int RunMe2()
{
Sleep (60000);
}
void TimeTest()
{
time_t start,end;
char szInput [256];
double dif;
time (&start); // Start clock
// call program to Test runtime speed below
// RunMe(); // Run for 1 second, show spinning wheel
// RunMe2(); // Run for 60 seconds, verify program reports correct time.
system("filename.exe"); // Run command line program
time (&end); // Stop clock
dif = difftime (end,start);
printf ("Program finished in %.2lf seconds \n", dif );
}
int main ()
{
TimeTest();
return 0;
}
|