dimanche 22 avril 2018

"Segmentation fault 11" crashing in for loop

I'm super new to C programming, only taking it for a required course in uni and finally getting done with my final project this week.

Problem is every time I run this code I get an error message that says "segmentation fault: 11" and I'm not sure what that means.

The code is supposed to run two-player race. Each player has a pre-determined "speed modifier", aka a number from 0-9. A random number from 1-10 is generated; if the speed modifier + the random number doesn't exceed 10, then the speed modifier is added and the car moves that total amount of "spaces". The whole race track is 90 "spaces".

Every time I run this code it works somewhat fine (more on that later) for one or two iterations, then gives out that error message. I normally don't ask for homework help online but I'm honestly so confused. Any help at all would be super appreciated.

Here is the relevant code:

Race (main) function:

int race(struct car cars[4], int mode)
{
/* Define variables. */
int endgame, i, x, y, first=-1, second=-1, randnum, spaces[]={};
char input[100];
/* Declare pointer. */
int *spacesptr=spaces;

for (i=0; i<mode; i++)
{
    /* Array spaces will keep track of how many spaces a car has moved. Start each car at 0. */
    spaces[i]=0;
}

/* Clear screen before race. */
system("cls");

/* Print message to indicate race has started. */
printf("\n3...\n2...\n1...\nGO!\n\n");

/* Open do while loop to keep race running until it is over. */
do
{
    /* Conditions for two player mode. */
    if (mode==2)
    {
        /* Run the next block of code once for each player. */
        for (i=0; i<mode; i++)
        {
            /* Generate random integer from 1-10 to determine how many spaces the car moves. */
            x=10, y=1;
            randnum=(getrandom(x, y));
            spaces[i]=spaces[i]+randnum;

            /* Call function speedmod to determine if speedmodifier should be added. */
            speedmod(cars, spaces, randnum);

            /* Rank players. */
            if (spaces[i]>89)
            {
                if (first==-1)
                {
                    first=i;
                }

                else if (second==-1)
                {
                    second=i;
                }

            }

        }
    }

    ...

    /* Call function displayrace to display the race. */
    displayrace(cars, spaces, mode);

    /* Call function endgame to determine if the race is still going. */
    endgame=fendgame(mode, spaces);

    if (endgame==0)
    {
        /* Ask for player input. */
        printf("Enter any key to continue the race:\n");
        scanf("%s", input);

        /* Clear screen before loop restarts. */
        system("cls");
    }

} while (endgame==0);
}

Random number function:

int getrandom(int x, int y)
{
/* Define variable. */
int randnum;

/* Use time seed. */
srand(time(0));

/* Generate random numbers. */
randnum=(rand()+y) % (x+1);

return randnum;
}

Add speed modifier function:

void speedmod(struct car cars[4], int spaces [], int go)
{
/* Declare pointer. */
int *spacesptr=spaces;

/* If the number of spaces plus the speed modifier is less than or equal to ten... */
if (spaces[go]+cars[go].speedmod<=10)
{
    /* ...add the speed modifier to the number of spaces moved. */
    spaces[go]=spaces[go]+cars[go].speedmod;
}
}

Display race function:

void displayrace(struct car cars[4], int spaces[], int mode)
{
/* Define variables. */
int i, j;

/* Declare pointers. */
int *spacesptr=spaces;
struct car *carsptr=cars;

/* Open for loop. */
for (i=0; i<mode; i++)
{
    /* Print racecar number. */
    printf("#%d\t", cars[i].carnumber);

    /* For every space the car has moved... */
    for (j=0; j<spaces[i]; j++)
    {
        if (j<=90)
        {
            /* ...print one asterisk. */
            printf("*");
        }
    }

    /* New line. */
    printf("\n");
}
}

End game function:

int fendgame(int mode, int spaces[])
{
/* Define variables. */
int racers=0, endgame=0, i;
/* Declare pointer. */
int *spacesptr=spaces;

/* Open for loop. */
for (i=0; i<mode; i++)
{
    /* If any of the racers have not yet crossed the finish line (90 spaces)... */
    if (spaces[i]<=90)
    {
        /* ...then add to the number of racers still in the game. */
        racers++;
    }
}

/* If all the racers have crossed the finish line... */
if (racers==0)
{
    /* ...then end the game. */
    endgame=1;
}

return endgame;
}

Now to be more specific on the issue...it's messing up somewhere on the "for" loop in the race function. For some reason it doesn't actually go through each iteration of "i", specifically in this line:

spaces[i]=spaces[i]+randnum;

I know this because I put in printf statements to display the value of spaces[i] before and after; the first iteration of i works fine...but the second iteration actually uses spaces[i-1] instead and adds the randnum to that value?

And then, like I said, it crashes after one or two times of this...:-(

I know it's a lot but I'm hoping someone more experienced than I could spot the error(s) in this code for me! Please help!




Aucun commentaire:

Enregistrer un commentaire