我有一个我创建的头文件。我想在一个程序中使用这个头文件来吐出NFL团队的数据。我似乎在我的packers()
函数下遇到了问题。我还没有真正理解指针,所以我不知道我在哪里错了。有人可以帮忙吗?
首先是我的头文件:
好。现在它编译并运行输出"绿湾包装工队"当你选择' 1'。为什么它与[2] [0] [3]一起输入?我如何结合" char superbowl和yearWon"的其他功能?我想让它编译所有这些信息...
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"}}
};
接下来是我的代码:
#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;
}
答案 0 :(得分:1)
我想你想表明哪支球队在哪一年赢得了哪个超级碗,那么你的packers
功能看起来
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");
}
}
}
同样在主要变化中
scanf(“%d \ n”,&amp; x);
到
scanf(“%d”,&amp; x);
答案 1 :(得分:0)
发布代码的其他问题,这一行,在'packers()'中:
i = *nflTeam[0][j][k];
应该看:指向字符串的指针,但是,'i'被定义为'int'而'''正在尝试将字符串的内容分配给整数。
也许你的意思是:
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]);
然而,为什么打印到'stderr'而不是'stdout'?