-
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:confused:
-
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)
-
You could also use select() for a more portable and accurate solution.
-
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
-
ypu probably forgot to close a bracket somwhere before this function definition....post all the code and we'll take a look,
axon
-
Nevermind, im a dumbass!! i wont even tell anyone what i did for fear of ostricization from this forum. thanks everbody:o
-
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
-
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.
-
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!). :)
-
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
-
Your code works but i dont think i can use cin because im using kbhit() and cin will stop my loop.
-
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!
-
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..
-
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:D :D :D :D