Segmentation Fault

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

#define BUFFER 64

#define PLAYINGDAYS 20  //Number of games recorded

void splash ( void );
void end ( void );
int scheduleInformation ( char *array [], char *input, int cmparray [], int n );
void print_schedule ( char *date [], char *opponent [], char *time [], char *location [],
                                                             char *home [], int cmparray [], int n );

int main ( void )
{   
    //The following data is the Jacksonville Jaguars schedule
    //************************************************************************//
    
    char *date [] = {"Aug. 17","Aug. 22","Aug. 27","Sept. 3","Sep. 13","Sep. 20",
    "Sep. 27","Oct. 4","Oct. 11","Oct. 18","Nov. 1","Nov. 8","Nov. 15","Nov. 22",
    "Nov. 29","Dec. 6","Dec. 13","Dec. 17","Dec. 27","Jan. 3"};
    
    char *time [] = {"7:30pm","7:30pm","7:00pm","7:30pm","1:00pm",
    "1:00pm","1:00pm","1:00pm","4:15pm","1:00pm","4:05pm","1:00pm","1:00pm","1:00pm",
    "4:05pm","1:00pm","1:00pm","8:20pm","1:00pm","1:00pm"};
    
    char *opponent [] = {"Miami Dolphins","Tampa Bay Buccaneers","Philadelphia Eagles",
    "Washington Redskins","Indianapolis Colts","Arizona Cardinals","Houston Texans",
    "Tennessee Titans","Seattle Seahawks","St. Louis Rams","Tennessee Titans",
    "Kansas City Chiefs","N.Y. Jets","Buffalo Bills","San Francisco 49ers",
    "Houston Texans","Miami Dolphins","Indianapolis Colts","New England Patriots",
    "Cleveland Browns"};

    char *home [] = {"Away","Home","Away","Home","Away","Home","Away","Home",
    "Away","Home","Away","Home","Away","Home","Away","Home","Home","Home",
    "Away","Away"};
    
    char *location [] = {"Dolphin Stadium","Jax Municipal Stadium",
    "Lincoln Financial Field","Jax Municipal Stadium","Lucas Oil Field",
    "Jax Municipal Stadium","Reliant Stadium","Jax Municipal Stadium",
    "Qwest Field","Jax Municipal Stadium","LP Field","Jax Municipal Stadium",
    "Meadowlands","Jax Municipal Stadium","Candlestick Park","Jax Municipal Stadium",
    "Jax Municipal Stadium","Jax Municipal Stadium","Gillette Stadium","Cleveland Browns Stadium"};
    
    //************************************************************************//
    
    int j;
    int cmpedarray [BUFFER];                 // Will store the location of matches
    char searchBy [BUFFER], option [BUFFER];
    
    splash ();
    
    printf ( "Search by [d]ate, [o]pponent, or [l]ocation: " );
    fgets ( searchBy, BUFFER, stdin );
    searchBy[strlen(searchBy) - 1] = '\0';
    printf ( "Continue Search: " );
    fgets ( option, BUFFER, stdin );
    option[strlen(option) - 1] = '\0';
    
    if ( *searchBy == 'd' )
    {
         j = scheduleInformation ( date, option, cmpedarray, PLAYINGDAYS );
         print_schedule ( date, opponent, time, location, home, cmpedarray, j );
    }
    
    else if ( *searchBy == 'o' )
    {
         j = scheduleInformation ( opponent, option, cmpedarray, PLAYINGDAYS );
         print_schedule ( date, opponent, time, location, home, cmpedarray, j );
    }
    
    else if ( *searchBy == 'l' )
    {
         j = scheduleInformation ( location, option, cmpedarray, PLAYINGDAYS );
         print_schedule ( date, opponent, time, location, home, cmpedarray, j );
    }
    
    system ( "PAUSE" );
    
    return 0;
}

void splash ( void )
{
     printf ( "This program accepts user options to display schedule\ninformation " );
     printf ( "based on date, opponent, or location.\n\n" );
     printf ( "Enter option in format.\n\n" );
     printf ( "Date Example: Aug. 17\n" );
     printf ( "Opponent Ex: 'San Francisco 49ers' 'San Francisco' or '49ers'\n" );
     printf ( "Location Ex: 'Jax Municipal Stadium'\n\n" );
     
     return ;
}

int scheduleInformation ( char *array [], char *input, int cmparray [], int n )
{
     int i, k = 0;
     char *part, *parta;
     char spare [BUFFER];
     
     for ( i = 0; i < n; i++ )
     {                                         // Ex: San Francisco 49ers
         strcpy ( spare, array [i] );
         part = strtok ( spare, " \0" );       // take first part
         strcpy ( parta, part );
         part = strtok ( NULL, " \0" );        // parta = San
         strcat ( parta, " " );
         strcat ( parta, part );               // parta = San Francisco
         part = strtok ( NULL, "\0" );         // part = 49ers
         
         if ( strcmp( array [i], input ) == 0 )
         {
              cmparray [k] = i;
              k++;
         }
         
         /*
           Problem in next if statement
         */
         
         // Going to compare parts////////////////////////////////////////
         
         else if ( strcmp ( parta, input ) == 0 || strcmp ( part, input ) == 0 )
         {
              cmparray [k] = i;
              k++;
         }
         ///////////////////////////////////////////////////////////////
         
     }
     return k;    //k is the number of elements in cmparray
}

void print_schedule ( char *date [], char *opponent [], char *time [], char *location [],
                                                             char *home [], int cmparray [], int n )
{
     int i, j;
     
     printf ( "\n---------------------------------------\n\n" );
     for ( i = 0; i < n; i++ )
     {
         j = cmparray [i];
         printf ( "Date: %s ", date [j] );
         printf ( "@ %s \n", time [j] );
         printf ( "%s at %s against %s\n\n", home [j], location [j], opponent [j] );
     }         
     printf ( "---------------------------------------\n" );
                                                             
     return ;
}


In scheduleInformation line 125 I was expecting a comparison but continue to get a Segmentation Fault.
Last edited on
Fooey. There's so much that could possibly go wrong here...
On line 107, parta is still pointing to nowhere.
I'm here to learn, not the best professor I have

Is parta not pointing to anything because its a pointer?
I change it so that parta is an array, same with other half yet, it still gives me a segmentation fault.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
int scheduleInformation ( char *array [], char *input, int cmparray [], int n )
    {
         int i, k = 0;
         char *spare;
         char whole [BUFFER], parta [BUFFER], partb [BUFFER];
         
         for ( i = 0; i < n; i++ )
         {                                         // Ex: San Francisco 49ers
             strcpy ( whole, array [i] );
             spare = strtok ( whole, " \0" );       // take first part
             strcpy ( parta, spare );
             spare = strtok ( NULL, " \0" );        // parta = San
             strcat ( parta, " " );
             strcat ( parta, spare );               // parta = San Francisco
             spare = strtok ( NULL, "\0" );         // part = 49ers
             strcpy ( partb, spare );
             
             printf ( "%d", strcmp ( parta, input ) );
             
             if ( strcmp( array [i], input ) == 0 )
             {
                  cmparray [k] = i;
                  k++;
             }
             
             /*
               Problem in next if statement
             */
             
             // Going to compare parts////////////////////////////////////////
             
             else if ( strcmp ( parta, input ) == 0 || strcmp ( partb, input ) == 0 )
             {
                  cmparray [k] = i;
                  k++;
             }
             ///////////////////////////////////////////////////////////////
             
         }
         return k;    //k is the number of elements in cmparray
    }
Topic archived. No new replies allowed.