2016-07-26 5 views
0

Ich bin nicht ganz sicher, was das Problem ist, aber meine Funktion makeArray speichert keine Werte beim Lesen der Datei, und so spuckt das Array nur Müll anstelle der Werte, die ich brauche.Main ruft nicht ordnungsgemäß Funktion "makeArray"

Hier ist meine Funktion

#include <stdio.h> 
#include <stdlib.h> 
#define ROW 12 
#define COL 8 

void makeArray(FILE *infile, int array[][8]) { 
    int i, j; 
    infile = fopen("scores.txt", "r"); 

    for (i = 0; i < ROW; i++) { 
     for (j = 0; j < COL; j++) { 
      fscanf(infile, "%d", &array[i][j]); 
     } 
    } 
    fclose(infile); 
} 

Hier ist Haupt:

int main() { 
    int choice, array[ROW][COL] = { 0 }; 
    FILE *infile; 

    makeArray(infile, array); 

    do { 
     displayMenu(); 
     scanf("%d", &choice); 
     processRequest(array, choice); 
    } while (choice != 0); 

    return 0; 
} 

Dieser gesamte Code ist unten:

#include <stdio.h> 
#include <stdlib.h> 

#define ROW 12 
#define COL 8 

void makeArray(FILE *infile, int array[][8]) { 
    int i, j; 

    infile = fopen("scores.txt", "r"); 

    for (i = 0; i < ROW; i++) { 
     for (j = 0; j < COL; j++) { 
      fscanf(infile, "%d", &array[i][j]); 
     } 
    } 
    fclose(infile); 
} 

int getScore(int array[][8], int month, int game) { 

    int score; 

    array[month-1][game-1] = score; 

    return score; 
} 

int getMonthMax(int array[][8], int month) { 

    int i, max; 

    for (i = 0; i < COL; i++) { 
     if (array[month - 1][i] > max) { 
      max = array[month - 1][i]; 
     } 
    } 
    return max;  
} 

int getYearMax(int array[][8]) { 

    int i, j, max; 

    for (i = 0; i < ROW; i++) { 
     for (j = 0; j < COL; j++) { 
      if (array[i][j] > max) { 
       max = array[i][j]; 
      } 
     } 
    } 
    return max; 
} 

float getMonthAvg(int array[][8], int month) { 

    int i, sum = 0, num = 0, j = 0; 
    float avg; 

    for (i = 0; i < COL; i++) { 
     array[month - 1][i] = num; 
     sum += num; 
     j++;  
    } 
    avg = (sum/j); 
    return avg; 
} 

float getYearAvg(int array[][8]) { 

    int i, j, k, sum = 0, num; 
    float avg; 

    for (i = 0; i < ROW; i++) { 
     for (j = 0; j < COL; j++) { 
      array[i][j] = num; 
      sum += num; 
      k++; 
     } 
    } 
    avg = (sum/k); 
    return avg; 
} 

int toursMissed(int array[][8]) { 

    int i, j, k; 

    for (i = 0; i < ROW; i++) { 
     for (j = 0; j < COL; j++){ 
      if (array[i][j] == 0) 
       k++; 
     } 
    } 
    return k; 
} 

void displayMenu() { 

    int i, com; 

    printf("What would you like to do?\n"); 
    for (i = 0; i < 40; i++) { 
     printf("-"); 
    } 
    printf("\nSelect from option 1-7 or 0 to stop\n"); 
    printf("Select 1 to get the score for a specific game\n"); 
    printf("Select 2 to get the max score for a specific month\n"); 
    printf("Select 3 to get the average score for a specific month\n"); 
    printf("Select 4 to get the max score for the year\n"); 
    printf("Select 5 to get the average score for the year\n"); 
    printf("Select 6 to get the number of tournamnets missed for the year\n"); 
    printf("Select 7 to print all scores for the year\n"); 
    printf("Select 0 to stop\n"); 
    for (i = 0; i < 40; i++) { 
     printf("-"); 
    } 
    printf("\n"); 

} 

void printArray(int array[][8]) { 

    int i, j; 

    for (i = 0; i < ROW; i++) { 
     for (j = 0; j < COL; j++) { 
      printf("%d\t", &array[i][j]); 
     } 
     printf("\n"); 
    } 
} 

void processRequest(int array[][8], int integer) { 

    int f1, f2, f3, f4, f5, f6, f7, f8; 
    int mont, gam; 


    if (integer == 0) { 
     printf("\nThank you! Goodbye\n"); 
    } 
    if (integer == 1) { 
     printf("\nPlease enter the month and the game\n"); 
     scanf("%d%d", &mont, &gam); 
     f1 = getScore(array, mont, gam); 
     printf("\nThe score for Tournament %d is %d", gam, f1); 
    } 
    if (integer == 2) { 
     printf("\nPlease enter the month\n"); 
     scanf("%d", &mont); 
     f2 = getMonthMax(array, mont); 
     printf("\nThe max score for month %d was %d\n", mont, f2); 
    } 
    if (integer == 3) { 
     printf("\nPlease enter the month\n"); 
     scanf("%d", &mont); 
     f3 = getMonthAvg(array, mont); 
     printf("\nThe average score for month %d is %4.2f\n", mont, f3); 
    } 
    if (integer == 4) { 
     f4 = getYearMax(array); 
     printf("\nThe max score for the year is %d\n", f4); 
    } 
    if (integer == 5) { 
     f5 = getYearAvg(array); 
     printf("\nThe average score for the year is %4.2f\n", f5); 
    } 
    if (integer == 6) { 
     f6 = toursMissed(array); 
     printf("\nThe number of tournaments missed for the year is %d\n", f6); 
    } 
    if (integer == 7) { 
     printf("\nThe scores for the year are:\n"); 
     printArray(array); 
    } 
} 

int main() { 

    int choice, array[ROW][COL] = { 0 }; 
    FILE *infile; 

    makeArray(infile, array); 

    do { 
     displayMenu(); 
     scanf("%d", &choice); 
     processRequest(array, choice); 
    } while (choice != 0); 

    return 0; 
} 

nicht sicher, ob es notwendig, alle diese Informationen haben verfügbar, aber es ist jetzt verfügbar.

+0

Nein, das ist nicht das Problem. Es scheint sich gut zu öffnen @ user3121023 – ctelchi

+2

bei 'getScore':' array [monate-1] [spiel-1] = score; '->' score = array [monat-1] [spiel-1]; ' – BLUEPIXY

+2

bei: 'getMonthAvg':' array [i] [j] = num; '->' num = array [i] [j]; ',' avg = (Summe/k); '->' avg = (float) sum/k; ' – BLUEPIXY

Antwort

0

Es gibt mehrere Probleme in Ihrem Code:

  • Das Hauptproblem ist, dass Sie nicht, wenn das Array gelesen wurde richtig sagen kann, weil der Dump-Code in Funktion printArray() falsch ist: printf("%d\t", &array[i][j]); lesen sollte printf("%d\t", array[i][j]);

  • Funktion makeArray nimmt ein FILE *infile Argument, dessen Wert ignoriert wird. Sie sollten dieses Argument entweder als lokale Variable definieren oder die Datei im Aufrufer öffnen und die FILE* übergeben oder möglicherweise den Dateinamen als Argument übergeben.

  • Sie testen keinen der Rückgabewerte für den korrekten Abschluss: fopen kann NULL zurückgeben, wenn die Datei nicht geöffnet werden kann. Die Verwendung eines Stream-Pointers NULL ruft in den meisten Standard-E/A-Funktionen undefiniertes Verhalten auf.

  • Sie testen nicht die Rückgabewerte von scanf() nicht fscanf(). Wenn die Eingabe fehlerhaft ist, ruft das Programm undefiniertes Verhalten auf und der Benutzer wird nicht wissen, wo er nach einer Erklärung suchen soll. Überprüfen Sie alle Rückgabewerte und erstellen Sie klar dokumentierte Fehlermeldungen.

  • Die Funktion getScore ist falsch. Es sollte lauten:

    int getScore(int array[][8], int month, int game) { 
        return array[month - 1][game - 1]; 
    } 
    
  • definieren COL nutzlos ist, wenn man den Wert von COL-8 in Ihren Funktionen codieren: Die Arrays sind definiert mit dem expliziten Konstante anstelle des COL Makro.

  • Funktionen getYearAvg() und getMonthAvg() speichern num in das Array, anstatt es aus dem Array zu lesen. Außerdem wird avg nicht initialisiert auf 0.0.

  • k ist nicht auf 0 in toursMissed() nicht getYearAvg() initialisiert.

  • max sollte in getYearMax() und getMonthMax()

Am Ende zu array[0][0] initialisiert werden, makeArray() die einzige Funktion ist, die korrekte Eingabe bereitgestellt werden nicht versagen.

+0

Vielen Dank !! Nach der Anpassung der & Array wurde alles gelöst. Vielen herzlichen Dank! Ich habe auch alles andere angepasst. – ctelchi

Verwandte Themen