954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

timer function and scanf

i would just like to ask

i'm making a timer w/in a game
my problem is with the scan function

the game is like a guessing game. where a jumpled word appears
on screen

the timer is supposed to be displayed like this
--------------------------------------------
output/user screen:
--------------------------------------------
sampleword

time left (10 - 1 sec)
//user prompt for answer
-------------------------------------------

my problem is that. i don't know how to run the timer while the scan function is on
the only thing i have done so far is get

time from point 1 to point 2
but not exactly a countdown timer

thanks,
btw: i'm just a beginner at C

ghost_squad7
Newbie Poster
3 posts since Feb 2008
Reputation Points: 10
Solved Threads: 0
 

Welcome to DaniWeb. Please don't hijack someone's thread to ask your question. Giving you a freebe this time because this is your first post :)

You probably need to create another thread for that timer to run in so that it will execute independently of the scanf() function. Exactly how to do that will depend on the operating system and possibly the compiler you are using.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

ok. thank you

sorry for the newbie mistake.

I see. The game is our project and I'm stuck with the timer.
Our professor hasn't taught us about multi-threading.

All i know is until array and some clock functions in C but since Turbo C (we're using borland C) just runs per line I don't how to perform the timer with scan.

ghost_squad7
Newbie Poster
3 posts since Feb 2008
Reputation Points: 10
Solved Threads: 0
 

The way to do it in Turbo C is to use the functions kbhit() and getch() . You will not be using thescan functions because they will stop the program.

Anyway, you should never use the scanf() to read a string. Here's why . It would be useful to read the entire series on the function so you can head off any problems now before using scanf() drives you crazy.

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

thank you.

just a few more questions

My professor taught us to use getch to see the output.
I'm not familiar with using it as a replacement for scan.

So here is the question.
getch() just gets 1 character right?
How could i use it to get a string?

would it be like example:
---------------------------
getch(1st letter)
getch(2nd letter)
--------------------------
And just loop it?

Thanks too for the info on scanf. I did notice the issue with gets() when you run your prog the 2nd time around, now I know why.

And last question. I'm a little confused with kbhit
kbhit() returns true only when a key is pressed

So the program would continue only if the user presses the correct key?

Sorry for a lot of questions. I haven't used kbhit before

*edit: sorry seems that getch(variable) isn't the correct syntax

ghost_squad7
Newbie Poster
3 posts since Feb 2008
Reputation Points: 10
Solved Threads: 0
 

Use it something like this:

char buffer[255] = {0};
int count = 0;
while( kbhit() )
{
    buffer[count++] = getch();
}


Now put something like that in your code. But that will also capture backspaces so you might need to make the above code a little more intelligent by decrementingcount when the backspace key is hit. Also if the return value of getch() is 0 that means that a special key such as F1, F2 ... F12, was hit and you need to call getch() a second time to get its value.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

thank you.

just a few more questions

My professor taught us to use getch to see the output. I'm not familiar with using it as a replacement for scan.


So? My mom taught me fire was hot. Then after research I found it can cook food. Do some researchSo here is the question.
getch() just gets 1 character right?
How could i use it to get a string?

would it be like example:
---------------------------
getch(1st letter)
getch(2nd letter)
--------------------------
And just loop it?
Yeah, kinda.And last question. I'm a little confused with kbhit
kbhit() returns true only when a key is pressed

So the program would continue only if the user presses the correct key?

Sorry for a lot of questions. I haven't used kbhit before
So? I repeat -- research. Look it up. Google or programming manual for your compiler.

It's going to take you a very long time to program if you have to ask a forum how something works and wait 12-48 hours for an answer. We can best help you clean up a problem or steer you in the right direction, but to answer questions like these is a waste of your time. Look up what you need. Then ask what you don't understand.

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You