Hello fellow coders! I am kind of stuck on one aspect of a project that I am currently working on. I have a code for a number guessing game in "C" but I need some assistance as to how to add a "high score" feature to the game. Basically, I want the lowest score to be the best, the score being how many guesses it took the user to get the correct answer. Any help at all will be greatly appreciated! Here is the code thus far:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h>


int main( void )
{

srand(time(NULL));
int i,n=0,r=rand()%100 + 1;

(void)printf("Welcome to GUESSTIMATOR 2000\n\nI am thinking of a number between 1 and 100. \n\nCan you guess what it is? ");
while(scanf("%d",&i))

{       

if (n >= 9 && i != r)

{          

printf("\n\nSorry, the number was %d.\n", r);
printf("You should have gotten it by now.\n");          
printf("Better luck next time.\n\n");          
system ("PAUSE");
break;       }       

if (i > r) 
{          
n++;

printf("Your guess is high. You only get 10 guesses. Try again: ");      
}      
else if (i < r) 
{          
n++;          
printf("Your guess is low. You only get 10 guesses. Try again: ");      
}      
else if (i == r) 
{              
printf("\n\nCongratulations!\nYou guessed the number within %d guesses! \n\n", n+1);

system ("PAUSE");
return;           
    }     
}
return 0;
}
Ancient Dragon commented: Thanks for reading RULES and using code tags +21

First, some thoughts:

Your compiler lets you get away with it, but in C you should generally stick all variables before any code:

int i, n = 0, r;
srand( time( NULL ) );
r = rand() %100 +1;

Don't use (void) in front of printf(). (There's no need.)

You don't need <windows.h> (you aren't using anything in there so it only serves to make your program only compile on Windows).

That return; on line #45 should be a break; Here's a little function for you:

void pause()
{
  int c;
  printf( "Please press ENTER to continue..." );
  do { c = getchar(); } while ((c != '\n') && (c != EOF));
}

Now you don't need to call system("PAUSE") , which is non-portable also...

OK. For your question, you want to keep the top score for each time the game is played. As it is, your program only plays the game once then quits. Is this how you plan to keep it or do you want to have the game play until the user says he's done and only keep top score for that?

If the former, you'll have to create a high scores file (something like "highscore.dat"). When your program starts, open it and read the high score. When your program is ready to end, if the person's high score is better than the last one, rewrite the file with the new high score.

Hope this helps.

Thanks for you help Duoas! I will try your suggestions!

Also, I was thinking of just playing the game once...I am just confused as to how to write the code to print to a txt file to save the scores. The scoring would be how many guesses it took the user to get the correct answer, so, the lower the score the better. Sorry, I should have explained that in my main post.

You may define the maximum number of attempts made, say N. Then if the user guesses correctly in M guesses, then assign (N-M) to score. Here, better the guessing power, higher will be the value of (N-M).

I am just confused as to how to write the code to print to a txt file to save the scores.

I don't think you will need to print the txt file. You just need to save 5 high scores and the respective usernames in a txt file. Then, whenever a user scores higher than the lowest high score, update the high score list by making appropriate changes in the txt file.

Hope this helps.

You've done a fine job of explaining yourself, so don't worry.

Just like Jishnu says, use a simple text file. It can be shaped any way you want. For example:

1 Caitlyn
3 Aislann
3 Colm
6 Seamus
7 Bridget

Each line of the file is composed of:
- a high score
- a name

That format should be easiest to read and write...

Hope this helps.

I got the code to work, however I did not use functions, and my professor said I must use functions. So, let me show you guys what I had, that worked, but now I can't seem to figure out how to make it into functions:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main( void )
{

int i, n = 0, r;
srand( time( NULL ) );
r = rand() %100 +1;
char userName[15];
FILE *fout;

fout = fopen ("Highscores.txt", "a");
printf("Welcome to GUESSTIMATOR 2000.\n\n");
printf("Try to get the lowest amount of guesses!\n\n");
printf("Please type your name here: ");
scanf("%s", &userName);
printf("\nI am thinking of a number between 1 and 100.\n\n");
printf("Can you guess what it is? ");
while(scanf("%d", &i))
{
if (n >= 9 && i != r)
{
printf("\n\nSorry, the number was %d.\n", r);
printf("You should have gotten it by now.\n");
printf("Better luck next time.\n\n");
system ("PAUSE");
break;
}
if (i > r)
{
n++;
printf("Your guess is high. You only have %d guesses left. Try again: ", 10-n);
}
else if (i < r)
{
n++;
printf("Your guess is low. You only have %d guesses left. Try again: ", 10-n);
}
else if (i == r)
{
printf("\nCongratulations!\n\n");
printf("You guessed the number within %d guesses!\n\n", n+1);
printf("Open up the highscores text file to see your score.\n\n");
{
fprintf(fout, "\nHigh Scorer (the lower the better)\n");
fprintf(fout, "Name: %s\t\tScore: %d", userName, n+1);
fclose(fout);
}
system ("PAUSE");
break;
}
}
return 0;}

I have tried to use functions, and my code compiles, but does not run correctly:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void getData ( char* userName, int* i );
int calcGuess ( int i, int r, int n );
void printResults ( char* userName, int n );

int main ( void )
{

FILE* fout;

int i;
int r;
int n;

char userName[15];

srand( time( NULL ) );
r = rand() %100 + 1;
n = 0;

getData( userName, &i );
calcGuess( i, r, n );
printResults( userName, n );

system ("PAUSE");
return;
}

void getData ( char* userName, int* i )
{
printf("Welcome to GUESSTIMATOR 2000.\n\n");
printf("Try to get the lowest amount of guesses!\n\n");
printf("Please type your name here: ");
scanf("%s", userName);
printf("\nI am thinking of a number between 1 and 100.\n\n");
printf("Can you guess what it is? ");
scanf("%d", &i);
}

int calcGuess ( int i, int r, int n )
{


if (i == r)
{
printf("\nCongratulations!\n\n");
printf("You guessed the number within %d guesses!\n\n", n+1);
printf("Open up the highscores text file to see your score.\n\n");
}


if (i > r)
{
n++;
printf("Your guess is high. You only have %d guesses left. Try again: ", 10-n);
return;
}


if (i < r)
{
n++;
printf("Your guess is low. You only have %d guesses left. Try again: ", 10-n);
return;
}


if (n >= 9 && i != r)
{
printf("\n\nSorry, the number was %d.\n", r);
printf("You should have gotten it by now.\n");
printf("Better luck next time.\n\n");
}


}
void printResults ( char* userName, int n )
{
FILE *fout;
fout = fopen ("Highscores.txt", "a");
fprintf(fout, "High Scorer (the lower the better)\n");
fprintf(fout, "Name: %s\t\tScore: %d", userName, n+1);
fclose(fout);
return;
}

Any help or ideas would be greatly appreciated. I know what I have so far, in the function portion is a mess, but I really am trying...

Just a quick look shows:

int main()
{
  ...
  int i;

  getData( userName, &i );  // correct, send the address of 'i'

  ...


void getData( char *userName, int *i )
{
  ...
  scanf( "%d", &i );  // oops. Now you get the address of the address of main's 'i'

I suggest you change your routines to indicate whether you are dealing with an address or not.

void getData( char *userName, int *ptr_to_i )
{
  ...
  scanf( "%d", ptr_to_i );

Notice how I removed that "&" from the front of "ptr_to_i". I already have the address of the int. (With the ampersand you get the address of the pointer to the int, which is wrong.) Make sense?

For just a quick look-over, that's all I see. If that doesn't fix it I or someone else will take a closer look at your code.

BTW. What happened to your nice [[I][/I]code[I][/I]] blocks? (Only use [[I][/I]inlinecode[I][/I]] for a single line of code.)

Hope this helps.

Your compiler lets you get away with it, but in C you should generally stick all variables before any code...

Not generally. It's always. :icon_wink:

BTW. What happened to your nice [[I][/I]code[I][/I]] blocks? (Only use [[I][/I]inlinecode[I][/I]] for a single line of code.)

There are no nice code blocks. I fixed the CODE tags and there's still no indentation.

Caitlyn, please read this about code formatting. This will do various things:
1) Make your code more readable. You will be able to follow the code better
2) Help us read the code to give you better help
3) Help you get a better grade because your instructor will be able to follow your code
4) And it will help you get a job in the field if that's your desire (seriously)

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.