How To Determine The Best Score?
I am doing a lab regarding 'guess number game'
Actually,the answer should be the random number,but I set the exact number to 88,because it is easy for me to test other requirement.
I need to printf("Best Score so far [X]")
How to make comparison between the previous and now guess game,and define the best score.

``````#include <stdio.h>
int main()

{
int GusNum,TotalNum,BesSor,a;
char again;

do
{   TotalNum=0;
do
{
printf("Guess what the lucky number is <between 1 and 100>,then <Enter>:");
scanf("%d",&GusNum);

if(GusNum>=89&&GusNum<=100)
printf("The number you typed is [%d] -Too LARGE,guess the lucky number again!\n\n",GusNum);
else if (GusNum<=87&&GusNum>=1)
printf("The number you typed is [%d] -Too SMALL,guess the lucky number again!\n\n",GusNum);
else if(GusNum==88)
printf("OK This is your LUCKY NUMBER [88] Well Done!\n");
else
printf("OUT OF THE RANGE \n");

TotalNum++;

}while(GusNum!=88);

if(GusNum==88)
{
printf("Number of attempt [%d]\n\n",TotalNum);
/*
if(a>TotalNum)
{
printf("Best score so far [%d]\n",TotalNum);

}else printf("Best score so far [%d]\n",a);
the wrong part i want to set the best socre*/

printf("Play again Type 'y' for YES; all other characters for NO:");
scanf(" %c", &again);

}
}while(again=='y');
}
``````

Well what is the best score? Is it closest without going over? Is it just the closest guess? Are you trying to see how many attemps it takes to find the random number? Once you know exactly what you want then you can start writing code.

Of course, if the user/player uses a binary search algorithm (start at 50, then 75, then 88) They will win in a maximum of 3 moves... Anyway, what NathanOliver said - what is your definition of "score"? Is it the number of attempts? Another observation: after your loop, you test the value of GusNum again. This is not necessary since you don't break out of the loop until the player guesses "88".

You need to add a variable to hold a temporary best score with a default value and then test against that each iteration like this:

`````` #include <stdio.h>
main() {
int GusNum,TotalNum,BesSor,a;
char again;

// a variable to hold the best score

int BEST_SCORE = 10; // Variable to hold fewest tries (Best Score right?)

// end of inserted code

do
{ TotalNum=0;
do
{
printf("Guess what the lucky number is <between 1 and 100>,then <Enter>:");
scanf("%d",&GusNum);
if(GusNum>=89&&GusNum<=100)
printf("The number you typed is [%d] -Too LARGE,guess the lucky number again!\n\n",GusNum);
else if (GusNum<=87&&GusNum>=1)
printf("The number you typed is [%d] -Too SMALL,guess the lucky number again!\n\n",GusNum);
else if(GusNum==88)
printf("OK This is your LUCKY NUMBER [88] Well Done!\n");
else
printf("OUT OF THE RANGE \n");
TotalNum++;
}while(GusNum!=88);
if(GusNum==88)
{
printf("Number of attempt [%d]\n\n",TotalNum);

// what you need to add I think

if(TotalNum < BEST_SCORE){
BEST_SCORE = TotalNum; // set tries as new best score
printf("Best Score so far is %d", TotalNum);
printf("\n\n");
}else{
printf("Sorry, but the best score is: %d and you took %d tries...", BEST_SCORE, TotalNum);
}

// end of inserted code

/*
if(a>TotalNum)
{
printf("Best score so far [%d]\n",TotalNum);
}else printf("Best score so far [%d]\n",a);
the wrong part i want to set the best socre*/
printf("Play again Type 'y' for YES; all other characters for NO:");
scanf(" %c", &again);
}
}while(again=='y');

}
``````
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.