因此在函数getRoster中,我有两个在main中以相同方式定义并以相同方式调用的数组。但是由于某种原因,其中一个scanf函数将垃圾数据写入数组,以便在第91行(在本例中为调试行)中,一个值是我输入的值,而另一个值似乎是随机的。我已经从上到下检查了此代码,以查看是否有添加到一个数组而不是其他数组的任何符号,而且我似乎找不到一个。
#include <stdio.h>
#include <stdlib.h>
void getRoster(int *jerseyNumbers[10], int *playerRatings[10])
{
int i;
for (i=0; i<5; ++i)
{
printf("Enter player %d's jersey number\n", i+1);
scanf("%d", &jerseyNumbers[i]);
printf("Enter player %d's rating\n", i+1);
scanf("%d", &playerRatings[i]);
}
}
void updateRating(int *jerseyNumbers[10], int *playerRatings[10])
{
int i, n = -1, hold;
printf("Enter a jersey number:\n");
scanf("%d", n);
for (i=0; i<5; ++i)
if(jerseyNumbers[i]==n)
hold = n;
if (n=-1);
printf("Error");
printf("Enter a new rating for player:\n");
scanf("%d", &playerRatings[n]);
}
void aboveRating()
{
}
void replacePlayer()
{
}
void outputRoster(int *jerseyNumbers[10], int *playerRatings[10])
{
int i;
for (i=0; i<5; ++i)
{
printf("Player %d -- Jersey number: %d , Rating: %d \n", i+1, jerseyNumbers[i], playerRatings[i]);
}
}
void menu(int *jerseyNumbers[10], int *playerRatings[10])
{
char menuInput;
printf("\nMENU\n");
printf("u - Update player rating\n");
printf("a - Output players above a rating\n");
printf("r - Replace player\n");
printf("o - Output roster\n");
printf("q - Quit\n");
printf("\nChoose an option:\n");
scanf(" %c", &menuInput);
if (menuInput == 'u')
{updateRating(&jerseyNumbers[10], &playerRatings[10]);
}
else if (menuInput == 'a')
{aboveRating();
}
else if (menuInput == 'r')
{replacePlayer();
}
else if (menuInput == 'o')
{outputRoster(&jerseyNumbers[10], &playerRatings[10]);
}
else if (menuInput == 'q')
printf("Reached Quit");
else
printf("Input Error\n");
}
int main()
{
int *jerseyNumbers[10];
int *playerRatings[10];
char menuInput;
int quitFlag = 0;
getRoster(&jerseyNumbers[10], &playerRatings[10]);
printf("%d %d", jerseyNumbers[0], playerRatings[1]);
menu(&jerseyNumbers[10], &playerRatings[10]);
return 0;
}
答案 0 :(得分:3)
请参阅@NaveenKumar和@DeiDei对您的问题的评论。 实际上,程序的逻辑还可以。但是哪里出错了,是关于如何声明数组并将其作为参数传递给函数的语法。我已在下面列出了要进行的更改:
首先,Dylan不要将数组声明为:int
*jerseyNumbers[10]
和int *playerRatings[10]
。对于实际需要的常规数组,只需声明为:
a。 *jerseyNumbers
和*playerRatings
...删除尺寸。或
b。 jerseyNumbers[10]
和playerRatings[10]
...从声明中删除*
。
getRoster(jerseyNumbers, playerRatings);
和menu(jerseyNumbers, playerRatings);
。在函数定义中,表示数组的参数应为*arrayname
或arrayname[size]
。由于我们在声明期间使用了第一个,因此在此处使用如下所示:
void getRoster(int *jerseyNumbers, int *playerRatings) {...}
,
void updateRating(int *jerseyNumbers, int *playerRatings){...}
,
void outputRoster(int *jerseyNumbers, int *playerRatings) {...}
,
void menu(int *jerseyNumbers, int *playerRatings) {...}
。
正如@ PaulSm4所建议的,不要在if
条件的末尾使用分号。尽管这不是造成问题的原因,但您必须遵循这种做法,并避免犯错。
我在下面附上了工作代码以及输出。
代码:
#include <stdio.h>
#include <stdlib.h>
void getRoster(int *jerseyNumbers, int *playerRatings)
{
int i;
for (i=0; i<5; ++i)
{
printf("Enter player %d's jersey number\n", i+1);
scanf("%d", &jerseyNumbers[i]);
printf("Enter player %d's rating\n", i+1);
scanf("%d", &playerRatings[i]);
}
}
void updateRating(int *jerseyNumbers, int *playerRatings)
{
int i, n = -1, hold;
printf("Enter a jersey number:\n");
scanf("%d", n);
for (i=0; i<5; ++i)
if(jerseyNumbers[i]==n)
hold = n;
if (n=-1)
printf("Error");
printf("Enter a new rating for player:\n");
scanf("%d", &playerRatings[n]);
}
void aboveRating()
{
}
void replacePlayer()
{
}
void outputRoster(int *jerseyNumbers, int *playerRatings)
{
int i;
for (i=0; i<5; ++i)
{
printf("Player %d -- Jersey number: %d , Rating: %d \n", i+1, jerseyNumbers[i], playerRatings[i]);
}
}
void menu(int *jerseyNumbers, int *playerRatings)
{
char menuInput;
printf("\nMENU\n");
printf("u - Update player rating\n");
printf("a - Output players above a rating\n");
printf("r - Replace player\n");
printf("o - Output roster\n");
printf("q - Quit\n");
printf("\nChoose an option:\n");
scanf(" %c", &menuInput);
if (menuInput == 'u')
{updateRating(jerseyNumbers, playerRatings);
}
else if (menuInput == 'a')
{aboveRating();
}
else if (menuInput == 'r')
{replacePlayer();
}
else if (menuInput == 'o')
{outputRoster(jerseyNumbers, playerRatings);
}
else if (menuInput == 'q')
printf("Reached Quit");
else
printf("Input Error\n");
}
int main()
{
int jerseyNumbers[10];
int playerRatings[10];
char menuInput;
int quitFlag = 0;
getRoster(jerseyNumbers, playerRatings);
printf("%d %d", jerseyNumbers[0], playerRatings[1]);
menu(jerseyNumbers, playerRatings);
return 0;
}
输出:
Enter player 1's jersey number 1 Enter player 1's rating 10 Enter player 2's jersey number 2 Enter player 2's rating 20 Enter player 3's jersey number 3 Enter player 3's rating 30 Enter player 4's jersey number 4 Enter player 4's rating 40 Enter player 5's jersey number 5 Enter player 5's rating 50 1 20 MENU u - Update player rating a - Output players above a rating r - Replace player o - Output roster q - Quit Choose an option: o Player 1 -- Jersey number: 1 , Rating: 10 Player 2 -- Jersey number: 2 , Rating: 20 Player 3 -- Jersey number: 3 , Rating: 30 Player 4 -- Jersey number: 4 , Rating: 40 Player 5 -- Jersey number: 5 , Rating: 50
希望这会有所帮助。
答案 1 :(得分:0)
强烈建议:
养成经常使用大括号的习惯-即使在不需要时也是如此。
void updateRating(int *jerseyNumbers, int *playerRatings)
// You probably only need a pointer or an array - but probably not both ;)
{
int i, n = -1, hold;
printf("Enter a jersey number:\n");
scanf("%d", n);
for (i=0; i<5; ++i) { // Curly brace here...
if(jerseyNumbers[i]==n) { // And here...
hold = n;
}
}
if (n=-1) { // BUG ALERT: deleted ";" (and added a brace)
printf("Error"); // BUG ALERT: fixed this by removing ";"?
} else {
printf("Enter a new rating for player:\n");
scanf("%d", &playerRatings[n]);
}
也许更好:
void updateRating(int *jerseyNumbers, int *playerRatings)
{
int n;
printf("Enter a jersey number:\n");
scanf("%d", n);
printf("Enter a new rating for player:\n");
scanf("%d", &playerRatings[n]);
}
而且,正如瑞诗凯诗·拉杰(Rishikesh Raje)正确指出的那样:
数组的定义和对OP的函数调用也是 不正确。请也提及这一点。