C程序 - 不知道它为什么不起作用

时间:2014-08-20 00:26:17

标签: c

作业是创建一个程序,让用户输入学生姓名和成绩,在完成所有作业后列出输入,然后计算输入的所有成绩的平均值。该程序使用结构和循环。

我到目前为止的代码如下。它构建没有任何错误。但是,在我输入第一组"名字,姓氏,等级"然后点击Enter,程序只是坐在那里,光标闪烁。我似乎无法弄清楚我做错了什么。

#include <stdio.h>
#include <string.h>

#define MAX_LENGTH 50

// Structure to hold student names and grades
struct student
{
    char    firstName[MAX_LENGTH];
    char    lastName[MAX_LENGTH];
    int     grade;
};

int main ()
{
int i, j, num_students;
int sum = 0;
int cnt = 0;

// Determine how many students will be entered

printf ("Enter the number of students in your class: \n");
scanf ("%d", num_students);

// Obtain student names and grades
printf ("Enter first name, last name and grade separated by spaces. \n");
printf ("Enter END 0 when done entering information.\n\n");

struct student s[num_students];
for (i = 0; i < num_students; ++i) {

    scanf ("%s %s %d", s[i].firstName, s[i].lastName, s[i].grade);

    if(strcmp(s[i].firstName, "END") == 0) {
       break;
    }
}

// List students and grades entered
printf ("\nYou entered: \n");
for (j = 0; j < num_students; ++j){

    if(strcmp(s[j].firstName, "END") == 0){
        break;
    }
    printf("Name: %s %s, Grade: %d\n", s[i].firstName, s[i].lastName, s[i].grade);

}
// Calculate average of grades entered
for (i = 0; i < num_students; ++i)
{
    if (strncmp(s[i].firstName, "END", 3) == 0){
        break;
    }
    else {
        sum += s[i].grade;
        ++cnt;
    }
}

printf ("Average of Grades: %f\n", (float)sum/cnt);

return 0;
}

5 个答案:

答案 0 :(得分:1)

更改

printf("Name: %s %2, Grade: %d\n", s[i].firstName, s[i].lastName, &s[i].grade);  
                  ^ Wrong specifier                                  ^ i should be j    

printf("Name: %s %s, Grade: %d\n", s[j].firstName, s[j].lastName, s[j].grade);

答案 1 :(得分:1)

修正“%2错误并传递s[i].grade而不是&s[i].grade

这对我有用:我在插入快速反馈值之后添加了一行

#include <stdio.h>
#include <string.h>

#define MAX_ENTRIES 20
#define MAX_LENGTH 50

// Structure to hold student names and grades
struct student
{
  char firstName[MAX_LENGTH];
  char lastName[MAX_LENGTH];
  int grade;
};

int
main ()
{
  int i, j;
  int sum = 0;
  int cnt = 0;

  //Obtain student names and grades
  printf ("Enter first name, last name and grade separated by spaces. \n");
  printf ("Enter END 0 when done entering information.\n\n");

  struct student s[MAX_ENTRIES];
  for (i = 0; i < MAX_ENTRIES; ++i)
    {

    scanf ("%s %s %d", s[i].firstName, s[i].lastName, &s[i].grade);

    printf ("Name: %s %s, Grade: %d\n", s[i].firstName, s[i].lastName,
          s[i].grade);

      if (strcmp (s[i].firstName, "END") == 0)
    {
      break;
    }

    }

  //List students and grades entered
  printf ("\nYou entered: \n");
  for (j = 0; j < MAX_ENTRIES; ++j)
    {

      if (strcmp (s[j].firstName, "END") == 0)
    {
      break;
    }
      printf ("Name: %s %s, Grade: %d\n", s[j].firstName, s[j].lastName,
          s[j].grade);

    }
  //Calculate average of grades entered
  for (i = 0; i < MAX_ENTRIES; ++i)
    {
      if (strncmp (s[i].firstName, "END", 3) == 0)
    {
      break;
    }
      else
    {
      sum += s[i].grade;
      ++cnt;
    }
    }

  printf ("Average of Grades: %f\n", (float) sum / cnt);

  return 0;
}

答案 2 :(得分:0)

更改此行(第42行)

printf("Name: %s %2, Grade: %d\n", s[i].firstName, s[i].lastName, &s[i].grade);

到这个

printf("Name: %s %s, Grade: %d\n", s[j].firstName, s[j].lastName, s[j].grade);

我认为它会对你有所帮助:)。

答案 3 :(得分:0)

Append the code as , 

#include <stdio.h>
#include <string.h>

#define MAX_LENGTH 50

// Structure to hold student names and grades
struct student
{
    char    firstName[MAX_LENGTH];
    char    lastName[MAX_LENGTH];
    int     grade;
};

int main ()
{
int i, j, num_students;
int sum = 0;
int cnt = 0;

// Determine how many students will be entered

printf ("Enter the number of students in your class: \n");
scanf ("%d", &num_students);

// Obtain student names and grades
printf ("Enter first name, last name and grade separated by spaces. \n");
printf ("Enter END 0 when done entering information.\n\n");

struct student s[num_students];
for (i = 0; i < num_students; ++i) {

    scanf ("%s %s %d", s[i].firstName, s[i].lastName, &s[i].grade);

    if(strcmp(s[i].firstName, "END") == 0) {
       break;
    }
}

// List students and grades entered
printf ("\nYou entered: \n");
for (j = 0; j < num_students; ++j){

    if(strcmp(s[j].firstName, "END") == 0){
        break;
    }
    printf("Name: %s %s, Grade: %d\n", s[j].firstName, s[j].lastName, s[j].grade);

}
// Calculate average of grades entered
for (i = 0; i < num_students; ++i)
{
    if (strncmp(s[i].firstName, "END", 3) == 0){
        break;
    }
    else {
        sum += s[i].grade;
        ++cnt;
    }
}

printf ("Average of Grades: %f\n", (float)sum/cnt);

return 0;

}

Appended line 23 scanf ("%d", &num_students);
         line 32 scanf ("%s %s %d", s[i].firstName, s[i].lastName, &s[i].grade);
         line 46 printf("Name: %s %s, Grade: %d\n", s[j].firstName, s[j].lastName, s[j].grade);

答案 4 :(得分:0)

scanf参数中错过的地址运算符两次:

scanf( "%d", &num_students );
/* .. */
    scanf( "%s %s %d", s[i].firstName, s[i].lastName, & s[i].grade );

在打印数组中使用了前一个循环的索引而不是实际的索引:

printf("Name: %s %s, Grade: %d\n", s[j].firstName, s[j].lastName, s[j].grade);
提示输入num_students时,

处理&#34; END&#34; s是不必要的。只是一个建议:即使循环变量名也应该长于一个字符。除非你错过了FPU,否则请在浮动分区使用双倍免费。