4 Replies - 1278 Views - Last Post: 04 October 2010 - 01:02 PM Rate Topic: -----

#1 roohan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 13-June 10

very basic C questions

Posted 04 October 2010 - 11:45 AM

I just started with CS201 in college and it seems like were going back to C from C++. So i had just a few questions about the C library. So for my assignment, I am suppose to make a rhythm calculator, which calculates if the user can hit the return key after exactly 1 second for 10 seconds. So my 1st question was that how do i take in or scanf the return key ? like i know how to take in integers or chars but how do i just take in a key? and secondly we have to use the time.h library and i went through the manual and i need to use this part of the struct

This is inside the time.h file.....


struct tm {
    int tm_sec;     /* seconds after the minute [0,60] */
........



How do i use that in my program after including the time.h file . and lastly , this is probably the most noob question, are there classes in C or just structs? Thanks :)

Is This A Good Question/Topic? 0
  • +

Replies To: very basic C questions

#2 aaa111  Icon User is offline

  • D.I.C Regular

Reputation: 88
  • View blog
  • Posts: 284
  • Joined: 21-February 07

Re: very basic C questions

Posted 04 October 2010 - 11:54 AM

1)Well for the return key i would suggest to use getchar() function and use it to check for the return key by if statement.

2)After including the header you have to do this:
tm t1;


3)C does not support classes.

This post has been edited by aaa111: 04 October 2010 - 12:00 PM

Was This Post Helpful? 0
  • +
  • -

#3 roohan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 13-June 10

Re: very basic C questions

Posted 04 October 2010 - 11:58 AM

View Postaaa111, on 04 October 2010 - 10:54 AM, said:

1)Well for the return key i would suggest to use getchar() function.
2)After including the header you have to do this:
tm t1;


so how do i use getchar() to get the current system time and store it in an int ?
Was This Post Helpful? 0
  • +
  • -

#4 eker676  Icon User is offline

  • Software Engineer
  • member icon

Reputation: 378
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: very basic C questions

Posted 04 October 2010 - 12:07 PM

Look here:
http://cplusplus.com...ctime/difftime/

/* difftime example */
#include <stdio.h>
#include <time.h>

int main ()
{
  time_t start,end;
  char szInput [256];
  double dif;

  time (&start);
  printf ("Please, enter your name: ");
  gets (szInput);
  time (&end);
  dif = difftime (end,start);
  printf ("Hi %s.\n", szInput);
  printf ("It took you %.2lf seconds to type your name.\n", dif );
 
  return 0;
}


Now incorporate that code into yours. Sleep for 1 second, then calculate the time it takes for the user to press enter. Then using something similar to GetAsyncKeyState() to see if enter is held down for 10 seconds.

This post has been edited by eker676: 04 October 2010 - 12:09 PM

Was This Post Helpful? 0
  • +
  • -

#5 aaa111  Icon User is offline

  • D.I.C Regular

Reputation: 88
  • View blog
  • Posts: 284
  • Joined: 21-February 07

Re: very basic C questions

Posted 04 October 2010 - 01:02 PM

View Postroohan, on 04 October 2010 - 10:58 AM, said:

so how do i use getchar() to get the current system time and store it in an int ?

I don't see why you need current system to determine the 10 sec elapsed time.You could use two clock_t variable(one for the start,another for end) to determine the elapsed time using the clock function and divide the difference by the CLK_TCK macro.
Something like this:
clock_t beg,end;
double e_time;

beg=clock();
....
//do something
....
end=clock();

e_time=(end-beg)/CLK_TCK;


This post has been edited by aaa111: 04 October 2010 - 01:09 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1