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:

You could also use select() for a more portable and accurate solution.

04-14-2003

athos

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

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

04-14-2003

axon

ypu probably forgot to close a bracket somwhere before this function definition....post all the code and we'll take a look,

axon

04-14-2003

athos

Nevermind, im a dumbass!! i wont even tell anyone what i did for fear of ostricization from this forum. thanks everbody:o

04-14-2003

axon

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

04-14-2003

DirX

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.

04-14-2003

athos

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!). :)

04-15-2003

axon

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

04-15-2003

athos

Your code works but i dont think i can use cin because im using kbhit() and cin will stop my loop.

04-15-2003

athos

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.