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

My C program is creating a segmentation fault, could you help me fix it? I\'ve i

ID: 3915463 • Letter: M

Question

My C program is creating a segmentation fault, could you help me fix it? I've included what the output should be

Sample Output:

$ ./a.out

Error. Usage: ./a.out <filename>

$ ./a.out sample.txt

1. Find the number of zip codes in a state

2. Find a zip code

3. Exit

> 1

Enter the state: MO

The state of MO has 153 zip codes in the file.

1. Find the number of zip codes in a state

2. Find a zip code

3. Exit

> 11

Error – not a valid option. Try again.

1. Find the number of zip codes in a state

2. Find a zip code

3. Exit

> 2

Enter the zip code you want to find: 444

No results found for zip code 444

1. Find the number of zip codes in a state

2. Find a zip code

3. Exit

> 2

Enter the zip code you want to find: 57349

Result found for zip code 57349:

City: Howard

State: SD

1. Find the number of zip codes in a state

2. Find a zip code

3. Exit

> 3

Program Terminate

CODE:

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#define MAXCITYNAME 30

#define MAXLINELENGTH 50

typedef struct n_

{

   int zipCode;//A zip code that exists in the given city/state

char* city; //Will point to a city name

char state[3]; //A state abbreviation. Note that we need

//room for the NULL terminator!

   struct n_* left; //connections to other nodes

   struct n_* right;

}Node;

Node* importTree(char* filename);

Node* addNode(Node* root, Node* newNode);

int findStateCount(Node* root, char* state);

Node* findZipCode(Node* root, int zipCode);

void freeTree(Node* root);

void printMenu();

int main(int argc, char** argv)

{

Node* nodeIndex = NULL;

Node* zipCode = NULL;

   if(argc != 2){

   printf("Usage: %s <filename> ", argv[0]);

   exit(1);

}

nodeIndex = importTree(argv[1] );

int selection;

printMenu();

scanf("%d", &selection);

while(selection <1 || selection > 3)

{

printf("Invalid selection, choose again: >");

scanf("%d", &selection);

}

while(selection !=3)

{

while(selection < 1 || selection >3)

{

printf("Invalid selection, choose again: >");

scanf("%d", &selection);

}

switch(selection)

{

case 1:

{

char state[3];

printf("Enter the state you want to search for: ");

scanf("%s", state );

printf("The state has %d results in the sample ", findStateCount( nodeIndex , state ));

break;

}

case 2:

{

   int zip = 0;

   printf("Enter the zip code you want to find: ");

   scanf("%d", &zip );

   zipCode = findZipCode( nodeIndex , zip );

if( zipCode == NULL )

{

printf("No results found for %d ", zip);

break;

}

else

{

printf("Result found for %d: City:%s State:%s ", zip, zipCode->city, zipCode->state);

break;

}

}

}

printMenu();

scanf("%d", &selection );

}

freeTree(nodeIndex);

return 0;

}

  

/**********************************************************************************************************************/

Node* addNode(Node* root, Node* newNode)

{

if(newNode == NULL){

printf(" NULL pointer passed to addNode()");

exit(1);

}

if(root == NULL) {

return newNode;

} else {

// large zip codes on the right

   if(newNode->zipCode > root->zipCode) {

   root->right = addNode(root->right, newNode);

}

// lil zip codes on the left

   if(newNode->zipCode < root->zipCode) {

   root->left = addNode(root->left, newNode);

   }

   }

   return root;

}

//**********************************************************************************************************************

int findStateCount(Node* root, char* state)

{

// if the tree is empty, theres no instances of the state

   if(root == NULL) {

   return 0;

   } else {

   if(strcmp(root->state, state) == 0) {

   return 1 + findStateCount(root->left, state) + findStateCount(root->right, state);

   }

   }

   return findStateCount(root->left, state) + findStateCount(root->right, state);

}

//************************************************************************************************************************

Node* findZipCode(Node* root, int zip)

{

if(root == NULL)

{

return NULL;

}

  

else if(root->zipCode == zip)

return root;

else if(zip < root -> zipCode)

return findZipCode(root->left, zip);

else

return findZipCode(root -> right, zip);

}

  

  

//***********************************************************************************************************************

void freeTree(Node* root)

{

if(root == NULL) //Base case//

{

free(root);

   return;

}

   freeTree( root->left );

   freeTree( root->right );

   free( root->city );

   free( root );

}

//************************************************************************************************************************

Node* importTree(char* filename)

{

   Node* root = NULL; //Initializes value for root //

FILE* fp = fopen("input.txt", "r"); /* opens input file, fp = the address of file */

   if(!fp) /* error check for if the file is not opened properly or not at all */

{

   printf("Error opening file. ");

   return NULL;

   }

while(!feof(fp)) /* while loop breaks when feof reaches the end of the file */

{

   Node* new =(Node*)malloc(sizeof(Node)); /* allocates memory for a new node */

if(!new) /* error check for if malloc fails */

{

printf("Failed to allocate memory. Ending read. ");

exit(1);

   }

new->city =malloc(sizeof(char)*MAXCITYNAME);

if(!(new->city))

   {

   printf("Failed to allocate memory. Ending read. ");

   exit(1);

   }

new->left = NULL;

new->right = NULL;

char* line = malloc(sizeof(char)*MAXLINELENGTH);

if(!line)

{

   printf("Failed to allocate memory. Ending read. ");

   exit(1);

   }

if(fgets(line, MAXLINELENGTH, fp) == NULL)

{

   if(!feof(fp))

   {

   printf("File reading ended prematurely. Check for errors in the file. ");

   exit(1);

   }

free(new->city);

free(line);

free(new);

fclose(fp);

   break;

}

   char* tmp = strtok(line, ",");

   new->zipCode = atoi(tmp);

   tmp = strtok(NULL, ",");

   strcpy(new->city, tmp);

   new->city[strlen(tmp)+1] = '';

   tmp = strtok(NULL, " ");

   strcpy(new->state, tmp);

   new->state[2] = '';

   root = addNode(root, new);

   if(!root)

   {

   printf("Root of tree is still NULL! Ending read. ");

   exit(1);

   }

   free(line);

}

return root;

}

//**************************************************************************************************************************

void printMenu()

{

printf("1: Find number of Zip codes in a state 2:Find zip code 3:exit ");

}

  

  

Explanation / Answer

Hi.. I have tried to execute the above code in www.onlinegdb.com. the code has executed successfull and i am able to find the output as expected. I am thinking like the problem is with ur c compiler you are using. Please check it once.

Main.c

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#define MAXCITYNAME 30

#define MAXLINELENGTH 50

typedef struct n_

{

int zipCode;//A zip code that exists in the given city/state

char* city; //Will point to a city name

char state[3]; //A state abbreviation. Note that we need

//room for the NULL terminator!

struct n_* left; //connections to other nodes

struct n_* right;

}Node;

Node* importTree(char* filename);

Node* addNode(Node* root, Node* newNode);

int findStateCount(Node* root, char* state);

Node* findZipCode(Node* root, int zipCode);

void freeTree(Node* root);

void printMenu();

int main(int argc, char** argv)

{

Node* nodeIndex = NULL;

Node* zipCode = NULL;

if(argc != 2){

printf("Usage: %s <filename> ", argv[0]);

exit(1);

}

nodeIndex = importTree(argv[1] );

int selection;

printMenu();

scanf("%d", &selection);

while(selection <1 || selection > 3)

{

printf("Invalid selection, choose again: >");

scanf("%d", &selection);

}

while(selection !=3)

{

while(selection < 1 || selection >3)

{

printf("Invalid selection, choose again: >");

scanf("%d", &selection);

}

switch(selection)

{

case 1:

{

char state[3];

printf("Enter the state you want to search for: ");

scanf("%s", state );

printf("The state has %d results in the sample ", findStateCount( nodeIndex , state ));

break;

}

case 2:

{

int zip = 0;

printf("Enter the zip code you want to find: ");

scanf("%d", &zip );

zipCode = findZipCode( nodeIndex , zip );

if( zipCode == NULL )

{

printf("No results found for %d ", zip);

break;

}

else

{

printf("Result found for %d: City:%s State:%s ", zip, zipCode->city, zipCode->state);

break;

}

}

}

printMenu();

scanf("%d", &selection );

}

freeTree(nodeIndex);

return 0;

}

  

/**********************************************************************************************************************/

Node* addNode(Node* root, Node* newNode)

{

if(newNode == NULL){

printf(" NULL pointer passed to addNode()");

exit(1);

}

if(root == NULL) {

return newNode;

} else {

// large zip codes on the right

if(newNode->zipCode > root->zipCode) {

root->right = addNode(root->right, newNode);

}

// lil zip codes on the left

if(newNode->zipCode < root->zipCode) {

root->left = addNode(root->left, newNode);

}

}

return root;

}

//**********************************************************************************************************************

int findStateCount(Node* root, char* state)

{

// if the tree is empty, theres no instances of the state

if(root == NULL) {

return 0;

} else {

if(strcmp(root->state, state) == 0) {

return 1 + findStateCount(root->left, state) + findStateCount(root->right, state);

}

}

return findStateCount(root->left, state) + findStateCount(root->right, state);

}

//************************************************************************************************************************

Node* findZipCode(Node* root, int zip)

{

if(root == NULL)

{

return NULL;

}

  

else if(root->zipCode == zip)

return root;

else if(zip < root -> zipCode)

return findZipCode(root->left, zip);

else

return findZipCode(root -> right, zip);

}

  

  

//***********************************************************************************************************************

void freeTree(Node* root)

{

if(root == NULL) //Base case//

{

free(root);

return;

}

freeTree( root->left );

freeTree( root->right );

free( root->city );

free( root );

}

//************************************************************************************************************************

Node* importTree(char* filename)

{

Node* root = NULL; //Initializes value for root //

FILE* fp = fopen("input.txt", "r"); /* opens input file, fp = the address of file */

if(!fp) /* error check for if the file is not opened properly or not at all */

{

printf("Error opening file. ");

return NULL;

}

while(!feof(fp)) /* while loop breaks when feof reaches the end of the file */

{

Node* new =(Node*)malloc(sizeof(Node)); /* allocates memory for a new node */

if(!new) /* error check for if malloc fails */

{

printf("Failed to allocate memory. Ending read. ");

exit(1);

}

new->city =malloc(sizeof(char)*MAXCITYNAME);

if(!(new->city))

{

printf("Failed to allocate memory. Ending read. ");

exit(1);

}

new->left = NULL;

new->right = NULL;

char* line = malloc(sizeof(char)*MAXLINELENGTH);

if(!line)

{

printf("Failed to allocate memory. Ending read. ");

exit(1);

}

if(fgets(line, MAXLINELENGTH, fp) == NULL)

{

if(!feof(fp))

{

printf("File reading ended prematurely. Check for errors in the file. ");

exit(1);

}

free(new->city);

free(line);

free(new);

fclose(fp);

break;

}

char* tmp = strtok(line, ",");

new->zipCode = atoi(tmp);

tmp = strtok(NULL, ",");

strcpy(new->city, tmp);

new->city[strlen(tmp)+1] = '';

tmp = strtok(NULL, " ");

strcpy(new->state, tmp);

new->state[2] = '';

root = addNode(root, new);

if(!root)

{

printf("Root of tree is still NULL! Ending read. ");

exit(1);

}

free(line);

}

return root;

}

//**************************************************************************************************************************

void printMenu()

{

printf("1: Find number of Zip codes in a state 2:Find zip code 3:exit ");

}

Output:

1: Find number of Zip codes in a state   
2:Find zip code
3:exit   
1
Enter the state you want to search for:
22323   
The state has 0 results in the sample
1: Find number of Zip codes in a state   
2:Find zip code
3:exit   
2
Enter the zip code you want to find:   
56586   
No results found for 56586  
1: Find number of Zip codes in a state   
2:Find zip code
3:exit   
3

Please test it and let me know any issues. Thank you. All the best.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote