学生信息系统课程分配矢量搜索

时间:2014-05-20 19:38:01

标签: c++ system

假设我有一个包含此数据的文本文档

First Name    Last Name      ID         Tuition   GPA
Alex           Robinson       02341     1400.50   3.45
John           Brown          043261    1200.00   3.21
Rachel         VanGorp        77239     600.00    4.00
James          Smith          89167     1150.55   1.45
Juan           Garcia         23165     1200.00   3.33
Lindsey        McDonald       32501     1340.50   2.25
Tom            Barry          78442     500.00    3.85
Gwen           Poss           30029     1150.50   3.85
Janice         Jones          44560     2200.50   2.45
Jack           Price          99314     1850.55   3.12

我将这个项目列表导入到一个向量中,这是我的学生类和我的classOfStudents类。协助这些信息会很棒。我迷路了,不知道该去哪儿。只是参考和正确方向的一点将是伟大的!

     class student
      {
    public:
        student()
        {
            firstName = "";
            lastName = "";
            ID = "";
            tuitionBalance = 0.0;
    GPA = 0.0;
}
student(string first, string last, string id, double tuition, double gpa)
{
    first = firstName;
    last = lastName;
    id = ID;
    tuition = tuitionBalance;
    gpa = GPA;
}

void openIF()
{
    inFile.open("students.txt");
}
void setFirstName()
{
    inFile >> temp;
    firstName = temp;
}
void setLastName()
{
    inFile >> temp;
    lastName = temp;
}
void setID()
{
    inFile >> temp;
    ID = temp;
}
void setBalance()
{
    inFile >> tuitionBalance;
}
void setGPA()
{
    inFile >> GPA;
}
void closeIF()
{
    inFile.close();
}

string firstName, lastName, ID;
double tuitionBalance, GPA;
    private:

ifstream inFile;
char temp[128];
    }

class classOfStudents
{
public:
    vector<student> students;

    classOfStudents()
    {
        student Students = student(firstName, lastName, ID, tuitionBalance, GPA);
        students.push_back(Students);
    }

    void findStudent()
    {
        int searchID;
        cout << "Enter ID of student" << endl;
        cin >> searchID;
        find(students.begin(), students.end(), searchID);
    }

private:
    string firstName, lastName, ID;
    double tuitionBalance, GPA;
}

如何仅使用学生的ID来提取其余信息?

1 个答案:

答案 0 :(得分:0)

using namespace std;

#include <iostream>
#include <map>

class student
{
public:
string firstName, lastName;
int ID;
double tuitionBalance, GPA;

student(){}

student(string fName, string lName, int id, double tBalance, double gpa)
{
    firstName = fName;
    lastName = lName;
    ID = id;
    tuitionBalance = tBalance;
    GPA = gpa;
}
};

class classOfStudents
{
public:

std::map <int, student> StudentMap;

classOfStudents()
{
    student Students = student("Firstname", "Lastname", 123, 0.00, 4.00);
    StudentMap.insert(std::pair<int, student>(123, Students));
}

void findStudent()
{
    int searchID;
    cout << "Enter ID of student" << endl;
    cin >> searchID;

    if (StudentMap.find(searchID) == StudentMap.end())
    {
        printf("Student not found");
    }
    else
    {
        printf("Name: %s %s ", StudentMap[searchID].firstName, StudentMap[searchID].lastName);
    }
}
};