我似乎无法为字符串变量运行数组

时间:2019-12-19 05:15:20

标签: c arrays string

我已经无数次地运行了此代码,但是我不确定这是怎么回事。每当我运行代码并选择“ 1”时,它都不会显示“ subj”变量。显示”选项。我得到的只是错误'divide error''异常程序终止'→'

这是我的代码:

#include <stdio.h>
#include <conio.h>

char subj[20], name[20], course[20], studentid[20];
float grade[20], hold, gu[20], guadd, gpa, temp, median, mode, max, a[20];
int z, i, h, n, tu, c, unit[20], count;

void disp(int n, int unit[], float grade[], char subj[], char name[], char studentid[], char course[])
{
    printf("Name: %s\n", name);
    printf("Course: %s\n", course);
    printf("Student ID: %s\n", studentid);
    printf("\n\nSubject\t\t\tUnits\t\t\tFinal Grade\n");

    for(i=0; i<n; i++)
    {
        printf("%s", subj[i]);
        printf("\t\t\t%d\t\t\t", unit[i]);
        printf("\%.2f\n", grade[i]);
    }
    tu=0;
    guadd=0;
    for(i=0; i<n; i++)
    {
        gu[i]=grade[i]*unit[i];
        tu+=unit[i];
        guadd+=gu[i];
    }
    gpa=guadd/tu;
    printf("\n\nSemester Grade Point Average:\t\t\t%.2f\n", gpa);
}

int main()
{
clrscr();
printf("Enter Student Name: ");
scanf ("%s", &name);
printf("Enter Student ID: ");
scanf ("%s", &studentid);
printf("Enter Course: ");
scanf ("%s", &course);
printf("Please Enter the Amount of Subjects Enrolled: ");
scanf ("%d", &n);

    for(i=0; i<n; i++)
    {
        printf("\nPlease Enter the Subject Name: ");
        scanf("%s", &subj[i]);
        printf("Please Enter the Final Grade for this Subject: ");
        scanf ("%f", &grade[i]);
        printf("Please Enter Number of Units for this Subject: ");
        scanf ("%d", &unit[i]);
    }
clrscr();
do
{
printf("\nPlease Choose an Option:\n\n1. Display Grades with GPA\n2. Ascending Order of Grades\n3. Descending Order of Grades\n4. Median and Mode\n5.EXIT\n\nChoice: ");
scanf ("%d", &z);
clrscr();

switch(z)
{
    case 1: disp(n, unit, grade, subj, name, studentid, course);
            break;
    case 2: ascending(n, grade);
            break;
    case 3: descending(n, grade);
            break;
    case 4: medmod(n, grade);
            break;
}
}
while (z!=5);


    getch();
    return 0;

}

P.S。我对此并不陌生,所以我真的不知道问题出在哪里。感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

我对您的代码进行了一些更改,并且可以正常工作。

char subj[20]替换为char *subj[20],因为您需要一个字符串数组,而不是一个字符串的字符数组。然后,动态分配内存给每个内存(从C的malloc使用stdlib.h),至少说100个字符。

此外,gcc编译器会生成足够好的警告,以了解发生了什么情况,因此很明显将参数固定为printfscanf函数。

PS:由于conio.h是不可移植的,因此我从代码中删除了它及其功能。

修改后的代码:

#include <stdio.h>
#include <stdlib.h>

char *subj[20];
char name[20], course[20], studentid[20];
float grade[20], hold, gu[20], guadd, gpa, temp, median, mode, max, a[20];
int z, i, h, n, tu, c, unit[20], count;

void disp(int n, int unit[], float grade[], char *subj[], char name[],
      char studentid[], char course[]) {
  printf("Name: %s\n", name);
  printf("Course: %s\n", course);
  printf("Student ID: %s\n", studentid);
  printf("\n\nSubject\t\t\tUnits\t\t\tFinal Grade\n");

  for (i = 0; i < n; i++) {
    printf("%s", subj[i]);
    printf("\t\t\t%d\t\t\t", unit[i]);
    printf("\%.2f\n", grade[i]);
  }
  tu = 0;
  guadd = 0;
  for (i = 0; i < n; i++) {
    gu[i] = grade[i] * unit[i];
    tu += unit[i];
    guadd += gu[i];
  }
  gpa = guadd / tu;
  printf("\n\nSemester Grade Point Average:\t\t\t%.2f\n", gpa);
}

int main() {
  for (i = 0; i < 20; i++)
    subj[i] = malloc(sizeof(char) * 100);
  printf("Enter Student Name: ");
  scanf("%s", name);
  printf("Enter Student ID: ");
  scanf("%s", studentid);
  printf("Enter Course: ");
  scanf("%s", course);
  printf("Please Enter the Amount of Subjects Enrolled: ");
  scanf("%d", &n);
  printf("%s %s %s %d", name, studentid, course, n);

  for (i = 0; i < n; i++) {
    printf("\nPlease Enter the Subject Name: ");
    scanf("%s", subj[i]);
    printf("%s", subj[i]);
    printf("Please Enter the Final Grade for this Subject: ");
    scanf("%f", &grade[i]);
    printf("Please Enter Number of Units for this Subject: ");
    scanf("%d", &unit[i]);
  }
  do {
    printf("\nPlease Choose an Option:\n\n1. Display Grades with GPA\n2. "
       "Ascending Order of Grades\n3. Descending Order of Grades\n4. "
       "Median and Mode\n5.EXIT\n\nChoice: ");
    scanf("%d", &z);

    switch (z) {
      case 1:
        disp(n, unit, grade, subj, name, studentid, course);
        break;
      case 2:
        break;
      case 3:
        descending(n, grade);
        break;
      case 4:
        medmod(n, grade);
        break;
    }
  } while (z != 5);

  return 0;
}