2017-03-05 62 views
-1

我想调用一个函数,它有一个IF语句,但我必须比较一个ID号与所有的动态数组students[i].idnum,但是当我运行函数全部我得到的是checkValue == false输出。我相信可能有3个原因,因为这是失败的,但我试图弄清楚它有点失落。函数IF语句中访问结构成员

  1. MAX_STUDENT是0
  2. MAX_GRADE是0
  3. idnumb不等于学生[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"; 
      } 
     } 
}; 

任何帮助,这将不胜感激。

输出 - 2测试的学生:

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

你介意张贴[最小,完整,可验证的示例](http://stackoverflow.com/help/mcve)与所需的夹杂物头文件,结构和调用者代码的定义(可能是'main()'函数)? – MikeCAT

+0

你有原因的候选人,所以请尝试使用你的调试器来检查原因是否真的存在。 – MikeCAT

+0

由于您没有学生(MAX_STUDENT为0),因此您可以正确跳过for循环并正确地只获取checkValue == falseoutput。也许你想尝试一个让学生去搜索的测试用例。 –

回答

0

你不可以访问什么是&students[MAX_STUDENT]指出,这是超出范围,或更高版本,因为只有MAX_STUDENT元素被分配到由students指向哪里。

尝试改变

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

StudentIdSearch(MAX_STUDENT,students,MAX_GRADE); 
+0

这是问题所在。 花了我一点时间,看看你指的是函数的实际调用,而不是函数的原型/定义。 不过谢谢你:-) – XmalevolentX