Question
question: using function and taking references of two parameters to find out the highest score and the name from a txt file dont know why
question: using function and taking references of two parameters to find out the highest score and the name from a txt file
dont know why it turned out like this, seems like it's functioning
please find out where this program went wrong, thanks!!
#define _CRT_SECURE_NO_DEPRECATE
#include
#include
void getHighScore(char *name, int *score );
int main()
{
char name[20];
int score = 0;
getHighScore(name, &score);
printf("%s %d ", name, score);
system("pause"); // prevent the cmd close itself after the program has finished
return 0;
}
void getHighScore(char *name, int *score)
{
FILE * fpointer;
fpointer = fopen("scores.txt", "r"); // opens the txt file inorder to read the info, "r" stands for read
char thisLine[150]; //use to store content from the txt every single line
char names[50];
int scores = 0;
int current_score = 0;
int i = 1;
while (!feof(fpointer)) //run to the end, feof stands for file end of file
{
if (i % 2 == 0)
{
fgets(thisLine,150, fpointer);
current_score = atoi(thisLine);
}
else
{
fgets(names, 150, fpointer); /ames are used to store names from the txt
}
if (current_score > scores)
{
scores = current_score;
for (int k = 0; names[k] != '\0'; ++k)
{
name[k] = names[k];
}
}
i++;
}
*score = scores;
fclose(fpointer);
}
#define CRT SECURE NO DEPRECATE
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started