Quantcast
Channel: C - Codecademy Forums
Viewing all articles
Browse latest Browse all 25

Race simulator in c only printing first letter of name and color

$
0
0

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

// Structures section
struct Race{
int numberOfLaps;
int currentLap;
char firstPlaceDriverName[20];
char firstPlaceRaceCarColor[10];

};
struct RaceCar{
char driverName[20];
char raceCarColor[10];
int totalLapTime;

};

// Print functions section
void printIntro(void){
printf(“Welcome to our main event digital race fans! \n I hope everybody has their snacks because we are about to begin!\n”);
}
void printCountDown(void){
printf(“Racers Ready! In…\n 5 \n 4 \n 3 \n 2 \n 1 \n RACE!!!\n”);
}
void printFirstPlaceAfterLap(struct Race race){
printf(“After lap number %d\n”, race.currentLap );
printf(“First place is %s in the %s car!\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 It truly was a great race and everybody have a goodnight!”, race.firstPlaceDriverName, race.firstPlaceRaceCarColor);
}
// Logic functions section
int calculateTimeToCompleteLap(void){
int speed= (rand()%3)+1;
int acceleration= (rand()%3)+1;
int nerves=(rand()%3)+1;
int all= speed+ acceleration+ nerves;
return all;
}
void updateRaceCar(struct RaceCar* racecar){
racecar->totalLapTime+= calculateTimeToCompleteLap();

}
void updateFirstPlace(struct Race* race, struct RaceCar* raceCar1, struct RaceCar* raceCar2){
if (raceCar1->totalLapTime<= raceCar2->totalLapTime){
*race->firstPlaceDriverName= *raceCar1->driverName;
*race->firstPlaceRaceCarColor = *raceCar1->raceCarColor;
}else{
*race->firstPlaceDriverName= *raceCar2->driverName;
race->firstPlaceRaceCarColor = raceCar2->raceCarColor;
}
}
void startRace(struct RaceCar
raceCar1, struct RaceCar
raceCar2){
struct Race race= {5,1,“”,“”};
for (int i=0;i< race.numberOfLaps; i++){
updateRaceCar(raceCar1);
updateRaceCar(raceCar2);
updateFirstPlace(&race, raceCar1, raceCar2);
printFirstPlaceAfterLap(race);
race.currentLap+=1;
}
printCongratulation(race);

}

//main
int main() {
printIntro();
printCountDown();
srand(time(0));
struct RaceCar raceCar1 = {
.driverName = “Ant”,
.raceCarColor = “Green”,
.totalLapTime = 0
};

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

startRace(&raceCar1, &raceCar2);

};

That is my code and for some reason it is only printing the first letter of each name and the color- I have no idea why!!

2 posts - 2 participants

Read full topic


Viewing all articles
Browse latest Browse all 25

Trending Articles