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