C ++ Поиск динамического массива - PullRequest
0 голосов
/ 28 октября 2011

У меня есть программа, которая принимает файл данных студентов с COSID между 100 и 200 и курсами, в которые они записаны. Похоже: 100 Greg Samson 3 COS301 COS431 COS490 120 Jo Ann Lyons 0.У меня есть динамический массив, который содержит все курсы, которые проходит студент.Проблема, с которой я сталкиваюсь, заключается в том, что меня просят напечатать других людей в определенном классе, когда вводится идентификатор курса с использованием этого прототипа: void printList (ostream& out, FlexArray<Student> majors, int cosID, string course);

Я не совсем уверен, как создать функцию для поискамассив для этого прототипа.Я попытался использовать массив, чтобы все 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++){

    }


}

1 Ответ

2 голосов
/ 28 октября 2011
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, за исключением того, кто COSID является параметром cosID?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...