2017-11-10 3 views
-1

Ich muss die Nummern der Schüler eingeben, dann markiert ihr Name dann. Aber wenn ich die Markierungen eintippe, stürzt das Programm ab. Ich weiß nicht warum, wenn ich in die letzte Loop-Funktion eingebe, stürzt es mein Programm ab. Vielleicht weil ich zu viel verschachtelt habe?For Loop-Funktion stürzt bei Eingabe ab

#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') 
    { 
     ; 
    } 

} 

Alles funktioniert gut, bis ich die letzte Loop-Funktion eingeben, es stürzt ab. Habt ihr irgendwelche Ideen warum? Danke, dass Sie mir helfen wollen.

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 Namen [Studenten] [NAME_LEN];' sollte nach 'Studenten = noOfStud();' sein. – mch

+0

'scanf ("% d ", & Optionen);' -> 'Optionen' ist vom Typ' char'. Dies sollte 'scanf ("% c ", & Optionen) sein;' –

Antwort

0

Sie sollten doppelte Eingabe mit %lf Specifier nehmen.

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

Auch scanf("%c",&options); weil Optionen sind vom Typ char.

Verwendung von falsch Format Specifier führt zu undefiniertem Verhalten.

In der Hauptfunktion initialisieren Sie das Array mit einigen nicht initialisierten Variablen.

Sie sollten es in der richtigen Reihenfolge platzieren. wie unten

int main(void) 
{ 
    int students; 

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


    return 0; 
} 
+0

Es stürzt immer noch –

+0

@nikunikuD .: Überprüfen Sie einmal ... es funktioniert. Was ist dein Beitrag? – coderredoc

+0

Vielen Dank. –

0

Sind gewisse Modifikation in main() Funktion:

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

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


    return 0; 
} 

gewisse Modifikation in courseWork1 Do() Funktion, wie unten:

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(); 
    } 

} 

und in switchOptions() Funktion übernehmen Optionsvariable als Integer-Typ

Verwandte Themen