Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am having a hard time with compiling this piece of code and I am hoping someon

ID: 3917670 • Letter: I

Question

I am having a hard time with compiling this piece of code and I am hoping someone can spot what is going on with it. Can someone help please.

#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <dirent.h>
#include <assert.h>
#include <string.h>
#include <pthread.h>

typedef struct Room Room;

/*Struct to define the rooms*/
struct Room{
char* room_Name;
char* out_Rooms[6];
char* room_Type;
};

/*array of rooms using pointers, 7 different rooms for program*/
Room rooms[7];

/*Function is to load the rooms from directory*/
void loadTheRooms(char* file_Name, int x){
/*Open the room's file*/
FILE* fd = fopen(file_Name, "a");
assert(fd); //Make sure it opened

/*allocate memory using malloc*/
Room* a = (Room*)malloc(sizeof(Room));

int i = 0;
for(i = 0; i < 6; i++){
  a->out_Rooms[i] = NULL;
}

char buffer[256];

/*read info on rooms*/
char* room_Name = (char*)malloc(sizeof(char) * 256);
fgets(buffer, 256, fd);
buffer[strcspn(buffer, " ")] = 0;
memset(room_Name, '', sizeof(room_Name));
memcpy(room_Name, &buffer[11], 256);
a->room_Name = room_Name;

/*Save information of the rooms by looping through*/
int k = 0;
while(fgets(buffer, 256, fd)){
  buffer[strcspn(buffer, " ")] = 0;
  if((char ) buffer[12] == ':'){
   char* b = (char*)malloc(sizeof(char) * 256);
   memset(b, '', sizeof(b));
   memcpy(b, &buffer[14], 255);
   a->out_Rooms[k] = b;
   k++;
  }
  if((char)buffer[9] == ':'){
   char* b = (char*)malloc(sizeof(char) * 256);
   memset(b, '', sizeof(b));
   memcpy(b, &buffer[11], 255);
   a->room_Type = b;
  }
}

fclose(fd); //closes the files

rooms[i] = a; //rooms added to the array
}

void roomLoad(){
/*Directory with unique PID that is added to the end so need to search for full name*/
struct dirent *dirEnd;
  DIR *direct = opendir(".");

/*make sure the directory open with an assert*/
  assert(direct);

/*create a directory*/
char directory_Name[256];
memset(directory_Name, '', 256);

while(dirEnd = readdir(direct)){ //searches for directory
  char buffer[16]; //Setting buffer to 16 because name is 15 characters long plus ending character
  memcpy(buffer, dirEnd->dir_Name, 256);

  if(directory_Name[0] == '' && strcmp(buffer, "ingramjo.rooms.") == 0){
   memcpy(directory_Name, dirEnd->dir_Name, 256);
  }else{
   /*compares times to see which is the most recent*/
   struct stat timing_1;
   struct stat timing_2;
   if(!(stat(directory_Name, &timing_1) || timing(dirEnd->dir_Name, &timing_2))){
    if(timing_2.st_mtime > timing_1.st_mtime){
     memcpy(directory_Name, dirEnd->dir_Name, 256); //indicates if it found a faster one
    }
   }
  }
}

closedir(direct);

direct = opendir(directory_Name);

int h = 0;
while(dirEnd = readdir(direct)){
  if((strcmp(dirEnd->dir_Name, ".") != 0) && (strcmp(dirEnd->dir_Name, "..") != 0)){
   char filename[256];
   sprintf(filename, "%s/%s", directory_Name, dirEnd->dir_Name);
   roomLoad(filename, h);
   h++;
  }
}

closedir(direct);
}

Room* findBegin(){
/*find the beginning room by searching array*/
int h = 0;
while(strcmp(rooms[h]->room_Type, "Start_Room") != 0 && h < 7){
  h++;
}

returm rooms[h];
}

/*finds and prints possible connections to rooms*/
void connectionOutput(Room* a){
int k = 0;
printf("Here are the connections for this room: ");
while(a->out_Rooms[k] != NULL && k < 6){
//max of 6 connections
  if(a->out_Rooms[k + 1] != NULL && k + 1 < 6){
   printf("%s, ", a->out_Rooms[k]);
  }else{
   printf("%s. ",a->out_Rooms[k]);
  }
k++;
}
}

char* directionsIn(){
char* line_In = malloc(sizeof(char) * 256);
memset(line_In, '', 256);
if(fgets(line_In, 255, stdin)){
  /*deletes line charecters*/
  line_In[strcspn(line_In, " ")] = 0;
}

printf(" "); //done to create a new line and clean formatting

return line_In;
}

/*converts the characters in the second array to integers, then finds the name for each room
* in the rooms array.*/
void pathDisplay(char* travel_Path){
int h = 0;
while(travel_Path[h] != ''){
  printf("%s ", rooms[travel_Path[h] - 48]->room_Name);
  h++;
}
}


void* getTimes(void* nut){
/*Start timing*/
time_t start_Time = time(NULL);
struct tim* z = local_Time(&start_Time);

char buffer[40];
memset(&buffer, '', 40);
strftime(buffer, 40, "%l:%M %p, %A, %B, %d, %Y", z);

/*opens the file to insert date and closes file once done*/
pthread_mutex_lock(nut);
FILE* fd = fopen("recentTime.txt", "y");
fwrite(buffer, 1, 32, fd);
fclose(fd);
pthread_mutex_unlock(nut);
}

void displayTime(pthread_mutex_t* z){
char buffer[40];
memset(&buffer, '', 40);

/*open file and change new lines to bull terminating characters*/
pthread_mutex_lock(z);
FILE* fd = fopen("recentTime.txt", "a");
fgets(buffer, 256, fd);
buffer[strcspn(buffer, " ")] = 0;

printf("%s ", buffer);

fclose(fd);
pthread_mutex_unlock(z);
}

int main(){
/*Loads the rooms and finds the starting room*/
roomLoad();
Room* a = findBegin();

pthread_mutex_t z = PTHREAD_MUTEX_INITIALIZER;

/*start the travel path and saves by storing a room's index in an array.*/
char* travel_Path = malloc(sizeof(char) * 1024); //create memory allocation
memset(travel_Path, '', 1024);
int m = 0;

/*count amount of steps taken counter*/
int walking_It_Out = 0;

/*starts the game and loops through*/
while(1){
  if(strcomp(a->room_Type, "End_Room") != 0){
   printf("Current Room: %s ", a->room_Name); //prints the current location
   connectionOutput(a); //prints the connections to a room
   /*get user input as to where to go next*/
   printf("What room would you like to endter next? >");
   char* command = directionsIn();
  
   if(strcomp(command, "time") == 0){
    pthread_t time_Thread;  
    int results = pthread_create(&time_Thread, NULL, getTimes, &z);
    assert(results == 0);
    results = pthread_join(time_Thread, NULL); //blocks tread until ready
    displayTime(&z);
    m--;
   }else{
   /*Looks through connections for a possible room*/
    int has_Found = 0;
    int k = 0;
    while(a->out_Rooms[k] != NULL){
     if(strcmp(command, a->out_Rooms[k]) == 0){
      has_Found = 1;
      break;
     }
     k++;
    }
    
    if(!has_Found){
     printf("Sorry that is an invalid command. Try Again. ");
    }else{
     /*find the room asked for*/
     k = 0;
     while(strcmp(rooms[k]->room_Name, command) != 0){
      k++;
     }
     a = rooms[k];
   
     int i = 0;
     while(strcmp(rooms[i]->room_Name, a->room_Name) != 0){
      i++;
     }
     travel_Path[m] = i + 48;
     m++;

     /*Increment steps that have been taken*/
     walking_It_Out++;
    }
   }
   free(command); //frees of memory used for the command
  }else{
   /*this is if the end room has be located*/
   printf("Congrats! You have found the end room. ");

   printf("It took you %d steps to find the end rooom. It should have taken ", walking_It_Out);
   printf(" ");
   
   /*Pringt the steps in the route out*/
   pathDisplay(travel_Path);

   /*exit our of the program*/
   pthread_mutex_destroy(&z);
   break;
  }
}   

return 0;
}

Explanation / Answer

I have removed all the compilation errors. Please find the code below.

CODE

================

#include <stdlib.h>

#include <stdio.h>

#include <time.h>

#include <sys/stat.h>

#include <fcntl.h>

#include <unistd.h>

#include <sys/types.h>

#include <dirent.h>

#include <assert.h>

#include <string.h>

#include <pthread.h>

typedef struct Room Room;

/*Struct to define the rooms*/

struct Room{

char* room_Name;

char* out_Rooms[6];

char* room_Type;

};

/*array of rooms using pointers, 7 different rooms for program*/

Room rooms[7];

/*Function is to load the rooms from directory*/

void loadTheRooms(char* file_Name, int x){

/*Open the room's file*/

FILE* fd = fopen(file_Name, "a");

assert(fd); //Make sure it opened

/*allocate memory using malloc*/

Room* a = (Room*)malloc(sizeof(Room));

int i = 0;

for(i = 0; i < 6; i++){

a->out_Rooms[i] = NULL;

}

char buffer[256];

/*read info on rooms*/

char* room_Name = (char*)malloc(sizeof(char) * 256);

fgets(buffer, 256, fd);

buffer[strcspn(buffer, " ")] = 0;

memset(room_Name, '', sizeof(room_Name));

memcpy(room_Name, &buffer[11], 256);

a->room_Name = room_Name;

/*Save information of the rooms by looping through*/

int k = 0;

while(fgets(buffer, 256, fd)){

buffer[strcspn(buffer, " ")] = 0;

if((char ) buffer[12] == ':'){

char* b = (char*)malloc(sizeof(char) * 256);

memset(b, '', sizeof(b));

memcpy(b, &buffer[14], 255);

a->out_Rooms[k] = b;

k++;

}

if((char)buffer[9] == ':'){

char* b = (char*)malloc(sizeof(char) * 256);

memset(b, '', sizeof(b));

memcpy(b, &buffer[11], 255);

a->room_Type = b;

}

}

fclose(fd); //closes the files

rooms[i] = *a; //rooms added to the array

}

void roomLoad(){

/*Directory with unique PID that is added to the end so need to search for full name*/

struct dirent *dirEnd;

DIR *direct = opendir(".");

/*make sure the directory open with an assert*/

assert(direct);

/*create a directory*/

char directory_Name[256];

memset(directory_Name, '', 256);

while(dirEnd = readdir(direct)){ //searches for directory

char buffer[16]; //Setting buffer to 16 because name is 15 characters long plus ending character

memcpy(buffer, dirEnd->d_name, 256);

if(directory_Name[0] == '' && strcmp(buffer, "ingramjo.rooms.") == 0){

memcpy(directory_Name, dirEnd->d_name, 256);

}else{

/*compares times to see which is the most recent*/

struct stat timing_1;

struct stat timing_2;

if(!(stat(directory_Name, &timing_1) || stat(dirEnd->d_name, &timing_2))){

if(timing_2.st_mtime > timing_1.st_mtime){

memcpy(directory_Name, dirEnd->d_name, 256); //indicates if it found a faster one

}

}

}

}

closedir(direct);

direct = opendir(directory_Name);

int h = 0;

while(dirEnd = readdir(direct)){

if((strcmp(dirEnd->d_name, ".") != 0) && (strcmp(dirEnd->d_name, "..") != 0)){

char filename[256];

sprintf(filename, "%s/%s", directory_Name, dirEnd->d_name);

roomLoad(filename, h);

h++;

}

}

closedir(direct);

}

Room* findBegin(){

/*find the beginning room by searching array*/

int h = 0;

while(strcmp(rooms[h].room_Type, "Start_Room") != 0 && h < 7){

h++;

}

return &rooms[h];

}

/*finds and prints possible connections to rooms*/

void connectionOutput(Room* a){

int k = 0;

printf("Here are the connections for this room: ");

while(a->out_Rooms[k] != NULL && k < 6){

//max of 6 connections

if(a->out_Rooms[k + 1] != NULL && k + 1 < 6){

printf("%s, ", a->out_Rooms[k]);

}else{

printf("%s. ",a->out_Rooms[k]);

}

k++;

}

}

char* directionsIn(){

char* line_In = malloc(sizeof(char) * 256);

memset(line_In, '', 256);

if(fgets(line_In, 255, stdin)){

/*deletes line charecters*/

line_In[strcspn(line_In, " ")] = 0;

}

printf(" "); //done to create a new line and clean formatting

return line_In;

}

/*converts the characters in the second array to integers, then finds the name for each room

* in the rooms array.*/

void pathDisplay(char* travel_Path){

int h = 0;

while(travel_Path[h] != ''){

printf("%s ", rooms[travel_Path[h] - 48].room_Name);

h++;

}

}

void* getTimes(void* nut){

/*Start timing*/

time_t start_Time = time(NULL);

const struct tm* z = localtime(&start_Time);

char buffer[40];

memset(&buffer, '', 40);

strftime(buffer, 40, "%l:%M %p, %A, %B, %d, %Y", z);

/*opens the file to insert date and closes file once done*/

pthread_mutex_lock(nut);

FILE* fd = fopen("recentTime.txt", "y");

fwrite(buffer, 1, 32, fd);

fclose(fd);

pthread_mutex_unlock(nut);

}

void displayTime(pthread_mutex_t* z){

char buffer[40];

memset(&buffer, '', 40);

/*open file and change new lines to bull terminating characters*/

pthread_mutex_lock(z);

FILE* fd = fopen("recentTime.txt", "a");

fgets(buffer, 256, fd);

buffer[strcspn(buffer, " ")] = 0;

printf("%s ", buffer);

fclose(fd);

pthread_mutex_unlock(z);

}

int main(){

/*Loads the rooms and finds the starting room*/

roomLoad();

Room* a = findBegin();

pthread_mutex_t z = PTHREAD_MUTEX_INITIALIZER;

/*start the travel path and saves by storing a room's index in an array.*/

char* travel_Path = malloc(sizeof(char) * 1024); //create memory allocation

memset(travel_Path, '', 1024);

int m = 0;

/*count amount of steps taken counter*/

int walking_It_Out = 0;

/*starts the game and loops through*/

while(1){

if(strcmp(a->room_Type, "End_Room") != 0){

printf("Current Room: %s ", a->room_Name); //prints the current location

connectionOutput(a); //prints the connections to a room

/*get user input as to where to go next*/

printf("What room would you like to endter next? >");

char* command = directionsIn();

if(strcmp(command, "time") == 0){

pthread_t time_Thread;

int results = pthread_create(&time_Thread, NULL, getTimes, &z);

assert(results == 0);

results = pthread_join(time_Thread, NULL); //blocks tread until ready

displayTime(&z);

m--;

}else{

/*Looks through connections for a possible room*/

int has_Found = 0;

int k = 0;

while(a->out_Rooms[k] != NULL){

if(strcmp(command, a->out_Rooms[k]) == 0){

has_Found = 1;

break;

}

k++;

}

if(!has_Found){

printf("Sorry that is an invalid command. Try Again. ");

}else{

/*find the room asked for*/

k = 0;

while(strcmp(rooms[k].room_Name, command) != 0){

k++;

}

a = &rooms[k];

int i = 0;

while(strcmp(rooms[i].room_Name, a->room_Name) != 0){

i++;

}

travel_Path[m] = i + 48;

m++;

/*Increment steps that have been taken*/

walking_It_Out++;

}

}

free(command); //frees of memory used for the command

}else{

/*this is if the end room has be located*/

printf("Congrats! You have found the end room. ");

printf("It took you %d steps to find the end rooom. It should have taken ", walking_It_Out);

printf(" ");

/*Pringt the steps in the route out*/

pathDisplay(travel_Path);

/*exit our of the program*/

pthread_mutex_destroy(&z);

break;

}

}

return 0;

}