| | |
Time function
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Nov 2004
Posts: 123
Reputation:
Solved Threads: 0
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
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?
C Syntax (Toggle Plain Text)
void rounds(int rand, int guess) { int attempts=1; const int MAX_ATTEMPTS = 5; int score; long int timetaken; time_t startTime,endTime; while(attempts <= 5) { if (rand==guess) { printf("Congratulations,You are correct!\n"); time(&startTime); break; } if ((unsigned int)(rand-guess) >40) { printf("You are Cold\n"); } else if (10<=(unsigned int)(rand-guess)) { printf("You are Warm\n"); } else if ((unsigned int)(rand-guess) < 10) { printf("You are Hot!\n"); } }/*endwhile*/ time(&endTime); timetaken=endTime-startTime; ++attempts; score += 5*(MAX_ATTEMPTS - attempts)*exp(5.0)/timetaken; }
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.
•
•
Join Date: Jul 2005
Posts: 1,761
Reputation:
Solved Threads: 283
>>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?
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.
•
•
Join Date: Nov 2004
Posts: 123
Reputation:
Solved Threads: 0
C Syntax (Toggle Plain Text)
/*function prototype*/ void rounds(int,int); 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.
•
•
Join Date: Jul 2005
Posts: 1,761
Reputation:
Solved Threads: 283
>>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?
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?
•
•
Join Date: Nov 2004
Posts: 123
Reputation:
Solved Threads: 0
I've built and rebuilt several times the variables are all integers
here is my updated function:
Still getting that error
here is my updated function:
C Syntax (Toggle Plain Text)
int rounds(int,int);/*Prototype*/ score1=rounds(ranNum,guess1);/*Call rounds function*/ int rounds(int random, int guess) { int attempts=1; const int MAX_ATTEMPTS = 5; int score; long int timetaken; time_t startTime,endTime; time(&startTime); while(attempts <= 5) { if (random==guess) { printf("Congratulations,You are correct!\n"); break; } if ((unsigned int)(random-guess) >40) { printf("You are Cold\a\n"); } else if (10<=(unsigned int)(random-guess)) { printf("You are Warm\a\n"); } else if ((unsigned int)(random-guess) < 10) { printf("You are Hot!\a\n"); } }/*endwhile*/ time(&endTime); timetaken=endTime-startTime; ++attempts; score += 5*(MAX_ATTEMPTS - attempts)*exp(5.0)/timetaken; return score; }
Still getting that error
•
•
Join Date: Jul 2005
Posts: 1,761
Reputation:
Solved Threads: 283
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.
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.
•
•
Join Date: Nov 2004
Posts: 123
Reputation:
Solved Threads: 0
Heres the code I know the indention is terrible but hopefully u would understand:
C Syntax (Toggle Plain Text)
#include<stdlib.h> #include<stdio.h> #include<time.h> #include<math.h> /*Function Prototypes*/ int genRandom(void); void lplayer(int,int,int); int rounds(int,int); int main(void) { /*Declare variables*/ int rounds; int ranNum; int attempts=1; int score1; int score2; int cumscore1; int cumscore2; int guess1; int guess2; char quit={0}; ranNum=genRandom();/*Computer generates random number*/ printf("Player1,Guess the value of the random number\n"); scanf("%d",&guess1); if((guess1>100)||(guess1<1)) { printf("Invalid your guess must be more than 0 and less than 100"); } printf("Player2,Guess the value of the random number\n"); scanf("%d",&guess2); if((guess2>100)||(guess2<1)) { printf("Invalid your guess must be more than 0 and less than 100"); } lplayer(ranNum,guess1,guess2);/*Call lplayer function*/ do{ ranNum=0; guess1=0; guess2=0; ranNum=genRandom();/*Computer generates random number*/ //if the random function generates a zero it will be initialized to 1 if(ranNum == 0) { ranNum += 1; } printf("Lead player, please enter your guess"); scanf("%d",&guess1); if((guess1>100)||(guess1<0)) { printf("Invalid!!!your number must less than 100 or more than 0\n"); } score1=rounds(ranNum,guess1);/*Call rounds function*/ printf("Next player, please enter your guess"); scanf("%d",&guess2); if((guess1>100)||(guess1<0)) { printf("Invalid!!!your number must less than 100 or more than 0\n\n"); } score2=rounds(ranNum,guess2);/*Call rounds function*/ printf("\n\n"); printf("Enter Q to quit, C to continue >"); scanf(" %c", &quit); }while ((quit != 'Q') && (quit != 'q')); } int genRandom(void) { return rand()%100; } void lplayer(int ran, int guessone, int guesstwo) { if(ran-guesstwo < ran-guessone) { printf("Player2 you are the lead player"); } else { printf("Player1 you are the lead player"); } } int rounds(int rands, int guess) { int attempts=1; const int MAX_ATTEMPTS = 5; int score; long int timetaken; time_t startTime,endTime; time(&startTime); while(attempts <= 5) { if (rands==guess) { printf("Congratulations,You are correct!\n"); break; } if ((unsigned int)(rands-guess) >40) { printf("You are Cold\a\n"); } else if (10<=(unsigned int)(rands-guess)) { printf("You are Warm\a\n"); } else if ((unsigned int)(rands-guess) < 10) { printf("You are Hot!\a\n"); } }/*endwhile*/ time(&endTime); timetaken=endTime-startTime; ++attempts; score += 5*(MAX_ATTEMPTS - attempts)*exp(5.0)/timetaken; return score; }
![]() |
Similar Threads
- Help with a time function (PHP)
- computation time for arithmeic operations (C)
- How to Get Last Accessed/Created/Modified File Date and Time (C)
- Sorting Algorithms using Time (C++)
- a little prob with TIME (C++)
- No "Data & Time" Function (Windows 95 / 98 / Me)
Other Threads in the C Forum
- Previous Thread: tab completion
- Next Thread: clear stream
Views: 3013 | Replies: 11
| Thread Tools | Search this Thread |
Tag cloud for C
#include * .net append array arrays bash binarysearch changingto char character cm copyanyfile copypdffile createprocess() database directory drawing dynamic execv feet fgets file floatingpointvalidation fork function functions getlogicaldrivestrin givemetehcodez global grade graphics gtkwinlinux histogram homework i/o ide include infiniteloop initialization input interest intmain() iso keyboard kilometer lazy license linked linkedlist linux list looping loopinsideloop. lowest matrix meter microsoft mqqueue mysql oddnumber odf open openwebfoundation overwrite pause pdf pointer pointers posix power process program programming pyramidusingturboccodes read recursion recv recvblocked reversing segmentationfault single socket socketprogramming spoonfeeding standard strchr string student suggestions system test testing threads unix urboc user whythiscodecausesegmentationfault win32api windowsapi






