在指向整数数组的指针中使用scanf

时间:2013-08-22 10:47:38

标签: c scanf

该程序应该存储3名学生的输入成绩并打印平均值。但是,如果我要求学生B的平均分,则会打印出学生A的平均分数。而学生A的平均分为0.我似乎无法找到出错的地方..请帮助 例如Student_A = {7,7,7},Student_B = {8,8,8}; ave(Student_B)= 7

#include<stdio.h>

int i;
char j;
int student_A[4];
int student_B[4];
int student_C[4];
float grade_input(int student[]);
float ave(int student[]);

main(){
    printf("For Student A:\n");
    grade_input(&student_A[4]);
    printf("For Student B:\n");
    grade_input(&student_B[4]);
    printf("For Student C:\n");
    grade_input(&student_C[4]);
    do{
        printf("Whose average grade do you want to see, a ,b ,c?  ");
        getchar();
        scanf("%c", &j);

        if(j=='a'){
            printf("%.2f\n", ave(student_A));
        }
        if(j=='b'){
            printf("%.2f\n", ave(student_B));
        }
        if(j=='c'){
            printf("%.2f\n", ave(student_C));
        }
    }while(j=='a' || j=='b' || j=='c');
}

float grade_input(int student[]){
    int i;
    for(i=0; i<3; i++){
        printf("Enter grade %d: ", i+1);
        scanf("%d", &student[i]);
    }
}

float ave(int student[]){
    return (student[0] + student[1] + student[2])/3.0;
}

2 个答案:

答案 0 :(得分:1)

您遇到的问题是将&student_X[4]传递给grade_input()。这只是非法的,因为它是一个4元素阵列;访问具有下标4的元素未定义。由于它们是连续定义的,因此当您使用student_B调用grade_input()函数时,最终会有效地将指针发送到&student_A[4],而这正是您所看到的!

您应该将指针传递给数组。我稍微改写了下面的代码来说明。

#include<stdio.h>

int i;
char j;
int student_A[4];

/* ******* */
/* note that student_A is defined as a 4 element array */

int student_B[4];
int student_C[4];
float grade_input(int student[]);
float ave(int student[]);

main(){
  printf("For Student A:\n");

  /* ******* */
  /* note that student_A is defined as a 4 element array */
  /* a pointer to the array is just student_A, not &student_A[4] */
  grade_input(student_A);
  printf("For Student B:\n");
  grade_input(student_B);
  printf("For Student C:\n");
  grade_input(student_C);
  do{
    printf("Whose average grade do you want to see, a ,b ,c?  ");
    scanf("%c", &j);

    if(j=='a'){
      printf("%.2f\n", ave(student_A));
    }
    else if(j=='b'){
      printf("%.2f\n", ave(student_B));
    }
    else if(j=='c'){
      printf("%.2f\n", ave(student_C));
    }
    else
      printf ( "Enter a, b or c\n" );

  }while(j != 'q');
}

float grade_input(int student[]){
  int i;
  for(i=0; i<3; i++){
    printf("Enter grade %d: ", i+1);
    scanf("%d", &student[i]);
  }
}

float ave(int student[]){
  return (student[0] + student[1] + student[2])/3.0;
}

答案 1 :(得分:0)

getchar();之前无需执行scanf("%c", &j); 你应该用fflush(stdout);清理缓冲区还是

char c;
while ((c = getchar()) != '\n');

所以你的完整代码应该是:

#include<stdio.h>

int i;
char j;
int student_A[4];
int student_B[4];
int student_C[4];
float grade_input(int student[]);
float ave(int student[]);

main(){
    printf("For Student A:\n");
    grade_input(&student_A[4]);
    printf("For Student B:\n");
    grade_input(&student_B[4]);
    printf("For Student C:\n");
    grade_input(&student_C[4]);
    do{
        printf("Whose average grade do you want to see, a ,b ,c?  ");
        fflush(stdout); // THE CHANGE
        scanf("%c", &j);

        if(j=='a'){
            printf("%.2f\n", ave(student_A));
        }
        if(j=='b'){
            printf("%.2f\n", ave(student_B));
        }
        if(j=='c'){
            printf("%.2f\n", ave(student_C));
        }
    }while(j=='a' || j=='b' || j=='c');
}

float grade_input(int student[]){
    int i;
    for(i=0; i<3; i++){
        printf("Enter grade %d: ", i+1);
        scanf("%d", &student[i]);
    }
}

float ave(int student[]){
    return (student[0] + student[1] + student[2])/3.0;
}