C - 循环错误

时间:2017-06-20 00:31:19

标签: c algorithm

我试着理解为什么我得到这个输出,我把一个while循环去另一个方法,我不知道为什么它不起作用! debug上的操作显示值1,if if clausule上的intry条件相同,并调用register方法在屏幕上打印“Test”。

//Struct Student
typedef struct {

    char name[50];
    float grade[4];
    int status;

} Student;

Student students[MAX_STUDENT];

//Methods Statements
void menu();
void registerStudent();
void deleteStudent();
void reproved();
void search();

int operation;  

int main() {

    menu();
    return 0;
}

//Display menu
void menu(){

    system("cls");   

    while(operation != 5){


        printf("1 - Register Student\n");
        printf("2 - Delete Student\n");
        printf("3 - Reproved Students\n");
        printf("4 - Find Student\n");
        printf("5 - Exit\n");
        scanf("%i", &operation);    

        if(operation == 1){
            registerStudent();
        }   

    };

}

void registerStudent(){

    char name[50];
float grade[4];

int op;
int i;

while(op != 0){

    system("cls");
    printf("Type the name: ");
    //Fgets to read line
    fgets(name, sizeof(name), stdin);
    printf("First two months: ");
    scanf("%f", &grade[0]);
    printf("Second two months: ");
    scanf("%f", &grade[1]);
    printf("Third two months: ");
    scanf("%f", &grade[2]);
    printf("Fouth two months: ");
    scanf("%f", &grade[3]);
    printf("1 - Continue\n");
    printf("0 - Exit\n");

    for(i=0; i<MAX_STUDENT; i++){

        if(students[i].status == 0){

            students[i].grade[0] = grade[0];
            students[i].grade[1] = grade[1];
            students[i].grade[2] = grade[2];
            students[i].grade[3] = grade[3];
            strcpy(students[i].name, name);
            break;
        }
    }

}

}

输出:

enter image description here

PS:现在正在工作!

1 个答案:

答案 0 :(得分:1)

menu() operation中使用未初始化,而registerStudent() op也未初始化。