Array of structs not updating in C project.

huangapple go评论65阅读模式
英文:

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 &lt; (currentEntries); i++) {
  printf(&quot;%s&quot;,listOfStudents[currentEntries].name);
  printf(&quot;%f&quot;,listOfStudents[currentEntries].gpa);
  printf(&quot;%s&quot;,listOfStudents[currentEntries].major);
    
  // these print statemnts above print &quot;&quot;, 0, and &quot;&quot; respectively.

}

The index is wrong. Replace currentEntries inside the loop with i:

int i;
for (i = 0; i &lt; (currentEntries); i++) {
  printf(&quot;%s&quot;,listOfStudents[i].name);
  printf(&quot;%f&quot;,listOfStudents[i].gpa);
  printf(&quot;%s&quot;,listOfStudents[i].major);
    
  // these print statemnts above print &quot;&quot;, 0, and &quot;&quot; respectively.

}

As well noted in the comments section, you should also fix the following block:

if (currentEntries &gt; 20) {
  currentEntries = 20;
  printf(&quot;Database is full&quot;);
  return;
}

To something like:

if (currentEntries == 20) {
  printf(&quot;Database is full&quot;);
  return;
} 

huangapple
  • 本文由 发表于 2023年2月18日 11:12:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/75490946.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定