Code:
#include <stdio.h>
#include <time.h>
#include <ctype.h>
#include <stdlib.h>
#include <math.h>
#define CARS_ARRAY_SIZE 4
struct cars
{
char name[20];
char make[20];
int car_number;
char color[20];
int distance;
};
int main (void)
{
//declaring variables in array
int i=0;
int AutoOrManual;
int automatic;
int CarType;
int number;
int CarColor;
char DriverName;
int ExitFlag=0;
//opening file for arrayos
FILE *fPointerCars;
srand(time(NULL));//
//Print name and explain this is a racing game
printf("RoseEllen Hoke\n This is a race car game a text base game to race 4 cars across the screen and determine the first second and third place. \n A text file (car.txt) with the name of the driver, type of the race car, car number, and the color of the car is given to you.");
do
{
struct cars AutoManual[CARS_ARRAY_SIZE];
printf("enter 1 if you want race against another user or \n enter 2 if you want to race against 4 cars automatically or \n enter 3 to exit");
scanf("%d",&AutoOrManual);
switch(AutoOrManual)
{
case 1:
// i do not know how to get it to move to the next thing
printf("You have selected Manual");
//CAll Function that runs that type of race
break;
case 2:
printf("You have selected Automatic");
AutoRace();
break;
case 3:
printf("EXIT");
ExitFlag=1;
default:
break;
}
}while(ExitFlag==0)
// return 0;
//}
//i was going to enter another switch statement for manuel mode but i dont see why its uses all the same
//componets as autoomatic i feel like
//int AutoRace()
int car_pic(struct cars *carpointer, int size)
{
//CALL the function to read the file and pass it the local array of structures
//CALL pick car function and pass it the local array of structures
printf("enter 1 if you want to customize your car or 2 if you want start the game");
scanf("%d", &automatic);
switch(automatic)
{ case 1:
printf("how do you want customize your car?");
//CAll function to modify the car, pass it the array and the car selection
break;
case 2:
printf("the game will begin into 3 seconds\n");
//Function that runs the movements
break;
case 3:
printf("error please enter a valid number");
break;
}
}
int Modify(struct cars *modpointer, int size, int CarSelect)
{
int i = CarSelect-1;
printf("Please select your modification\n enter 1 for name\n enter 2 for a make\n enter 3 for a number\n enter 4 for a color");
scanf("%d", &CarType);
switch(CarType)
{case 1:
printf("\n please enter driver name");
scanf("%s", &DriverName);
strcpy((modpointer+i)->name, DriverName);
break;
case 2:
printf("please enter the type of car you want");
scanf("s", &CarType);
strcpy ((modpointer+i)->make, CarType);
break;
case 3:
printf("\nplease enter the number you want");
scanf("%d", &number);
strcpy((modpointer+i)->car_number, number);
break;
case 4:
printf("please enter the color you want your car");
scanf("%s", &CarColor);
strcpy((modpointer+i)->color, CarColor);
break;
}
}
//user selects what type of car they want
printf("Please select your car\n enter 1 for a ford\n enter 2 for a chevy\n enter 3 for a kia\n enter 4 for a porsche");
scanf("%d", &CarType);
switch(CarType)
{case 1:
break;
case 2:
break;
case 3:
break;
case 4:
break;
}
//this reads cars.txt file and then prints each statement in the text file
fPointerCars= fopen("cars.txt","r");
char singleline[150];
struct RCinfo Cars [4];
while(!feof(fPointerCars))
{
fscanf(fPointerCars, "%s %s %d %s",&Cars[i].DriverName,&Cars[i].CarModel,&Cars[i].CarNumber,&Cars[i].CarColor);
printf("%s %s %d %s", Cars[i].DriverName, Cars [i]. CarModel, Cars[i]. CarNumber, Cars [i].CarColor);
i++;
}
fclose(fPointerCars);
//Chanfe this main to a function that just reads the cars. txt file
//Pass this function a structure
int readCarsTXT (struct cars *readpointer, int size)
{
// prototype
//int car_pic(struct cars *, int size);
// int auto_or_manual (struct auto_or_manual *, int size);
// declaring variables
int i=0;
int Auto_array_size;
int cararrayindex;
int autoormanualindex;
struct AutoOrManual auto1 [Auto_array_size];
struct cars carlist[CARS_ARRAY_SIZE];
FILE *carFilePointer;
struct cars *carStructPointer;
struct auto1 *autoStructPointer;
// assign pointer to array of structure
carStructPointer = carlist;
carFilePointer = fopen("car.txt", "r");
// check existence of the file
if (carFilePointer == NULL)
{
printf("file does not exist");
}
else
{
// read the file and assign data to array of structures
for (i=0; i < CARS_ARRAY_SIZE; i++)
{
fscanf(carFilePointer,"%s %s %d %s",carStructPointer-> name, carStructPointer-> make, &carStructPointer->car_number, carStructPointer-> color);
// pointer arithmetic. point to the next array element
carStructPointer++;
}
// rest pointer to structure
carStructPointer = carlist;
// call car_pic function
cararrayindex = car_pic (carStructPointer, CARS_ARRAY_SIZE);
// display the chosen car
printf("\nyour car: ");
printf("\t%s %s %d %s\n", carlist[cararrayindex].name, carlist[cararrayindex].make,carlist[cararrayindex].car_number, carlist[cararrayindex].color);
}
close(carFilePointer);
}
// function definition
int car_pic(struct cars *carpointer, int size)
{
int i=0, chosen_car=0;
// display cars
for (i=0; i < size; i++)
{
printf("%d- %-12s %-12s %-3d %-12s\n", i+1, carpointer-> name, carpointer-> make, carpointer->car_number, carpointer-> color);
// pointer arithmetic. point to the next array element
carpointer++;
}
printf("\nEnter the line number for your car ");
scanf("%d", &chosen_car);
// assign array index to chosen_car and return
chosen_car --;
return chosen_car;
}
return 0;
}