2013-04-19 15 views
5

यह प्रोग्राम उपयोगकर्ता से इनपुट में इनपुट कैप्चर करना है और फिर दी गई जानकारी के हिस्टोग्राम को मुद्रित करना है। सबकुछ ठीक से काम करता है सिवाय इसके कि जब मैं हिस्टोग्राम मुद्रित करने की कोशिश करता हूं तो सभी '*' वर्ण एफ ग्रेड के नीचे आते हैं, इससे कोई फर्क नहीं पड़ता कि छात्र का ग्रेड क्या है। जो मुझे लगता है वह हो रहा है कि छात्र की सरणी सूचकांक वास्तविक चर के बजाए पारित किया जा रहा है, लेकिन मैं उलझन में हूं क्योंकि हिस्टोग्राम मुद्रित होने से पहले आउटपुट में यह सही मान दिखाता है। कोई सुझाव?संरचना सरणी तत्वों का उपयोग

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

    } 
} 

उत्तर

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

&studentRecords[students] अपने सरणी studentRecords के बाद एक पते है। displayHists में, records[i] तक पहुंच studentRecords[students+i] को अस्वीकार करने का प्रयास करेगी, जो आपके सरणी की सीमाओं के बाहर है।

एक सही कॉल हो सकता है:

displayHist(&studentRecords[0], students); 

कौन सा के बराबर है: वैसे

displayHist(studentRecords, students); 

, char * साथ scanf में & उपयोग करने के लिए है, क्योंकि char (*)[] और char * अलग हो सकता है की कोई जरूरत नहीं स्मृति प्रतिनिधित्व।

+1

Aaaa! लॉल धन्यवाद मुझे यह पता होना चाहिए था। मैं उस गलती के लिए खुद को सिर पर थप्पड़ मार रहा हूं। बहुत बहुत धन्यवाद – 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] 

आप पता-का उपयोग करते हैं, अर्थात &, यह char ** हो जाता है, जो कि नहीं scanf उम्मीद है।

तो इस तरह से उपयोग करने का प्रयास करें।

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

और

displayHist(studentRecords, students); 
+0

ध्यान दें कि यह 'char ** 'नहीं बनता है; यह 'char (*) [28] '(28 अक्षरों की सरणी के लिए एक सूचक) बन जाता है, ठीक उसी तरह त्रुटि संदेश कहता है। आप सही हैं कि यह नहीं है कि 'scanf()' अपेक्षा करता है (लेकिन, उत्सुकता से पर्याप्त, पारित पता वही है)। –

संबंधित मुद्दे