我對此非常陌生,并且有一項學校作業,我必須撰寫一個成績簿程式,該程式使用自定義結構來保存學生 ID 和成績。幾天來我一直在嘗試不成功地弄清楚為什么它不能正確列印,或者當它列印時(經過大量轉移之后)它只列印第二組輸入。我無計可施。任何人都可以幫忙嗎?
Gradebook.h 部分是自定義結構。
// gradebook.h
struct gradeID
{
int id;
char grades[25];
};
// Gradebook.h is a header file to define
// a global structure.
#include "gradebook.h"
#include <stdio.h>
#include <stdlib.h>
void sort(struct gradeID *, int);
int main(void)
{
// Variables and structure definitions
int ctr;
char contInput;
int i;
struct gradeID grade;
struct gradeID *identifier;
int *temps;
// Allocates 10 integers worth of memory for the program to use.
// If there is not enough memory, the program will print an error
// statement and terminate the program.
temps = (int *) malloc(10 * sizeof(int));
if (temps == 0)
{
printf("Not enough memory!\n");
exit(1);
}
// Prints basic instructions for the program
printf("\t\tGradebook Recorder\n");
printf("Input student IDs and grades.\n");
printf("These will be sorted by ID and printed.\n");
/* Creates a for loop that will continue until the program
hits the designated number of array elements. For the sake
of expediency, I have set this amount to 10, but it can be
changed as necessary.*/
for(i = 0; i < 10; i )
{
printf("Input student ID:\n");
scanf(" %d", &grade.id);
printf("Input grade:\n");
scanf(" %s", grade.grades);
// This allows early exit of the program
printf("Do you have more grades to enter?\n");
printf("Y/N\n");
scanf(" %c", &contInput);
if(contInput == 'N' || contInput == 'n')
{
printf("Finalizing and printing input-\n\n");
break;
}
ctr ;
}
printf("Grades Sorted by Student ID:\n\n");
printf("\tStudent ID: Student Grade: \n");
for(i = 0; i < ctr; i )
{
printf("\t%d", grade.id );
printf("\t%s", grade.grades);
}
identifier[i] = grade;
return(0);
free(temps);
}
void sort(struct gradeID identifier[], int counter)
{
int inner;
int outer;
struct gradeID temp;
// Loops for sorting
for(outer = 0; outer < counter - 1; outer)
{
for(inner = outer 1; inner < counter; inner)
{
if(identifier[inner].id < identifier[outer].id)
{
temp = identifier[inner];
identifier[inner] = identifier[outer];
identifier[outer] = temp;
}
}
}
return;
}
uj5u.com熱心網友回復:
指標identifier
未初始化
struct gradeID *identifier;
所以這個說法
identifier[i] = grade;
獨立于i
呼叫未定義行為的值。
在這個 for 回圈中
for(i = 0; i < 10; i )
{
printf("Input student ID:\n");
scanf(" %d", &grade.id);
printf("Input grade:\n");
scanf(" %s", grade.grades);
// This allows early exit of the program
printf("Do you have more grades to enter?\n");
printf("Y/N\n");
scanf(" %c", &contInput);
if(contInput == 'N' || contInput == 'n')
{
printf("Finalizing and printing input-\n\n");
break;
}
ctr ;
}
grade
您正在結構型別的同一物件中輸入新資料。因此,新資料會覆寫物件中存盤的先前資料。
此外,變數ctr
未初始化
int ctr;
所以上面for回圈中的這個陳述句
ctr ;
還呼叫未定義的行為。
temps
指向動態分配陣列的變數
temps = (int *) malloc(10 * sizeof(int));
未使用。
這個說法
free(temps);
永遠不會獲得控制權,因為在它之前有 return 陳述句
return(0);
free(temps);
您需要定義一個型別的陣列,struct gradeID
例如
struct gradeID grades[10];
并用值填充它。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/492611.html
上一篇:接受用戶的輸入,然后列印字母系列