2017-11-10 3 views
-1

학생 번호를 입력 한 다음 이름과 마크를 입력해야합니다. 그러나 마크를 입력하면 프로그램이 충돌합니다. 왜 내가 마지막 루프 기능에 들어가면 내 프로그램이 충돌하는지 모르겠다. 어쩌면 내가 너무 많이 중첩했기 때문에?루프 기능이 입력되면 오류가 발생합니다.

#include <stdio.h> 
    #include <string.h> 
    #define NAME_LEN 25 

    int noOfStud(void); 
    void listStudents(int noOfStud, char names[][NAME_LEN]); 
    void options(int noOfStud, double marks[]); 
    void switchOptions(int noOfStud, double marks[]); 
    void courseWork1(int noOfStud, double marks[]); 
    void emptyBuffer(void); 
int main(void) 
{ 
    int students; 
    char names[students][NAME_LEN]; 
    double marks[students]; 
    students = noOfStud(); 

    listStudents(students, names); 
    options(students, marks); 


    return 0; 
} 

int noOfStud(void) 
{ 
    int students; 
    printf("Number of students: "); 
    scanf("%d", &students); 
    getchar(); 
    return students; 
} 

void listStudents(int noOfStud, char names[][NAME_LEN]) 
{ 
    int i; 
    for(i=0; i<noOfStud; i++) 
    { 
     printf("Enter name: "); 
     scanf("%[^\n]", names[i]); 
     getchar(); 
    } 
} 

void options(int noOfStud, double marks[]) 
{ 
    printf("1. Enter marks for course work 1\n"); 
    printf("2. Enter marks for course work 2\n"); 
    printf("3. Enter makrs for course work 3\n"); 
    printf("4. Display a particular student's marks\n"); 
    printf("5. Supervisor mode\n"); 
    printf("6. Exi program\n"); 

    switchOptions(noOfStud, marks); 
} 

void switchOptions(int noOfStud, double marks[]) 
{ 
    char options; 

    printf("\nPlease enter a number for which choice you want: "); 
    scanf("%d", &options); 
    emptyBuffer(); 
    switch(options) 
    { 
     case 1: 
      printf("Thank you for chosing to enter marks for course work 1!\n"); 
      courseWork1(noOfStud,marks); 
      break; 
     case 2: 
      printf("Thank you for chosing to enter marks for course work 2!\n"); 
      break; 
     case 3: 
      printf("Thank you for chosing to enter marks for course work 3!\n"); 
      break; 
     case 4: 
      printf("work 4"); 
      break; 
     case 5: 
      printf("work 5"); 
      break; 
     case 6: 
      printf("work 6"); 
      break; 
     default: 
      printf("You didn't enter anything"); 
    } 
} 

void courseWork1(int noOfStud, double marks[]) 
{ 
    int i; 
    for(i=0; i<noOfStud; i++) 
    { 
     printf("Enter marks: "); 
     scanf("%d", &marks[i]); 
     getchar(); 
    } 

} 

void emptyBuffer(void) /* Empty the keyboard buffer */ 
{ 
    while(getchar() != '\n') 
    { 
     ; 
    } 

} 

마지막 루프 기능을 입력하기 전까지는 문제가 없습니다. 충돌이 발생합니다. 너 왜 그런 생각이있어? 도와 주셔서 감사합니다.

void courseWork1(int noOfStud, double marks[]) 
{ 
    int i; 
    for(i=0; i<noOfStud; i++) 
    { 
     printf("Enter marks: "); 
     scanf("%d", &marks[i]); 
     getchar(); 
    } 

} 
+0

http://idownvotedbecau.se/nomcve/ – klutt

+0

'char names [students] [NAME_LEN];은'students = noOfStud();'뒤에 와야합니다. – mch

+0

'scanf ("% d", & options);'->'options'는'char' 타입입니다. 'scanf ("% c", & options);' –

답변

0

%lf 지정자로 두 번 입력해야합니다.

scanf("%lf",&marks[i]);

또한 옵션은 char 형이다 scanf("%c",&options); 때문이다.

을 사용하면 잘못된 형식 인은 정의되지 않은 동작으로 이어집니다.

main 함수에서 일부 초기화되지 않은 변수로 배열을 초기화합니다.

올바른 순서로 배치해야합니다.

int main(void) 
{ 
    int students; 

    students = noOfStud(); 
    char names[students][NAME_LEN]; 
    double marks[students]; 
    listStudents(students, names); 
    options(students, marks); 


    return 0; 
} 
+0

여전히 충돌이 발생합니다. –

+0

@nikunikuD .: 한번 확인하십시오. 작동하고 있습니다. 당신의 의견은 무엇입니까? – coderredoc

+0

고맙습니다. –

0

아래 main() 함수에서 일부 수정 수행 아래

int main(void) 
{ 
    int students = noOfStud(); 
    char names[students][NAME_LEN]; 
    double marks[students]; 
    // students = noOfStud(); 

    listStudents(students, names); 
    options(students, marks); 


    return 0; 
} 

이 (courseWork1에 약간의 수정 작업을 수행)를 기능 :

void courseWork1(int noOfStud, double marks[]) 
{ 
    int i; 
    for(i=0; i<noOfStud; i++) 
    { 
     printf("Enter marks: "); 
     //scanf("%d", &marks[i]); 
     scanf("%lf", &marks[i]); 
     getchar(); 
    } 

} 

및 switchOptions에서 (

) 기능을 가지고 options 변수를 정수형으로 사용합니다.