英文:
Array of structs not updating in C project
问题
  printf("%s", listOfStudents[i].name);
  printf("%f", listOfStudents[i].gpa);
  printf("%s", listOfStudents[i].major);
英文:
I am currently trying to make a Student Database system for my introductory C class. I am working on the feature of adding new students to the database so that it can be displayed. However, whenever i try to print out each individual feature of a struct in the array, it does not return what I intended. Floats become 0 and strings are not visible.
For context, here is the code used to display each element of the array
void displayStudents() {
  printf("\t\tList of Student Information\n");
  printf("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\n ");
  printf("|No.| \t \t \t Name \t \t \t |\t Major \t\t| GPA |");
  printf("\n- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\n");
  int i;
  for (i = 0; i < (currentEntries); i++) {
    printf("%s",listOfStudents[currentEntries].name);
    printf("%f",listOfStudents[currentEntries].gpa);
    printf("%s",listOfStudents[currentEntries].major);
    
    // these print statemnts above print "", 0, and "" respectively.
  }
  
}
Here are the functions used to add new students into the array
typedef struct Student {
  float gpa;
  char major[100];
  char name[100];
} student;
student createNewStudent(char sname[100], float gpa, char smajor[100]) {
  student newstudent;
  newstudent.gpa = gpa;
  strcpy(newstudent.name,sname);
  strcpy(newstudent.major,smajor);
  return newstudent;
}
void addNewStudents() {
  char name[100];
  char major[100];
  float gpa;
  if (currentEntries > 20) {
    currentEntries = 20;
    printf("Database is full");
    return;
  }
  printf("\t\t Insert Student Information \n");
  printf("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \n ");
  printf("Name: \t ");
  scanf("%[^\n]s",name);
  printf("\nMajor\t: ");
  scanf(" %[^\n]s",major);
  printf("\nGPA\t:");
  scanf(" %f", &gpa);
  listOfStudents[currentEntries] = createNewStudent(name,gpa,major);
  currentEntries++;  
}
and here is how theyre called in main()
  scanf("%d", &selection);
  switch(selection) {
      case 1:
        displayStudents();
        promptKeyPress(&inMenu);
        break;
      case 2:
        addNewStudents(); 
        promptKeyPress(&inMenu);
        break;
      default:
        programRunning = false;
        break;
also, both currentEntries and listOfStudents are defined as global variables
int currentEntries = 0;
student listOfStudents[20];
So, my question is, how should I tackle this problem so that the displayStudents function would print the correct values of my input (the student name, major and gpa)?
Any help would be appreciated. Thanks!
答案1
得分: 2
我认为问题出在这里:
int i;
for (i = 0; i < (currentEntries); i++) {
  printf("%s", listOfStudents[currentEntries].name);
  printf("%f", listOfStudents[currentEntries].gpa);
  printf("%s", listOfStudents[currentEntries].major);
    
  // 这些打印语句分别打印"", 0 和 ""。
}
索引是错误的。请在循环中用i替换currentEntries:
int i;
for (i = 0; i < (currentEntries); i++) {
  printf("%s", listOfStudents[i].name);
  printf("%f", listOfStudents[i].gpa);
  printf("%s", listOfStudents[i].major);
    
  // 这些打印语句分别打印"", 0 和 ""。
}
正如评论部分所指出的,您还应该修复以下代码块:
if (currentEntries > 20) {
  currentEntries = 20;
  printf("Database is full");
  return;
}
改为:
if (currentEntries == 20) {
  printf("Database is full");
  return;
}
英文:
I think the problem is here:
int i;
for (i = 0; i < (currentEntries); i++) {
  printf("%s",listOfStudents[currentEntries].name);
  printf("%f",listOfStudents[currentEntries].gpa);
  printf("%s",listOfStudents[currentEntries].major);
    
  // these print statemnts above print "", 0, and "" respectively.
}
The index is wrong. Replace currentEntries inside the loop with i:
int i;
for (i = 0; i < (currentEntries); i++) {
  printf("%s",listOfStudents[i].name);
  printf("%f",listOfStudents[i].gpa);
  printf("%s",listOfStudents[i].major);
    
  // these print statemnts above print "", 0, and "" respectively.
}
As well noted in the comments section, you should also fix the following block:
if (currentEntries > 20) {
  currentEntries = 20;
  printf("Database is full");
  return;
}
To something like:
if (currentEntries == 20) {
  printf("Database is full");
  return;
} 
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论