我不明白为什么我总是收到这个错误。该程序运行良好,但始终会出现此错误。
我认为这可能与数组有关,但我不确定100%。
任何人都可以给我的帮助将对我有所帮助。我什至都不是程序员。我只是在学习信息技术,所以我对此并不擅长。
https://i.stack.imgur.com/XoH0x.png
int main(void)
{
// Constant and Variable Declarations
const int ARRAY_SIZE = 4;//decarling the array size
int homeScore[ARRAY_SIZE];
int awayScore[ARRAY_SIZE];
int finalHome = 0;
int finalAway = 0;
// *** Your program goes here ***
for (int i = 1; i <= 4; i++) {
printf("How Many Points were Scored by Home Team in Quarter #%d? ", i);//getting the hometeam numbers
scanf("%d", &homeScore[i]);
while (homeScore[i] < 0) { //checking continuously home team score negative or not
printf("\tThe number entered cannot be negative. Please re-enter the number #%d? ", i);
scanf("%d", &homeScore[i]);
}
printf("How Many Points were Scored by Away Team in Quarter #%d? ", i);//getting the awayteam numbers
scanf("%d", &awayScore[i]);
while (awayScore[i] < 0) {//checking continuously away team score negative or not
printf("\tThe number entered cannot be negative. Please re-enter the number #%d? ", i);
scanf("%d", &awayScore[i]);
}printf("\n");
}
printf("After each quarter, the score was:");
//printing Score Board
printf("\n H\t A\n");
printf("\t---\t---");
for (int i = 1; i <= 4; i++) {
finalHome = finalHome + homeScore[i];//processing
finalAway = finalAway + awayScore[i];//processing
printf("\nQ%d", i);
printf("\t %d\t", finalHome);//printing out the scores for the home team
printf(" %d\t", finalAway);//printing out scores for the away team
}
printf("\n");
if (finalHome > finalAway) {//printing out the home team win
printf("Yea! The Home Team Won!\n");
}
else if (finalHome == finalAway) {//printing out if they tied
printf("After 4 quarters, the score was tied.\n");
}
else {//printing out if the away team won
printf("Sorry, the Away Team won.\n");
}
printf("\n");
return 0;
} // end main()
答案 0 :(得分:2)
正如我在评论中所说,C数组索引从0到len -1。所以改变
for (int i = 1; i <= 4; i++) {
到
for (int i = 0; i <4; i++) {
应该是
for (int i = 0; i < ARRAY_SIZE; i++) {
答案 1 :(得分:0)
Kilogramos Huerta
13807.00 El DOS
不是for (int i = 0; i < 4;i++)
在数组中,我们从0开始计数
例如:array [0],array [1],array [n]
答案 2 :(得分:0)
太棒了!非常令人印象深刻。
通常,我尝试将'\ n'放在行尾。这是您的代码,进行了一些更改。祝你好运!
正如其他人所指出的那样,在c ++中,由于数组的索引从0开始,我们通常会运行从i = 0到i #include <stdio.h>
int main(void)
{
// Constant and Variable Declarations
const int ARRAY_SIZE = 4;//decarling the array size
int homeScore[ARRAY_SIZE];
int awayScore[ARRAY_SIZE];
int finalHome = 0;
int finalAway = 0;
for (int i = 0; i < 4; i++) {
printf("How Many Points were Scored by Home Team in Quarter #%d? ", i);//getting the hometeam numbers
scanf("%d", &homeScore[i]);
while (homeScore[i] < 0) { //checking continuously home team score negative or not
printf("\tThe number entered cannot be negative. Please re-enter the number #%d? ", i);
scanf("%d", &homeScore[i]);
}
printf("How Many Points were Scored by Away Team in Quarter #%d? ", i);//getting the awayteam numbers
scanf("%d", &awayScore[i]);
while (awayScore[i] < 0) {//checking continuously away team score negative or not
printf("\tThe number entered cannot be negative. Please re-enter the number #%d? ", i);
scanf("%d", &awayScore[i]);
}printf("\n");
}
printf("After each quarter, the score was:");
//printing Score Board
printf("\n\t\tH\t A\n");
printf("\t\t---\t ---\n");
for (int i = 0; i < 4; i++) {
finalHome = finalHome + homeScore[i];//processing
finalAway = finalAway + awayScore[i];//processing
printf("Q%d", i);
printf("\t %3d", finalHome);//printing out the scores for the home team
printf("\t %3d\n", finalAway);//printing out scores for the away team
}
if (finalHome > finalAway) {//printing out the home team win
printf("Yea! The Home Team Won!\n");
}
else if (finalHome == finalAway) {//printing out if they tied
printf("After 4 quarters, the score was tied.\n");
}
else {//printing out if the away team won
printf("Sorry, the Away Team won.\n");
}
printf("\n");
return 0;
} // end main()