1

I have a header file that I have created. I want to use this header file in a program to spit out data on NFL teams. I seem to be having an issue with my loop under my packers() function. I do not really understand pointers yet so I don't know where I am going wrong with this. Can anyone be of assistance? First is my header file:

Okay. Now it compiles and runs with the output "The Green Bay Packers" when you select '1'. Why does it work with [2][0][3] entered? And how do I incorporate the other functions of "char superbowl and yearWon"?? I want to have it compile all of that info...

char *nflTeam[4][2][4] =
{                 {{"49ers", "Bears", "Bengals", "Bills"}, {"Broncos", "Browns", "Bucaneers", "Cardinals"}},     //data to that shows all team names in
                  {{"Chargers", "Chiefs", "Colts", "Cowboys"}, {"Dolphins", "Eagles", "Falcons", "Giants"}},     //the NFL
                  {{"Jaguars", "Jets", "Lions", "Packers"}, {"Panthers", "Patriots", "Raiders", "Rams"}},
                  {{"Ravens", "Redskins", "Saints", "Seahawks"}, {"Steelers", "Texans", "Titans", "Vikings"}}

};

char *superbowl[4][2][4] =
{                {{"Super Bowls XVI, XIX, XXIII, XXIV & XXIX", "Super Bowl XX", "Im sorry, they lost twice!", "They lost 4 times in a row!!"}, {"Super Bowls XXXII, XXXIII", "Cheer for a different team...", "Super Bowl XXXVII", "They went once...and lost once"}},                               //array of data that 
                 {{"Your team sucks. They lost.", "Super Bowl IV", "Super Bowl XLI", "Super Bowls VI, XII, XXVII, XXVIII & XXX"}, {"Super Bowls VII & VIII", "They have gone twice, and lost twice.", "Sorry to burst your bubble, they lost.", "Super Bowls XXI, XXV, XLII and XLVI"}},                   //that displays what super bowl
                 {{"Never been, never will.", "Super Bowl III", "You should cheer for the Packers", "Super Bowls I, II, XXXI and XLV"}, {"Im sorry, they lost once", "Super Bowls XXXVI, XXXVIII, XXXIX and XLIX", "Super Bowls XI & XV", "Super Bowl XXXIV"}},                                         //the corresponding team has won
                 {{"Super Bowls V, XXXV & XLVII", "Super Bowls XVII, XXII & XXVI", "Super Bowl XLIV", "Super Bowl XLVIII"}, {"Super Bowls IX, X, XIII, XIV, XL & XLIII", "Time to root for a new team.", "Gone once...and lost", "Gone 4 times, lost EVERY time. You should root for the Packers!"}}    //or not won
};

char *yearWon[4][2][4] =
{            {{"1982, 1985, 1989, 1990 & 1995","1986", "1982, 1989", "1991, 1992, 1993 & 1994"}, {"1998 & 1999", "They have never been to a Super Bowl", "2003", "2009"}},                          //array of data that shows the year/s
             {{"1995", "1970", "2007", "1972, 1978, 1993, 1994 & 1996"}, {"1973 & 1974", "1981 & 2005", "1999", "1987, 1991, 2008 & 2012"}},                                                         //the corresonding team won
             {{"um...", "1969", "Never been", "1967, 1968, 1997 & 2011"}, {"2004", "2002, 2004, 2005 & 2015", "1977 & 1981", "2000"}},   
             {{"1971, 2001 & 2013", "1983, 1988 & 1992", "2010", "2014"}, {"1975, 1976, 1979, 1980, 2006 & 2009", "They have never gone to the big dance!", "2000", "1970, 1974, 1975 & 1977"}}
};

Next is my code:

#include <stdio.h>
#include "nflData.h>
int main(void)
{
    int packers(void);
    int x=0;

    while(1)                                    //while loop to run menu program
    {
        fprintf(stderr, "\n========MENU========\n\n");

        fprintf(stderr, "Choose a team to find out some NFL trivia!: \nEnter 4 to quit.\n");        //prompt user to enter a selection

        fprintf(stderr, "1) Packers\n");        //selection options for user
        fprintf(stderr, "2) Vikings\n");
        fprintf(stderr, "3) Cowboys\n");
        fprintf(stderr, "4) Exit\n");           //exits the program
        scanf("%d\n", &x);                      //scanner to read the selection of the user

        if(x<1 || x>4)
        {
            fprintf(stderr, "You entered an invalid entry. Please try again\n");    //denotes that user entered a number that did not
        }       
        else                                                                    //fall within the selection guidelines
        if(x==1)
        {
            packers();      //call to function packers
        }

    }
    return 0;
}

int packers()                   //i think the problem is in this function...
{
    int i=0;
    int j, k;
    for(j = 0; j<2; j++){       
        for(k=0; k<4; k++){
            i = *nflTeam[0][j][k];  
            fprintf(stderr,"The Green Bay %s", nflTeam[2][0][3]);   
        }
    }
    return i;


}
6
  • 1
    Try printf( "%s\n", nflTeam[2][0][3] ); Commented Sep 17, 2015 at 18:10
  • 1
    the code, in the head file, should ONLY define the array. and declare such an array as 'extern'. Then one and only one source file should define an instance of that array, with the initializers. Commented Sep 17, 2015 at 18:15
  • the code does not cleanly compile. Always compile with all warnings enabled, then fix those warnings. for instance, the main parameters: argc and argv[] are not used. That will cause the compiler to raise two warnings. to fix, declare main as: int main( void ) Commented Sep 17, 2015 at 18:17
  • the code is missing a prototype for the 'packers()' function. the prototype type should be placed just before the main() function and look like: int packers( void ) Commented Sep 17, 2015 at 18:19
  • 1
    the head file is missing the 'guard' against multiple inclusion. in general, when asking about a runtime problem, post code that cleanly compiles and shows the problem. The posted code does not compile Commented Sep 17, 2015 at 18:22

2 Answers 2

1

I guess you wanted to show which team won which superbowl in which year then your packers function looks

void packers()                 
    int j, k;
    for(int i=0; i<4; i++){
        for(int j=0; j<2; j++){
            for(int k=0; k<4; k++){
               fprintf(stderr,"The Green Bay %s won %s in %s\n",nflTeam[i][j][k] ,superbowl[i][j][k] , yearWon[i][j][k]);
            }
            printf("\n");
        }
    }
}

Also in main change

scanf("%d\n", &x);

to

scanf("%d", &x);

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

2 Comments

the packers sub function seems to be only for the packers team, not for all the teams.
I don't know the intention of OP, I guess that he wanted to show all team information. If it is for packers team there no need of a function call because there just one team and one entry.
0

amongst other problems with the posted code, this line, in 'packers()':

i = *nflTeam[0][j][k]; 

should be looking at: pointers to strings, however, 'i' is defined as a 'int' and the '*' is trying to assign the contents of a string to an integer.

perhaps you meant:

no 'i' processing at all


fprintf(stderr,"The Green Bay %s won %s in %s", 
    nflTeam[0][j]pk], superbowl[0][j][k], yearwon[0][j][k]);

however, why printing to 'stderr' rather than 'stdout'?

Comments

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.