我已经创建了一个头文件。我想在一个程序中使用这个头文件来吐出NFL球队的数据。我似乎有一个问题,我的循环下我的packers()
函数。我还没有真正理解指针,所以我不知道我在哪里出错了。有人能帮忙吗?首先是我的头文件:
好吧。现在,当您选择"1"时,它将编译并运行输出为"the Green Bay Packers"。为什么输入[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========nn");
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) Packersn"); //selection options for user
fprintf(stderr, "2) Vikingsn");
fprintf(stderr, "3) Cowboysn");
fprintf(stderr, "4) Exitn"); //exits the program
scanf("%dn", &x); //scanner to read the selection of the user
if(x<1 || x>4)
{
fprintf(stderr, "You entered an invalid entry. Please try againn"); //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;
}
我猜你想显示哪支球队在哪一年赢得了哪个超级碗那么你的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 %sn",nflTeam[i][j][k] ,superbowl[i][j][k] , yearWon[i][j][k]);
}
printf("n");
}
}
}
也在main change
scanf("% d n",x);
scanf("% d",x);
张贴的代码中的其他问题,这一行,在'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'?