2017-03-05 3 views
-1

J'essaie d'appeler une fonction qui contient une instruction IF, mais je dois comparer un numéro d'ID à toutes les tables dynamiques students[i].idnum, mais lorsque j'exécute la fonction Je reçois est la sortie checkValue == false. Je crois qu'il peut y avoir 3 raisons car cela échoue, mais je me perds un peu en essayant de le comprendre.Accès aux membres de la structure dans une instruction IF des fonctions

  1. MAX_STUDENT est 0
  2. MAX_GRADE est 0
  3. idnumb ne sont pas égaux aux étudiants [i] .idnum
#include<iostream> 
#include<fstream> 
#include<string> 
#include<cstring> 

using namespace std; 

char GradeLetter(double value); 

struct Student 
{ 
    string name; 
    int idnum; 
    double *tests; 
    double average; 
    char grade; 
}; 
void StudentIdSearch(int &MAX_STUDENT, Student students[], int &MAX_GRADE); 

int main() 
{ 
    int MAX_GRADE, MAX_STUDENT,choice; 

    cout<<"Please enter the number of tests: "; 
    cin>>MAX_GRADE; 
    cout<<"Please enter the number of students: "; 
    cin>>MAX_STUDENT; 
    cin.ignore(); 

    Student *students = new Student[MAX_STUDENT]; 

    for(int i = 0; i <= MAX_STUDENT-1; i++) 
    { 
     cout<<"\nPlease enter the name for student "<<i+1<<": "; 
      getline(cin,students[i].name); 
     cout<<"\nPlease enter the ID number for student "<<i+1<<": "; 
      cin>>students[i].idnum; 
     students[i].tests = new double[MAX_GRADE]; 
     cout<<"\nPlease enter the grades for student "<<i+1<<": "<<endl; 
     for(int j = 0; j <= MAX_GRADE-1; j++) 
     { 
      cout<<"Grade "<<j+1<<": "; 
       cin>>students[i].tests[j]; 
      students[i].average += students[i].tests[j]; 
      if(j == MAX_GRADE-1) 
      { 
       students[i].average = students[i].average/MAX_GRADE; 
       students[i].grade = GradeLetter(students[i].average); 
      } 
     } 
     cin.ignore(); 
    } 

do 
{ 
    cout<<"\n\nMENU"; 
    cout<<"\n1. Search for a student with a particular ID and output the student's information."; 
    cout<<"\n2. Search for a student with a particular Name and output the students information."; 
    cout<<"\n3. Output students of a particular grade (A,B,C,D,F) to a file."; 
    cout<<"\n4. Sort the students according to the average and output the students into an output file."; 
    cout<<"\n5. Exit"; 
    cout<<"\n\nEnter your choice 1-5 :"; 
    cin>>choice; 

    switch(choice) 
    { 
     case 1:  StudentIdSearch(MAX_STUDENT,&students[MAX_STUDENT],MAX_GRADE); 
      break; 
//  case 2: 
//   break; 
//  case 3: 
//   break; 
//  case 4: 
//   break; 
     case 5: break; 
      default:cout<<"\nInvalid choice"; 
    } 
    }while(choice!=5); 

    return 0; 
} 

char GradeLetter(double value) 
{ 
    char A,B,C,D,F; 

    if(value >= 91 && value <= 100) 
     return 'A'; 
    if(value >= 81 && value < 91) 
     return 'B'; 
    if(value >= 71 && value < 81) 
     return 'C'; 
    if(value >= 61 && value < 71) 
     return 'D'; 
    if(value <= 60) 
     return 'F'; 
}; 

void StudentIdSearch(int &MAX_STUDENT ,Student students[], int &MAX_GRADE) 
{ 
    int idnumb; 
    cout<<"Please enter the ID number of the student: "; 
    cin>>idnumb; 
    for(int i=0;i<MAX_STUDENT;i++) 
     { 
      if(idnumb == students[i].idnum) 
      { 
       for(int j=0;j<MAX_GRADE;j++) 
       { 
        cout<<"\nInformation about the requested student"<<endl; 
        cout<<"Name: "<<students[i].name<<endl; 
        cout<<"ID Number: "<<students[i].idnum<<endl; 
        cout<<"Grade "<<j+1<<": "<<students[i].tests[j]<<endl; 
        cout<<"GPA: "<<students[i].average<<endl; 
        cout<<"Course Grade: "<<students[i].grade<<endl; 
       } 
      } 
      else 
      { 
       cout<<"No Student Found"; 
      } 
     } 
}; 

Toute aide à ce serait apprécié.

sortie - 2 Test étudiants:

Please enter the number of tests: 2 
Please enter the number of students: 2 

Please enter the name for student 1: Student1 

Please enter the ID number for student 1: 1234 

Please enter the grades for student 1: 
Grade 1: 100 
Grade 2: 90 

Please enter the name for student 2: Student2 

Please enter the ID number for student 2: 4321 

Please enter the grades for student 2: 
Grade 1: 70 
Grade 2: 80 


MENU 
1. Search for a student with a particular ID and output the student's information. 
2. Search for a student with a particular Name and output the students information. 
3. Output students of a particular grade (A,B,C,D,F) to a file. 
4. Sort the students according to the average and output the students into an output file. 
5. Exit 

Enter your choice 1-5 :1 
Please enter the ID if the student you wish to look up: 1234 
No Student Found 

MENU 
1. Search for a student with a particular ID and output the student's information. 
2. Search for a student with a particular Name and output the students information. 
3. Output students of a particular grade (A,B,C,D,F) to a file. 
4. Sort the students according to the average and output the students into an output file. 
5. Exit 

Enter your choice 1-5 : 
+1

Pourriez-vous postant un [exemple minimal, complet et Vérifiable] (http://stackoverflow.com/help/mcve) avec des inclusions nécessaires des en-têtes, des définitions de structures et du code appelant (peut-être la fonction 'main()')? – MikeCAT

+0

Vous avez des candidats de cause, essayez donc d'utiliser votre débogueur pour vérifier si les causes existent vraiment. – MikeCAT

+0

Puisque vous n'avez aucun étudiant (MAX_STUDENT vaut 0), vous sautez correctement la boucle for et vous obtenez correctement la valeur checkValue == falseoutput. Peut-être que vous voulez essayer un cas de test que les étudiants doivent rechercher. –

Répondre

0

Vous ne devez pas accéder à ce qui est fait par &students[MAX_STUDENT], qui est hors de portée, ou plus tard parce que MAX_STUDENT éléments sont attribués à l'endroit où est pointé par students.

Essayez de changer

StudentIdSearch(MAX_STUDENT,&students[MAX_STUDENT],MAX_GRADE); 

à

StudentIdSearch(MAX_STUDENT,students,MAX_GRADE); 
+0

C'était le problème. m'a pris un peu de temps pour voir que vous faisiez référence à l'appel réel de la fonction et non au prototype/définition de la fonction. Mais merci tout de même :-) – XmalevolentX