每当我调用我的 tieFix() 函数时,它都会以某种方式导致我的 displayRace() 函数读取 struct carData racers[0].distance 中的一些随机大数。每当我从主函数中排除 tieFix() 时,它就会恢复正常,我无法确定原因。我认为这可能是一个内存问题,但我太菜鸟了,无法解决这个问题。
我已经尝试打印 racers[0].distance 的内容,直到我的 displayRace() 函数使用它,它只会在 displayRace() 启动后立即更改,并且它会导致“*”以其读取的任何随机大数字打印来自赛车手[0].距离。我不确定从这里去哪里。下面是代码的一部分。
`
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h>
#include <math.h>
#include <string.h>
#define TRACKMAX 20
#define TRACKMIN 15
#define MOVEMAX 10
#define MOVEMIN 1
#define STRUCTROWS 4
#define MAXRACERS 20
#define PLAYER1 0
#define PLAYER2 1
#define NAME 30
#define CAR 20
#define COLOR 15
#define NUMPERLINE 1
**struct carData**
{
char name[NAME];
char car[CAR];
int num;
char color[COLOR];
int distance;
int rolls;
};
struct results
{
int place;
char racer[NAME];
int distance;
int rolls;
};
void createCarData(struct carData racers[], struct results placement[], FILE *read);
int trackLength();
int playerCount(FILE *read);
void saveMods(int player);
int moveCar(struct carData racers[], int totalRacers, int trackL);
**void displayRace(struct carData racers[], int totalRacers);**
int lapTracker(struct carData racers[], struct results placement[], int endRace, int trackL, int totalRacers);
**int tieFix(struct results placement[], int endCheck, int totalRacers);**
int main(void)
{
int trackL = 0, endRace = 1, endCheck = 0, totalRacers = MAXRACERS, i, j;
char again, mode;
FILE *dataRead = fopen("car.txt", "r");
totalRacers = playerCount(dataRead);
endCheck = totalRacers;
printf("testing %d\n", totalRacers);
struct carData racers[MAXRACERS];
struct results placement[MAXRACERS];
dataRead = fopen("car.txt", "r");
createCarData(racers, placement, dataRead);
fclose(dataRead);
printf("Would you like to play in Multi-player mode or Auto mode?");
printf("\nEnter M for multi-player and A for auto.\n\tDecision?:");
scanf("%c", &mode);
while (mode != 'm' && mode != 'M' && mode != 'a' && mode != 'A')
{
printf("\nYour response was invalid. Please try again.\nEnter M for multi-player and A for auto.\n\tDecision?:");
scanf("%c", &mode);
}
do
{
if (mode == 'a' || mode == 'A')
{
trackL = trackLength();
displayRace(racers, totalRacers);
for (i = 0; i < totalRacers; i++)
{
placement[i].place = 0;
}
do
{
moveCar(racers, totalRacers, trackL);
**displayRace(racers, totalRacers);**
printf("Track:%d\nendRace:%d\n", trackL, endRace);
endRace += lapTracker(racers, placement, endRace, trackL, totalRacers);
**endCheck = tieFix(placement, endCheck, totalRacers);**
}while (endRace < endCheck+1);
}
else
{
}
} while(again == 'Y' || again == 'y');
return 0;
}
**void displayRace(struct carData racers[], int totalRacers)**
{
int i, j;
for (i = 0; i < totalRacers; i++)
{
fflush(stdin);
printf("%s\t#%d\t%d\t", racers[i].name, racers[i].num, racers[i].distance);
for(j = 0; j < racers[i].distance;j++)
{
printf("*");
}
printf("\n");
}
}
**int tieFix(struct results placement[], int endCheck, int totalRacers)**
{
int i, j;
for(i = 0; i < totalRacers; i++)
{
for (j = 0; j < totalRacers; j++)
{
if (placement[i].rolls == placement[j].rolls && placement[i].distance == placement[j].distance && placement[i].place < placement[j].place)
{
placement[j].place = placement[i].place;
endCheck--;
}
}
}
return (endCheck);
}
int lapTracker(struct carData racers[], struct results placement[], int endRace, int trackL, int totalRacers)
{
int i, j;
int total = 0, upPlace = 0, downPlace = 0;
int original = endRace;
for(i = 0; i < totalRacers; i++)
{
if(racers[i].distance >= trackL && placement[i].place == 0)
{
placement[i].place = endRace;
placement[i].distance = racers[i].distance;
strcpy(placement[i].racer, racers[i].name);
placement[i].rolls = racers[i].rolls;
endRace++;
fflush(stdin);
fflush(stdout);
printf("%s %d\n", placement[i].racer, placement[i].place);
}
}
for (i = 0; i < totalRacers; i++)
{
for (j = 0; j < totalRacers; j++)
{
if (placement[i].rolls == placement[j].rolls && placement[i].distance < placement[j].distance && placement[i].place < placement[j].place)
{
downPlace = placement[i].place;
upPlace = placement[j].place;
placement[i].place = upPlace;
placement[j].place = downPlace;
}
}
}
if (endRace != original)
{
total = endRace - original;
printf("%d\t%d\t%d\t\n", total, endRace, original);
}
for (i = 0; i < totalRacers; i++)
{
printf("--%s\t%d\t%d\t%d--\n", placement[i].racer, placement[i].place, placement[i].distance, placement[i].rolls);
}
printf("Finish Count:%d\n", endRace);
return (total);
}
int moveCar(struct carData racers[], int totalRacers, int trackL)
{
int i, j;
for(i = 0; i < totalRacers; i++)
{
Sleep(500);
int distance = 0;
srand(time(NULL));
distance = rand() % MOVEMAX + MOVEMIN;
if (racers[i].distance < trackL)
{
racers[i].distance += distance;
racers[i].rolls++;
}
}
}
int trackLength()
{
int length = 0;
srand(time(NULL));
length = rand() % (TRACKMAX - TRACKMIN + 1) + TRACKMIN;
return (length);
}
int playerCount(FILE *read)
{
int lines = 0, number = 0, totalChar = 0;
char newLine;
while ((newLine = fgetc(read)) != EOF)
{
if (newLine == '\n')
{
lines++;
totalChar = 0;
}
else
{
totalChar++;
}
}
if (totalChar > 0)
{
lines++;
}
return(lines);
}
void createCarData(struct carData racers[], struct results placement[], FILE *read)
{
int i = 0;
while (fscanf(read, "%s %s %d %s", racers[i].name, racers[i].car, &racers[i].num, racers[i].color) == STRUCTROWS)
{
i++;
racers[i].distance = 0;
placement[i].distance = 0;
placement[i].place = 0;
racers[i].rolls = 0;
}
}
`