【问题标题】:C++ Searching a dynamic arrayC ++搜索动态数组
【发布时间】:2011-10-28 18:39:31
【问题描述】:

我有一个程序接收 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);

我不太确定如何创建函数来在数组中搜索此原型。我尝试对数组进行整合,因此所有 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++){

    }


}

【问题讨论】:

  • 是动态数组吧?为什么不像搜索任何其他数组那样搜索 - 循环并进行比较。
  • 我不知道FlexArray 来自哪里,但它肯定提供了一种迭代其数据的方法。

标签: c++ arrays search


【解决方案1】:
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&amp; out, FlexArray&lt;Student&gt; majors, int cosID, string course);这是否意味着打印所有正在接受course的学生,除了COSID是谁的cosID参数?

【讨论】:

  • `这是否意味着打印所有正在上课的学生,除了谁的COSID是cosID参数?`是的,完全正确。
  • 第一个 forloop iStudent s; for (int i = 0; i<101; i++){ if(s.COSID != cosID){ for(int j=0; j<=s.totCourses ;j++){ if(s.coursearr[j] == course){ cout << "\n" << s.fname << " " << s.lname; } } } }
  • 在您的情况下,第一个 for 循环是 for(int i=0; i&lt;101; ++i) if(courses[i].COSID != -1) { ... 您必须检查学生是否存在。您的代码也没有分配s
  • 我想我明白了,谢谢你的帮助!
猜你喜欢
  • 2016-05-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-16
  • 2015-03-27
  • 1970-01-01
相关资源
最近更新 更多