2011-10-28 59 views
0

我有一個程序,它接收COSID在100到200之間的學生的數據文件以及它們註冊的課程。它看起來像:100 Greg Samson 3 COS301 COS431 COS490 120 Jo Ann Lyons 0。我有一個動態數組,包含學生正在學習的所有課程。我遇到的問題是當我使用這個原型輸入一個課程ID時,我被要求打印其他人:void printList (ostream& out, FlexArray<Student> majors, int cosID, string course); C++搜索動態數組

我不太確定如何創建函數來搜索數組樣機。我試圖利用這個數組,所以所有的COSID都是-1,所以當試圖搜索時,我應該更容易找到有信息的元素,但也無法獲得這些工作。這裏是我的代碼迄今T:

#include <iostream> 
#include <string> 
#include <iomanip> 
#include <fstream> 
#include "templates.h" 


using namespace std; 

struct Student { 

    int COSID; 
    string fname; 
    string lname; 
    int totCourses; 
    string * coursearr; 
}; 
void printList (ostream& out, FlexArray<Student> majors, int cosID, string course); 

int main(){ 
    Student s; // Object for struct student 
    int searchCOSID; //varible to find COS id 
    int Loopcheck = -1; //break loop variable 
    string LoopCOSID = ""; //break loop variable 
    FlexArray<Student> fa(100,200); //object for flex array setting Upper bounds to 200 and lower bounds to 100 

    //Initilize the flexarray cosid to -1; 
    for(int i=100; i<=200; i++){ 
     fa[s.COSID = -1]; 
    } 
    cout << fa[105].COSID; 
    char c; 


    ifstream fin;    // declare input file stream object 
    fin.open ("a5.txt"); 
    fin >> s.COSID; 
    fin.get(c); 
    getline(fin, s.fname); 
    getline(fin, s.lname); 
    fin >> s.totCourses; 
    while(!fin.fail()){ 
     s.coursearr = new string[s.totCourses]; 
     if(s.totCourses > 0){ 
     for(int i=0; i<s.totCourses; i++){ 
      fin >> s.coursearr[i]; 
     } 
     } 
     fa[s.COSID] = s; 
     fin >> s.COSID; 
     fin.get(c); 
     getline(fin, s.fname); 
     getline(fin, s.lname); 
     fin >> s.totCourses; 
    } 



     cout << "\nEnter your COS ID: "; 
     cin >> searchCOSID; 
     Student currstudent = fa[searchCOSID]; 
     if(currstudent.COSID=-1){ 
      cout << "ID not asscoaited with a student"; 
     }else{ 
      cout << "The courses taken this semester by " << currstudent.fname << " " << currstudent.lname << " include:"; 
      if(currstudent.totCourses > 0){ 
       for(int i=0; i<currstudent.totCourses;i++){ 
       cout << "\n" << currstudent.coursearr[i]; 
       } 
      } 
      else{cout << endl <<"No courses";} 
     } 



    while(Loopcheck == -1){ 
     cout << "\n\nEnter a couses (Q) to quit: "; 
     cin >> LoopCOSID; 
     if(LoopCOSID == "Q" || LoopCOSID=="q"){ 
      Loopcheck = 0; 
     }else{ 
      string course; 
      cout << endl << "Other students taking this course include: "; 
      //printList(cout,FlexArray<Student>,searchCOSID,coursearr); 
     } 

    } 
    return 0; 


} 

void printList (ostream& out, FlexArray<Student> majors, int cosID, string course){ 
    for (int i = 0; i<101; i++){ 

    } 


} 
+0

這是一個動態數組,對吧?爲什麼不像你一樣搜索任何其他數組 - 循環並做比較。 –

+0

我不知道FlexArray來自哪裏,但肯定它提供了一種迭代其數據的方法。 –

回答

2
For each student s in majors (silly name for a list of students isn't it?) 
    if s.COSID is not cosID 
     for each class c that s is taking 
      if c equals course 
       display s.fname 

我想。很難說。 print other people in a paticular class when a course ID is inputed using this prototype: void printList (ostream& out, FlexArray<Student> majors, int cosID, string course);這是否意味着打印所有正在服用course的學生,除非COSIDcosID參數?

+0

'這是否意味着打印所有正在參加課程的學生,除了COSID是cosID參數?'是的。 – Nick

+0

好的,這就是我發佈的算法。 –

+0

我的第一個forloop應該是什麼?這就是我所擁有的''學生的; \t對(INT I = 0; I <101;我++){ \t \t如果(s.COSID = cosID!){ \t \t \t對(INT J = 0;Ĵ<= s.totCourses; J ++){ \t \t \t \t如果(s.coursearr [J] ==當然){ \t \t \t \t \t COUT << 「\ n」 個<< s.fname << 「」 << s.lname; \t \t \t \t} \t \t \t} \t \t} \t} '' – Nick