Thread: Real time issues

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    13

    Talking Real time issues

    I have to create a loop that must end upon keyboard input. If no input is receieved in say 15 seconds the loop must end. After the keyboard input, the system must compare the input to a saved character or string of characters. I am using VC++ 6.0. Any assistance would be GREATLY apprecaited. thank you

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Code:
    #include <iostream>
    #include <time.h>
    #include <conio.h>
    
    using namespace std;
    
    int main()
    {
    	//Data
    	int StartTime = time(NULL);
    	int CurrentTime = 0;
    	int LastTime = 0;
    
    	//Loop condition
    	while(!kbhit() && ((15 + StartTime - CurrentTime) > 0))
    	{
    		//Update the current time
    		CurrentTime = time(NULL);
    
    		//Print the remaining time if it has been updated
    		if(LastTime != CurrentTime)
    		{
    			cout << "There is now " << (15 + StartTime - CurrentTime) << " seconds left!" << endl;
    			LastTime = CurrentTime;
    		}
    	}
    
    	return 0;
    }
    (WARNING: Uses conio.h)
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    1
    You could also use select() for a more portable and accurate solution.

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    13

    Question unknown error.

    Thanks for the kbhit() it works great! but i now have an error that i cannot fix. The error is "local function definitions are illegal". Its pointing to the beginning of a function that i think i know is properly called by reference and set up.
    void fruitveg(int&, int&, int&);//prototype

    if(game==2)
    fruitveg(score1, score2, score3);//called here by reference

    void fruitveg(int& score1, int& score2, int& score3)//start of function
    {<-----here is the line where the error is.

    everyone has been really helpful thanks again,
    athos

  5. #5
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    ypu probably forgot to close a bracket somwhere before this function definition....post all the code and we'll take a look,

    axon

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    13
    Nevermind, im a dumbass!! i wont even tell anyone what i did for fear of ostricization from this forum. thanks everbody

  7. #7
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    don't worry, everyone makes mistakes...and most often it is very simple ones that tend to get overlooked in all the code...I'm very new to c++, and I totally know how you feel!!!

    but so far, people on this board have been EXTREMELY helpful, don't give up on us,

    axon

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  8. #8
    Registered User
    Join Date
    Aug 2002
    Posts
    87
    I have been using c++ for quite some time now and it is always the little things that give me the problems that i can;t solve . the hard stuf is usually easier to fix because you know where you went wrong. The opening and closing of brackets is one of the things that take m the longest to debug even with good formatting.

  9. #9
    Registered User
    Join Date
    Apr 2003
    Posts
    13

    Getstr

    Thanks alot guys I have the utmost faith in this website and the help provided. It has all worked! One more question though. I need to compare a two strings of characters, one will be a saved string and the other will be a string receieved from keyboard input that ends the kbhit(). I was trying to use getstr and getline nieither worked, i dont know if i have the correct header so please include the include (ha!).

  10. #10
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    if you are using c-strings, in other words character arrays finished of with a NULL you could use strcmp(); it is in the same include as cstring:
    here is an example:
    Code:
    char theWords[ MaxNumberOfWords][ Size];    // Array to store words from input line
    char wordToLookup[ Size];                   // word to lookup
    bool wordWasFound = false;                  // flag to track whether or not word is found
    char c;                                     // stores return character after input
    // Prompt for and get the word to be looked up
       cout << "Enter the word to lookup: ";
       cin >> wordToLookup;
       cin.get( c);      // clear the "enter" character from the input buffer.
    
       // See if the word is found
       // Note that if we didn't know exactly the number of words in the array, 
       // (which could be less than the total number that *could* be stored) 
       // then we would need to initialize each array element to NULL so that
       // string compare wouldn't choke.
       for (int i=0; i<wordRow; i++)  {
          // See if this word matches
          if ( strcmp( wordToLookup, theWords[ i]) == 0 ){
             wordWasFound = true;
             break;      // quit looking
          }
       }
    notice that if the two strings are the same a result of 0 will be obtained,

    axon

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  11. #11
    Registered User
    Join Date
    Apr 2003
    Posts
    13
    Your code works but i dont think i can use cin because im using kbhit() and cin will stop my loop.

  12. #12
    Registered User
    Join Date
    Apr 2003
    Posts
    13
    The only problem i have with my code now(hopefully) is that i need to store a string of characters from kbhit() instead of only one character. answer= getch() works great for one character. I dont know how to store what is placed in the buffer into a string as soon as I press something on the keyboard.

    Thanks!

  13. #13
    Burning in Hell! Luigi's Avatar
    Join Date
    Nov 2002
    Posts
    117
    to copy/store a cstring use strcpy() in <string.h>..

    but u should use c++ string contained in <string> header

    Code:
    string temp_string, my_string = "bob";
    cin >> temp_string;
    if (temp_string == my_string) my_string = temp_string;
    I know this code doesnt do much but it shows how easier it is to copy and compare string from cstring..

    hope it helped..
    Luigi


    // I use Xcode 1.1 && CodeWarrior 8.3
    // When on Mac Os X 10.3.2

    // I use Microsoft Visual C++ 6.0
    // When on windows XP

  14. #14
    Registered User
    Join Date
    Apr 2003
    Posts
    13

    Talking

    Thank you all so much for your help!! I finally got everything working you guys are great. I will keep looking around on here so maybe i can return the favor for someone.

    Thanks again

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 07-05-2010, 10:43 AM
  2. Clearing the console
    By beanroaster in forum C++ Programming
    Replies: 53
    Last Post: 09-14-2005, 07:46 PM
  3. Killing someones grandparents
    By nickname_changed in forum A Brief History of Cprogramming.com
    Replies: 37
    Last Post: 09-07-2003, 07:56 AM
  4. Is this really true or it's just science fiction?
    By Nutshell in forum A Brief History of Cprogramming.com
    Replies: 145
    Last Post: 04-09-2002, 06:17 PM
  5. Programming Puns
    By kermi3 in forum A Brief History of Cprogramming.com
    Replies: 44
    Last Post: 03-23-2002, 04:38 PM