0

When trying to run my program on Linux I get a segmentation fault. I am a novice with C. In doing research, I figure the reason is probably a loop running beyond the last index in one of the arrays and accessing memory it shouldn't. Any hints?


Program overview: takes in a file in a certain format and determines from that file that best place to eat at based on the voter's three top favorites and least favorite.

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

int outputToFile (int winner, char **restaurantList, char **voterNameList, int numberOfVoters, int **voterFavorites) {
int index;
FILE *output;

output = fopen ("output_1", "w"); //write mode

fprintf(output, "%s\n", restaurantList[winner]); // write out the winning restuarant
fprintf(output, "Happy:\n");
for (index = 0; index < numberOfVoters ; index++) {              // write out the happy persons
    if (voterFavorites[index][1] == winner || voterFavorites[index][2] == winner || voterFavorites[index][3] == winner) {
        fprintf(output, "%s\n", voterNameList[index]);
    }   
}

fprintf (output, "Sad:\n");
for (index = 0; index < numberOfVoters ; index++) {              // write out the sad persons
    if (voterFavorites[index][4] == winner) {
        fprintf(output, "%s\n", voterNameList[index]);
    }
}

fclose (output);
}

int countScore (int **voterFavorites, int tallyCard[], int numberOfVoters, int numberOfRestaurants) {
int index;

for (index = 0; index < numberOfRestaurants; index++ ) {
    tallyCard[index] = 0;
}

for (index = 0; index < numberOfVoters; index++) {
    if (voterFavorites[index][1] != -999) {
        tallyCard[voterFavorites[index][1]] = tallyCard[voterFavorites[index][1]] + 1;
    } else if (voterFavorites[index][2] != -999) {
        tallyCard[voterFavorites[index][2]] = tallyCard[voterFavorites[index][2]] + 1;
    } else if (voterFavorites[index][3] != -999) {
        tallyCard[voterFavorites[index][3]] = tallyCard[voterFavorites[index][3]] + 1;
    }

    if (voterFavorites[index][4] != -999) {
        tallyCard[voterFavorites[index][4]] = tallyCard[voterFavorites[index][4]] - 1;
    }
}
}

int determineWinner( int tallyCard[], int maxVotes, int **voterFavorites) {
int x, y, max = 0, min = 999, maxIndex, percent; 

for ( x = 0 ; x < 20 ; x++ ) { 
    if (tallyCard[x] > max) {
        maxIndex = x;
        max = tallyCard[x];
    } 
    if (tallyCard[x] < min) {
        if (tallyCard[x] != -999) {
            min = tallyCard[x];
        }
    }
}

percent = max/maxVotes * 100;
if ( percent >= 50) {
    return maxIndex;
} else {
    for(x = 0; x < maxVotes ; x++) {
        for (y = 0; y < 4; y++) {
            if (voterFavorites[x][y] == min) {
                voterFavorites[x][y] == -999;
            }
        } 
    }
    return -444;
}
}

void inputFromFile (void) {
int index, numberOfRestaurants, numberOfVoters, winner;
char *nor, *nov, **restaurantList = malloc(20 * sizeof(char*));;
char **voterNameList = malloc(100 * sizeof(char*));
int **voterFavorites = (int**) calloc(100, sizeof(int*)), tallyCard[20];
int *fav1, *fav2, *fav3, *notFav;
FILE *input;

for ( index = 0; index < 100; index++ ) {
    voterFavorites[index] = (int*) calloc(4, sizeof(int));
    voterNameList[index] = malloc(26 * sizeof(char));
}
for ( index = 0 ; index < 20; index++ ) {
    restaurantList[index] = malloc(51 * sizeof(char));
}

input = fopen ("input_1", "r"); // !!! need to check if this will give an error. Refer to slide set 2-48
if( input == NULL ) { 
    perror("Error while opening the file.\n"); 
    exit(1);
} 

//get names of restaurants
fgets(nor, 51, input);
numberOfRestaurants = atoi(nor); //resolve char * to int
for ( index = 0 ; index < numberOfRestaurants ; index++ ) {
    fscanf(input, "%s", restaurantList[index]);
}

//get info lines and names of voters and initial values
fgets(nov, 101, input);
numberOfVoters = atoi(nov); //resolve char * to int
for ( index = 0 ; index < numberOfVoters ; index++ ) {

    fscanf(input, "%s %i %i %i %i", voterNameList[index], fav1, fav2, fav3, notFav);
    voterFavorites[index][1] = *fav1-1;
    voterFavorites[index][2] = *fav2-1;
    voterFavorites[index][3] = *fav3-1;
    voterFavorites[index][4] = *notFav-1;
}

//count total score for resturants
countScore (voterFavorites, tallyCard, numberOfVoters, numberOfRestaurants);
winner = determineWinner(tallyCard, numberOfVoters, voterFavorites); // !!! probably need to look into these max votes
while (winner < 0) {
    countScore(voterFavorites, tallyCard, numberOfVoters, numberOfRestaurants);
    winner = determineWinner(tallyCard, numberOfVoters, voterFavorites);
}

outputToFile(winner, restaurantList, voterNameList, numberOfVoters, voterFavorites);
}

int main (void) 
{
 inputFromFile();
return 0;   
}

I've been working on this for a while but I'm sure this could be cleaned up a bit. Sorry about that!

2
  • 1
    Done any debugging yourself? e.g. narrow down to WHERE in the code the error occurs? We're happy to help, but we are NOT going to read through a wall of code and try to GUESS at which microscopic small section of that wall of code is causing the problem. Commented Feb 3, 2014 at 19:11
  • @ Marc B I'm not expecting you to. I wish I had time to go through and learn all about debugging and how to use it right now. My knowledge of gcc and adb are minimal at best. You're welcome to help me learn by posting a tutorial on how to debug properly. Commented Feb 3, 2014 at 21:12

1 Answer 1

1
char *nor, *nov, **restaurantList = malloc(20 * sizeof(char*));;

/* ... */

fgets(nor, 51, input);

nor object is not initialized.

Sign up to request clarification or add additional context in comments.

1 Comment

Why would these (nor and nov) need initialized? Should have mentioned that they are certain to have a character that can be cast to an int there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.