Problem adding time limit expiration using time.h problem

Hello I am trying to learn how to add time limit expiration using this code but the problem is that is gives me an error saying " error C2065: 'timer' : undeclared identifier" when "timer" is suppose to be part of the time.h library (the pointer i I think). I don't know what's going on with it and I need to hand it in soon. Anybody knows??


#include "stdafx.h"
#include <time.h>
#include <stdio.h>
#include <string.h>


int _tmain(int argc, _TCHAR* argv[])


time_t start,end; // start time and end time

int score=0;
char ans[10];


time (&start); //start timer

printf("Guess the 2 words as many trials within 3minutes");

int minutes = 3 ;

while ( timer < minutes * 60 ) // convert minutes to seconds
{
printf("");
scanf("%s", &ans);
gethchar() ; // for the extra new line because of scanf()

if (strcmp(ans, "hello") == 0)
score++;

if (strcmp(ans, "goodbye") == 0))
score++;


time (&end);
timer = difftime (end,start);

} //end while


puts("Time Up!");

printf("score is %d over 2", score);


return 0;
}

Forget the difference stuff. Just use one time:
 
while ( timer < (time + minutes * 60) ) // convert minutes to second 
OK. But my major problem is that the variable timer still as an undeclared variable showing the same message 'timer' : undeclared identifier"
when is supposed to be part of time.h. Pretty much I am trying to manage time and have a time expiration using a loop. I don't know if i am using the right library or doing the right thing.
Last edited on
There is nothing in time.h (or ctime) that is called timer. If you want a variable with that name you have to create it yourself.
1
2
3
4
for (time_t start = time(0), t = start; t < (start + 60*minutes); t = time(0))
{
    // ...
}
Well I guess that assumption threw me off I should've noticed that but well every mistake is part of the learning process. Thanks for everything. THIS CODE CERTAINLY WORKS.

#include "stdafx.h"
#include <time.h>
#include <stdio.h>
#include <string.h>

int _tmain(int argc, _TCHAR* argv[])
{


time_t start,end; // start time and end time

int score=0;
char ans[10];
double timer=NULL; //TIMER IS INITIALLY 0.

time(&start); //start timer

printf("Guess the 2 words as many trials within 1 minutes");

int minutes = 1 ;

while ( timer < minutes * 60 ) // convert minutes to second
{
printf("");
scanf_s("%s", &ans);
getchar() ; // for the extra new line because of scanf()

if (strcmp(ans, "hello") == 0)
score++;

if (strcmp(ans, "goodbye") == 0)
score++;

time (&end);
timer = difftime (end,start);

} //end while


puts("Time Up!");

printf("score is %d over 2", score);

return 0;
}
Topic archived. No new replies allowed.