我有一个程序可以接收COSID在100到200之间的学生及其注册的课程的数据文件。它看起来像:100GregSamson3COS301COS431COS490120Jo AnnLyons0
。我有一个动态数组,其中包含该学生正在修读的所有课程。我遇到的问题是,当使用此原型输入课程ID时,要求我打印特定班级的其他人: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++){
}
}
最佳答案
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
参数的学生?关于c++ - C++搜索动态数组,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7933411/