Help with the final Structure project - Race Simulator

Hello, I am having difficulty getting the Race Simulator project to run correctly. I am almost certain that it has something to do with mixing up *'s and &'s when using pointers in my functions (I really wish the section on pointers had been more thorough because I still feel like I hardly understand what they do / why they are necessary).

Any help is much appreciated!

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

// Structures section
struct Race {
    int numberOfLaps;
    int currentLap;
    char* firstPlaceDriverName;
    char* firstPlaceRaceCarColor;
};

struct RaceCar {
    char* driverName;
    char* raceCarColor;
    int totalLapTime;
};

// Print functions section
const char* printIntro(void) {
    return "\n\nWelcome to the race\n\n";
}

const char* printCountDown(void) {
    return "Races ready! In...\n5\n4\n3\n2\n1\nRace!\n\n";
}

void printFirstPlaceAfterLap(struct Race race) {
    printf("After lap number %d\n", race.currentLap); 
    printf("First place is: %s in the %s car!\n\n", race.firstPlaceDriverName, race.firstPlaceRaceCarColor);
}

void printCongratulation(struct Race race) {
    printf("Let's all congratulate %s in the %s race car for an amazing performance!\n\n", race.firstPlaceDriverName, race.firstPlaceRaceCarColor);
}

// Logic functions section
int calculateTimeToCompleteLap(void) {
    int speed, acceleration, nerves;

    
    speed = (rand() % 3) +1;
    acceleration = (rand() % 3) +1;
    nerves = (rand() % 3) +1;

    return speed + acceleration + nerves;

}


void updateRaceCar(struct RaceCar* raceCarPointer) {
    raceCarPointer->totalLapTime += calculateTimeToCompleteLap();

}

void updateFirstPlace(struct Race* racePointer, struct RaceCar* raceCar1Pointer, struct RaceCar* raceCar2Pointer) {

    if (raceCar1Pointer->totalLapTime <= raceCar2Pointer->totalLapTime) {
        racePointer->firstPlaceDriverName = raceCar1Pointer->driverName;
        racePointer->firstPlaceRaceCarColor = raceCar1Pointer->raceCarColor;
    } else {
        racePointer->firstPlaceDriverName = raceCar2Pointer->driverName;
        racePointer->firstPlaceRaceCarColor = raceCar2Pointer->raceCarColor;
    }
    

}

void startRace(struct RaceCar* raceCar1Pointer, struct RaceCar* raceCar2Pointer) {
    struct Race race = {
        .numberOfLaps = 5,
        .currentLap = 1,
        .firstPlaceDriverName = "",
        .firstPlaceRaceCarColor = ""
    };
    
    for (int i = 0; i <= race.numberOfLaps; i++) {
        updateRaceCar(raceCar1Pointer);
        updateRaceCar(raceCar2Pointer);
        updateFirstPlace(&race, raceCar1Pointer, raceCar2Pointer);
        printFirstPlaceAfterLap(race);
    }

    printCongratulation(race);

}

int main() {
	srand(time(0));

    struct RaceCar raceCar1 = {
        .driverName = "Ant",
        .raceCarColor = "Green",
        .totalLapTime = 0
    };

    struct RaceCar raceCar2 = {
        .driverName = "Taylor",
        .raceCarColor = "Black",
        .totalLapTime = 0
    };

    printIntro();
    startRace(&raceCar1, &raceCar2);


};
1 Like

Could you explain what it is that goes wrong? Is it a compilation error or something at run-time?

The for loop in your startRace function only increments the i variable. It never updates the currentLap. So, it loops through 5 times like it should but it never actually updates the lap. You can simply add race.currentLap++ under printFirstPlaceAfterLap(race)

1 Like

Hi,
Just finished this project so I understand where you might have some problems
void startRace(struct RaceCar* raceCar1Pointer, struct RaceCar* raceCar2Pointer) {

struct Race race = {

    .numberOfLaps = 5,

    .currentLap = 0,

    .firstPlaceDriverName = "",

    .firstPlaceRaceCarColor = ""

};



for (int i = 0; i <= race.numberOfLaps; i++) {

    race.currentLap++;

    updateRaceCar(raceCar1Pointer);

    updateRaceCar(raceCar2Pointer);

    updateFirstPlace(&race, raceCar1Pointer, raceCar2Pointer);

    printFirstPlaceAfterLap(race);

}

printCongratulation(race);

}

Instead of initializing the first lap at 1 start it at 0 then add to the currentLap++ so the number changes.
This is what I did so it would show each lap.
My solution is here Journey/raceSimulator.c at main · 1ong1evi/Journey · GitHub

1 Like

Hi,
Following my repository with the code: https://github.com/fjorwb/RaceSimulator