简体   繁体   中英

C Program - Don't know why it isn't working

The assignment is to create a program that lets the user enter student names and grades, list the input once all entries have been made, then calculate the average for all grades entered. The program uses structures and loops.

The code I have so far is below. It builds without any errors. However, after I enter the first set of "first name, last name, grade" and hit Enter, the program just sits there with a blinking cursor. I can't seem to figure out what I'm doing wrong.

#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;
}

Change

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

to

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

Fix the ' %2 bug and pass s[i].grade not &s[i].grade

This works for me: I added a line just after inserting the values for quick feedback

#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;
}

Change this line (line no. 42)

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

to this

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

I think it will help you :)

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);

Missed address operator in scanf argument twice:

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

And index of previous loop was used instead of actual at printing array:

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

Handling "END"s is unneccessary when prompting for num_students. Just a suggestion: even loop variable names should be longer than a character. And feel free using double at floating point division unless you miss FPU.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM