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

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?

> "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.

I changed the variable to random and I'm still getting the error

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.

>>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?

/*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?

>>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?

I've built and rebuilt several times the variables are all integers

here is my updated function:

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

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.

Heres the code I know the indention is terrible but hopefully u would understand:

#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;
}

same problems as before -- you have used the same name for functions and variables. You can not do that.

Heres the code I know the indention is terrible but hopefully u would understand:

Then why not take 30 seconds and make it neat so we don't have to struggle to understand the code. Don't post terribly formatted code you know is bad -- fix it! :rolleyes:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.