Time function

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Nov 2004
Posts: 123
Reputation: boujibabe is an unknown quantity at this point 
Solved Threads: 0
boujibabe boujibabe is offline Offline
Junior Poster

Time function

 
0
  #1
Feb 21st, 2007
I have a function called rounds that that carries out a round for a two player guessing game the score is calculated based on the time taken and the attempts used

  1. void rounds(int rand, int guess)
  2. {
  3. int attempts=1;
  4. const int MAX_ATTEMPTS = 5;
  5. int score;
  6. long int timetaken;
  7. time_t startTime,endTime;
  8.  
  9. while(attempts <= 5)
  10. {
  11.  
  12. if (rand==guess)
  13. {
  14. printf("Congratulations,You are correct!\n");
  15. time(&startTime);
  16. break;
  17. }
  18. if ((unsigned int)(rand-guess) >40)
  19. {
  20. printf("You are Cold\n");
  21. }
  22. else if (10<=(unsigned int)(rand-guess))
  23. {
  24. printf("You are Warm\n");
  25. }
  26. else if ((unsigned int)(rand-guess) < 10)
  27. {
  28. printf("You are Hot!\n");
  29. }
  30. }/*endwhile*/
  31. time(&endTime);
  32.  
  33. timetaken=endTime-startTime;
  34.  
  35. ++attempts;
  36.  
  37. score += 5*(MAX_ATTEMPTS - attempts)*exp(5.0)/timetaken;
  38.  
  39. }

Firstly i'm getting an error when I call the rounds function into main, "term does not evaluate to a function taking two arguments" I checked the prototype and the definition but I'm not seeing it.
Second I want store the score for the round for each player and then at the end of a round display a winner should the function return the score?
Last edited by boujibabe; Feb 21st, 2007 at 1:31 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Time function

 
0
  #2
Feb 21st, 2007
> "term does not evaluate to a function taking two arguments"
You have an int parameter called rand, which is hiding the function rand() by scope.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 123
Reputation: boujibabe is an unknown quantity at this point 
Solved Threads: 0
boujibabe boujibabe is offline Offline
Junior Poster

Re: Time function

 
0
  #3
Feb 21st, 2007
I changed the variable to random and I'm still getting the error
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,761
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 283
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Time function

 
0
  #4
Feb 21st, 2007
I found this when looking up informtaion about time_t:

In the GNU system, you can simply subtract time_t values. But on other systems, the time_t data type might use some other encoding where subtraction doesn't work directly.

I'd suggest using difftime() to calculate the elapsed time interval.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,761
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 283
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Time function

 
0
  #5
Feb 21st, 2007
>>I checked the prototype and the definition but I'm not seeing it.

Please post function prototype and function call in addition to function definition.

>> Second I want store the score for the round for each player and then at the end of a round display a winner should the function return the score?

Probably.


In addition, I noticed that each iteration through the while loop generates a new startTime value. Wouldn't it seem more reasonable to get a single startTime before the while loop starts and the endTime after the while loop finishes?
Last edited by Lerner; Feb 21st, 2007 at 3:42 pm.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 123
Reputation: boujibabe is an unknown quantity at this point 
Solved Threads: 0
boujibabe boujibabe is offline Offline
Junior Poster

Re: Time function

 
0
  #6
Feb 21st, 2007
  1. /*function prototype*/
  2. void rounds(int,int);
  3.  
  4. rounds(ranNum,guess1);/*Call rounds function*/

The error is on the function call line.Can the same function return the score to main to determine the winner?
Last edited by boujibabe; Feb 21st, 2007 at 5:45 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,761
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 283
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Time function

 
0
  #7
Feb 21st, 2007
>>Can the same function return the score to main to determine the winner?

Sure, either use the value of score as a return value, or pass score to the function as reference parameter type. You would then need to store each score value returned in some variable/container to compare with other scores that are generated to determine the winner.

>>The error is on the function call line.

Have you done a complete rebuild since making corrections to your program? Are the arguments passed in the function call declared as type int?
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 123
Reputation: boujibabe is an unknown quantity at this point 
Solved Threads: 0
boujibabe boujibabe is offline Offline
Junior Poster

Re: Time function

 
0
  #8
Feb 21st, 2007
I've built and rebuilt several times the variables are all integers

here is my updated function:

  1. int rounds(int,int);/*Prototype*/
  2. score1=rounds(ranNum,guess1);/*Call rounds function*/
  3.  
  4. int rounds(int random, int guess)
  5. {
  6. int attempts=1;
  7. const int MAX_ATTEMPTS = 5;
  8. int score;
  9. long int timetaken;
  10. time_t startTime,endTime;
  11.  
  12. time(&startTime);
  13. while(attempts <= 5)
  14. {
  15.  
  16. if (random==guess)
  17. {
  18. printf("Congratulations,You are correct!\n");
  19. break;
  20. }
  21. if ((unsigned int)(random-guess) >40)
  22. {
  23. printf("You are Cold\a\n");
  24. }
  25. else if (10<=(unsigned int)(random-guess))
  26. {
  27. printf("You are Warm\a\n");
  28. }
  29. else if ((unsigned int)(random-guess) < 10)
  30. {
  31. printf("You are Hot!\a\n");
  32. }
  33. }/*endwhile*/
  34. time(&endTime);
  35.  
  36. timetaken=endTime-startTime;
  37.  
  38. ++attempts;
  39.  
  40. score += 5*(MAX_ATTEMPTS - attempts)*exp(5.0)/timetaken;
  41. return score;
  42. }

Still getting that error
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,761
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 283
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Time function

 
0
  #9
Feb 21st, 2007
I'd either try commenting out the function call and rebuild/run the program to see if the error goes away or post the entire program, assuming it isn't too long, so somebody can try compiling it on their compiler.

BTW, random() is another function in the standard header files, so rename the variable something other than random. (maybe call randNum or ranNum_ or RandNum or whatever). Maybe that will do it.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 123
Reputation: boujibabe is an unknown quantity at this point 
Solved Threads: 0
boujibabe boujibabe is offline Offline
Junior Poster

Re: Time function

 
0
  #10
Feb 21st, 2007
Heres the code I know the indention is terrible but hopefully u would understand:

  1.  
  2. #include<stdlib.h>
  3. #include<stdio.h>
  4. #include<time.h>
  5. #include<math.h>
  6.  
  7. /*Function Prototypes*/
  8.  
  9. int genRandom(void);
  10. void lplayer(int,int,int);
  11. int rounds(int,int);
  12.  
  13. int main(void)
  14.  
  15. {
  16. /*Declare variables*/
  17.  
  18. int rounds;
  19. int ranNum;
  20. int attempts=1;
  21. int score1;
  22. int score2;
  23. int cumscore1;
  24. int cumscore2;
  25. int guess1;
  26. int guess2;
  27. char quit={0};
  28.  
  29. ranNum=genRandom();/*Computer generates random number*/
  30.  
  31. printf("Player1,Guess the value of the random number\n");
  32. scanf("%d",&guess1);
  33.  
  34. if((guess1>100)||(guess1<1))
  35. {
  36. printf("Invalid your guess must be more than 0 and less than 100");
  37. }
  38.  
  39. printf("Player2,Guess the value of the random number\n");
  40. scanf("%d",&guess2);
  41.  
  42. if((guess2>100)||(guess2<1))
  43. {
  44. printf("Invalid your guess must be more than 0 and less than 100");
  45. }
  46.  
  47. lplayer(ranNum,guess1,guess2);/*Call lplayer function*/
  48.  
  49. do{
  50. ranNum=0;
  51. guess1=0;
  52. guess2=0;
  53.  
  54. ranNum=genRandom();/*Computer generates random number*/
  55.  
  56. //if the random function generates a zero it will be initialized to 1
  57. if(ranNum == 0)
  58. {
  59. ranNum += 1;
  60. }
  61. printf("Lead player, please enter your guess");
  62. scanf("%d",&guess1);
  63.  
  64. if((guess1>100)||(guess1<0))
  65. {
  66. printf("Invalid!!!your number must less than 100 or more than 0\n");
  67. }
  68.  
  69. score1=rounds(ranNum,guess1);/*Call rounds function*/
  70.  
  71. printf("Next player, please enter your guess");
  72. scanf("%d",&guess2);
  73.  
  74. if((guess1>100)||(guess1<0))
  75. {
  76. printf("Invalid!!!your number must less than 100 or more than 0\n\n");
  77. }
  78.  
  79. score2=rounds(ranNum,guess2);/*Call rounds function*/
  80.  
  81. printf("\n\n");
  82. printf("Enter Q to quit, C to continue >");
  83. scanf(" %c", &quit);
  84.  
  85. }while ((quit != 'Q') && (quit != 'q'));
  86.  
  87. }
  88. int genRandom(void)
  89. {
  90. return rand()%100;
  91. }
  92.  
  93. void lplayer(int ran, int guessone, int guesstwo)
  94. {
  95. if(ran-guesstwo < ran-guessone)
  96. {
  97. printf("Player2 you are the lead player");
  98. }
  99. else
  100. {
  101. printf("Player1 you are the lead player");
  102. }
  103. }
  104.  
  105. int rounds(int rands, int guess)
  106. {
  107. int attempts=1;
  108. const int MAX_ATTEMPTS = 5;
  109. int score;
  110. long int timetaken;
  111. time_t startTime,endTime;
  112.  
  113. time(&startTime);
  114. while(attempts <= 5)
  115. {
  116.  
  117. if (rands==guess)
  118. {
  119. printf("Congratulations,You are correct!\n");
  120. break;
  121. }
  122. if ((unsigned int)(rands-guess) >40)
  123. {
  124. printf("You are Cold\a\n");
  125. }
  126. else if (10<=(unsigned int)(rands-guess))
  127. {
  128. printf("You are Warm\a\n");
  129. }
  130. else if ((unsigned int)(rands-guess) < 10)
  131. {
  132. printf("You are Hot!\a\n");
  133. }
  134. }/*endwhile*/
  135. time(&endTime);
  136.  
  137. timetaken=endTime-startTime;
  138.  
  139. ++attempts;
  140.  
  141. score += 5*(MAX_ATTEMPTS - attempts)*exp(5.0)/timetaken;
  142. return score;
  143. }
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 3013 | Replies: 11
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC