2013-04-19 18 views
5

Ten program ma przechwycić dane wejściowe od użytkownika do struktury, a następnie wydrukować histogram podanych informacji. Do tej pory wszystko działa dobrze, z tą różnicą, że kiedy próbuję wydrukować histogram, wszystkie znaki "*" należą do klasy F bez względu na ocenę ucznia. To co myślę dzieje się to, że indeks tablicy ucznia jest przekazywany zamiast samej zmiennej, ale jestem zdezorientowany, ponieważ na wydruku przed wydrukowaniem histogramu pokazuje on poprawną wartość. Jakieś sugestie?Dostęp do elementów tablicy struct

#include <stdio.h> 
#include <stdlib.h> 
#define MAXSIZE 28 
#define MAXGRADE 100 

struct studentData{ 
    char studentID[MAXSIZE]; 
    char studentName[MAXSIZE]; 
    int examPercent; 
} studentRecords[MAXSIZE]; 

// function prototype for histogram 
void displayHist(struct studentData *records, int classSize); 

int main() 
{ 
    int i, students = -1; 
    //struct studentData *studentRecords[MAXSIZE]; 
    while(students < 0 || students > MAXSIZE) 
    { 
     printf("Please enter the number of students in your class:\n"); 
     scanf("%d", &students); 

     if(students > MAXSIZE || students <= 0) 
     { 
      printf("Try again..\n"); 
      scanf("%d", &students); 
     } 

    } 

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

     printf("Please enter the student #%d's lastname:\n", i+1); 
     scanf("%s", &studentRecords[i].studentID); 


     printf("Please enter the student #%d's ID#:\n", i+1); 
     scanf("%s", &studentRecords[i].studentName); 

     printf("Please enter the student's exam percent:\n"); 
     scanf("%d", &studentRecords[i].examPercent); 

} 

//This is just here to view the input... 
for(i=0;i<students;i++) { 

     printf("Student #%d's name is %s\n", i+1, studentRecords[i].studentName); 
     printf("student #%d's ID#:%s\n", i+1, studentRecords[i].studentID); 
     printf("student #%d's grade was %d\n", i+1, studentRecords[i].examPercent); 

    } 

    displayHist(&studentRecords[students], students); 

    return 0; 
} 

void displayHist(struct studentData *records, int classSize) 
{ 
    int i; 


     printf("A:"); 
    for(i=0;i<classSize;i++) 
    { 

     if(records[i].examPercent >=90) 
     { 
      printf("*"); 
     } 

    } 


     printf("\n"); 
     printf("B:"); 
    for(i=0;i<classSize;i++) 
    { 
     if(records[i].examPercent< 90 && records[i].examPercent >= 80) 
     { 
      printf("*"); 
     } 
    } 

     printf("\n"); 
     printf("C:"); 
    for(i=0;i<classSize;i++) 
    { 
     if(records[i].examPercent < 80 && records[i].examPercent >= 70) 
     { 
      printf("*"); 
     } 

    } 

    printf("\n"); 
    printf("D:"); 
    for(i=0;i<classSize;i++) 
    { 
     if(records[i].examPercent< 70 && records[i].examPercent >= 60) 
     { 
      printf("*"); 
     } 

    } 

    printf("\n"); 
    printf("F:"); 
    for(i=0;i<classSize;i++) 
    { 
     if(records[i].examPercent < 60) 
     { 
      printf("*"); 
     } 

    } 
} 

Odpowiedz

2
displayHist(&studentRecords[students], students); 

&studentRecords[students] to adres po tablicy studentRecords. W wersji displayHists, dostęp do records[i] spowoduje usunięcie dereferencji studentRecords[students+i], która znajduje się poza granicami twojej tablicy.

Prawidłowe połączenia mogą być:

displayHist(&studentRecords[0], students); 

co jest równoważne:

displayHist(studentRecords, students); 

Nawiasem mówiąc, nie ma potrzeby korzystania & w scanf z char *, ponieważ char (*)[] i char * może mieć różny reprezentacje pamięci.

+1

Aaaa! Lol, dzięki, powinienem to wiedzieć. Uderzam się w głowę za ten błąd LoL. Wielkie dzięki – KryptNick

0
scanf("%s", &studentRecords[i].studentID); 

scanf("%s", &studentRecords[i].studentName); 

warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[28]’ [-Wformat] 

Podczas korzystania z adresu, tj & staje char **, który nie jest co scanf oczekuje.

Spróbuj użyć tej metody.

scanf("%s", &(*studentRecords[i].studentID)); 

i

displayHist(studentRecords, students); 
+0

Pamiętaj, że nie zmienia się w "char **"; staje się "char (*) [28]" (wskaźnik do tablicy składającej się z 28 znaków), dokładnie tak, jak stwierdza komunikat o błędzie. Masz rację, że nie jest to coś, czego 'scanf()' oczekuje (ale, co ciekawe, przekazany adres jest taki sam). –

Powiązane problemy