Thread: Creating a timer in C++

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    18

    Creating a timer in C++

    Can someone give me a basic sample program on how I would create a timer to keep track of how long it takes the user to perform a certain task. I am making a little program to calculate how many words per minute someone can type. Any help is appreciated thanks. And no it is not for a programming class, just my own personal enjoyment.
    Be yourself for those who mind don't matter, and those who matter don't mind.

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Well I'll give some code, but keep in mind that there are better ways of doing this that aren't as portable.

    Code:
    #include <iostream>
    #include <time.h>
    
    class Timer {
    public:
        Timer() { start = ::time(NULL); }
        virtual ~Timer() { }
    
        unsigned long Lapse() {
            //edit: this is wrong
            //return (::time(NULL)-start)/1000;
            return ::time(NULL)-start;
        }
    private:
        unsigned long start;
    };
    
    int main(void) {
        Timer t;
        std::cout << "Start" << std::endl;
        while(t.Lapse() < 5) {}
        std::cout << "5 seconds have passed" << std::endl;
        return 0;
    }
    Last edited by master5001; 11-27-2002 at 01:40 AM.

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    18
    I got the timer working and this is the program so far. I'm pretty happy with how it's turning out.
    Code:
    #include <iostream.h>
    #include <conio.h>
    #include <string.h>
    #include <windows.h>
    #include <stdio.h>
    
    //function prototypes
    void starttest();
    bool askquestion();
    int length(char l[100]);
    bool invalidletter(char letter, char in);
    
    
    void main()
    {
    	if (askquestion()){ starttest();}
    }
    
    void starttest()
    {
    	char sentence[10][100];
    	int input;
    	int hi = 0;
    	int lengthofsentence;
    	int number= 0;
    	float time = 0.0;
    	int time2 = 0;
    	float wpm = 0.0;
    	DWORD StartTime;
    
    	system("cls");
    	strcpy(sentence[0], "Mary failed english class because she forgot to read her books.");
    	strcpy(sentence[1], "Jose fell down in gym class today and scraped his knees.");
    	strcpy(sentence[2], "Robert needs to get an A on his next test to pass history class.");
    	strcpy(sentence[3], "Jennifer went to Florida to visit her sister who is at college.");
    	strcpy(sentence[4], "In math class today I learned that ten times ten is one hundred.");
    	strcpy(sentence[5], "Don't you just love reading and typing these lame sentences.");
    	strcpy(sentence[6], "Do you try hard enough to be civilized in an uncivilized situation?");
    	strcpy(sentence[7], "After I wake up in the morning I brush my teeth and take a shower.");
    	strcpy(sentence[8], "If you pass the house with the green car in the driveway you went too far.");
    	strcpy(sentence[9], "Make sure you spend at least five hours in front of your computer.");
    	
    	StartTime = GetTickCount();
    
    	for (int cnt = 0; cnt < 10; cnt++)
    	{
    		time = (GetTickCount() - StartTime) / 1000;
    		time2 = time;
    		wpm = (number / time) * 60;
    		printf("\n\nWords: %i\nTime: %i seconds\nWPM: %f\n\n", number, time2, wpm);
    		
    		cout<<sentence[cnt]<<endl;
    		lengthofsentence = length(sentence[cnt]);
    		hi = 0;
    		
    		while (hi < lengthofsentence)
    		{
    			input = _getch();
    			if (input == 8 || invalidletter(sentence[cnt][hi], input)) hi--;
    			++hi;
    			
    			if (sentence[cnt][hi] == input){printf("%c", input); ++hi;}
    			if (sentence[cnt][hi] == ' '){number++;}
    		}
    		number++;
    		system("cls");
    	}
    	
    	time = (GetTickCount() - StartTime) / 1000;
    	time2 = time;
    	wpm = (number / time) * 60;
    	printf("\n\nWords: %i\nTime: %i seconds\nWPM: %f\n\n", number, time2, wpm);
    
    }
    
    bool askquestion()
    {
    	char answer;
    	cout<<"Would you like to see how many words per minute (WPM) you can type?";
    	cin>>answer;
    	if (answer == 'y' || answer == 'Y'){ return true; }
    	if (answer == 'n' || answer == 'N'){ return false; }
    	return false;
    }
    
    int length(char l[100])
    {
    	int num = 0;
    
    	while (l[num] != '\0')
    	{
    		num++;
    	}
    	return num;
    }
    bool invalidletter(char letter, char in)
    {
    	if (letter == in){ return true;}
    
    	return true;
    }
    PROPS to the first person who can answer why I named the program MBclone. Also don't bash my coding style instead I'm open for ideas on how to make my code more efficient and organized. enjoy.
    Last edited by motocross95; 11-27-2002 at 02:53 AM.
    Be yourself for those who mind don't matter, and those who matter don't mind.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >#include <iostream.h>
    You really should be using the current C++ headers instead of the old ones:

    #include <iostream>
    #include <conio.h>
    #include <cstring>
    #include <windows.h>
    #include <cstdio>

    >int length(char l[100]);
    l is usually a bad choice for an identifier, it resembles the number 1 too closely and isn't informative at all.

    >void main()
    This is wrong, and causes your entire program to also be wrong. main returns an int, nothing else.

    >if (answer == 'y' || answer == 'Y'){ return true; }
    if ( toupper ( answer ) == 'Y' ) { return true; } may be a bit simpler.

    -Prelude
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Jan 2002
    Posts
    18
    I understand about the l variable not being informative and closely resembling 1. If main returns an int then why does it let me declare it void and still work? What is the difference if both work? What problems can I encounter from declaring it void instead of int? I also forgot about making the input uppercase before checking thanks for reminding me. Can you explain the whole thing about using the newer ones. Which ones are old? Which ones are the new ones? What advantages do the newers ones have? More efficient? Thanks for all the feedback.
    Be yourself for those who mind don't matter, and those who matter don't mind.

  6. #6
    Registered User
    Join Date
    Jul 2002
    Posts
    66
    If main returns an int then why does it let me declare it void and still work?
    Your compiler is protecting you from yourself. Just because it works doesn't mean it's right.
    What is the difference if both work?
    void main() is wrong, int main() is right. That's the difference. Even if it seems like its working that doesn't mean it really is.
    What problems can I encounter from declaring it void instead of int?
    I learned that void main is undefined behavior, and undefined behavior can do anything at all.
    Can you explain the whole thing about using the newer ones. Which ones are old? Which ones are the new ones? What advantages do the newers ones have? More efficient?
    You mean the header files? The new ones have more stuff that makes programming easier, why use the old ones if you have the new ones?

  7. #7
    Registered User
    Join Date
    Jan 2002
    Posts
    18
    Thanks for all the answers Crimpy. I suppose I'll start using int main since alot of people say it's the correct way. I will also have to look into the new functions in the newer header files. Thanks again. Have any functions in the newers ones that you think are great additions?
    Be yourself for those who mind don't matter, and those who matter don't mind.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  2. SIGALRM and timer
    By nkhambal in forum C Programming
    Replies: 1
    Last Post: 06-30-2008, 12:23 AM
  3. tic tac toe crashes :(
    By stien in forum Game Programming
    Replies: 4
    Last Post: 05-13-2007, 06:25 PM
  4. Need help with a count down timer
    By GUIPenguin in forum C# Programming
    Replies: 0
    Last Post: 07-07-2006, 04:18 PM