Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

2 Part Question: elapsed time  & writing data to a file in VC++

Posted on 1998-10-07
3
Medium Priority
?
228 Views
Last Modified: 2010-04-01
Hello, I am quite a beginner in the C++ arena, so bare with me.

1)  I am trying to create a function that will collect a response time between hitting RETURN and typing the very first character of a sentence. (better explained below)
Example:

         Hit RETURN or ENTER to begin    (user hits the key)
         Begin typing your sentence      (printf statement)
(sentence 1) This is a test sentence <CR> (I want the timer
                                           to collect the
                                           the response time
                                           as soon as <CR>
                                           is hit.
(sentence 2) this is second sentence.     (The timer stops
                                           as soon as the
                                           first character
                                          of the next sente
                                          is typed.  In this
                                          case, 't' is
                                          typed in the word
                                          'this')
                                                                                   

Here is the code I have, but it's not quite working right.  It is collecting the time it takes for the entire sentence to be typed instead of the interval between hitting return and typing the first character of the next sentence.  I want
the user to eventually type in 5 sentences then go back to the main part of the program. and write this data (sentence and response time--see question 2)



void Do_Experiment(USHORT which)  <<--(using switch() to get
{                                     here in main program)
     if(which == 2)
     {
      clock_t begin, end;
      clock_t *t=NULL;
      double duration;
      char buffer[80];
      char character = '0';
      int i, j=0;
   printf("\nBegin typing your sentence. \n");
   for(i =0; i <=5; i++)
{
   while(scanf("\0", buffer));
   end = NULL;
   begin = clock();
   gets(buffer);
   if((character=getchar()) !='\n')
   {
   printf("character is... %c\n",character);
   buffer[j]=character;
   j++;
   }
   buffer[j-1]='\0';
   printf("buffer is...%s\n",buffer);
   end = clock();      
   duration = (double)(end-begin) / (double)CLOCKS_PER_SEC;
   printf("\nThe response time was %1.4f\n",duration);
 }  
}

2)  Next what I want to do is take the informatin that the user enters (the typed sentence) and the response time taken (see above) and write it to a file that can be viewed later for analysis.    

If you need/want to see the entire program, please let me know via email:  haubnerk@trac.wsmr.army.mil
I would like to thank you in advance for anyone that can help me out.
0
Comment
Question by:haubnerk
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 

Author Comment

by:haubnerk
ID: 1174621
Thank you for any insight to this small snafu I have created for
myself.
0
 
LVL 2

Accepted Solution

by:
shchuka earned 480 total points
ID: 1174622
1. I think, one problem is in gets(buffer) call.  This one will return only after the CR is pressed and you need to catch the first letter being typed in.  I think the way out is to use getch() or getche() (depending whether you do/don't need echo to the screen) to catch the first letter and then gets() to get the rest of the sentence.  A possible pitfall if the user wants to use backspace to delete the first typed letter.  You need to think about it.

2.Writing to a file is easy - are you using C or C++?
in C you would do something like this:

FILE *fp;

if((fp=fopen("filename","a"))==NULL)
    /* Some error occurred */
else {
    fprintf(fp,"%c%s\n%1.4f\n",first_char,buffer,duration);
    fclose(fp);
}

If you're using C++ you just open a file output tream fos and do
    fos << first_char << buffer << endl << duration;
After that, close the stream.

Hope this helps,
-Aleks.
0
 

Author Comment

by:haubnerk
ID: 1174623
Thanks for the quick response, I will give this a try.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

609 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question