搜索类型类的数组

时间:2014-03-19 17:07:05

标签: c++

我正在研究控制学生信息的C ++程序。我已经能够添加和显示学生的信息。我需要通过他的ID在数组中搜索特定的学生,但似乎将类型类的数组分配给int变量以便搜索ID?

#include <iostream>
#include <string>
#include <array>
#include <iomanip>

using namespace std;

class student{
private:
    int id, age;
    std::string fname, lname;
    string cob;
    char s;
public:

    student()
    {
        id=age=0;
        fname=lname=cob="";
    }

    void setid(int);
    void setage(int);
    void setfname(string);
    void setlname(string);
    void setsex(char);
    void setcob(string);

    int getid();
    int getage();
    string getfname();
    string getlname();
    string getcob();
    char getsex();
};

void student::setfname(string n)
{ fname=n;}
void student::setlname(string n)
{ lname=n;}
void student::setcob(string n)
{ cob=n;}
void student::setid(int n)
{ id=n;}
void student::setage(int n)
{ age=n;}
void student::setsex(char n)
{ s=n;}


string student::getfname()
{ return fname;}
string student::getlname()
{ return lname;}
string student::getcob()
{ return cob;}
int student::getid()
{ return id;}
int student::getage()
{ return age;}
char student::getsex()
{ return s;}


std::array<student, 100> studentlist;

void addstd()
{
    int id, age;
    string fname, lname;
    string cob;
    char s;
    for(int i=0;i<3;i++)
            {
                cout<<"Enter ID Number"<<endl;
                cin>>id;
                studentlist[i].setid(id);
                cout<<"Enter First Name"<<endl;
                cin>>fname;
                studentlist[i].setfname(fname);
                cout<<"Enter last Name"<<endl;
                cin>>lname;
                studentlist[i].setlname(lname);
                cout<<"Enter age"<<endl;
                cin>>age;
                studentlist[i].setage(age);
                cout<<"Enter student's Sex(F or M) "<<endl;
                cin>>s;
                studentlist[i].setsex(s);
                cout<<"Enter Country of birth"<<endl;
                cin>>cob;
                studentlist[i].setcob(cob);
            }
}
void displayinfo()
{
    for(int i=0;i<3;i++)
    {
        cout<<"Student ID:  "<<studentlist[i].getid()<<endl;
        cout<<"First Name:  "<<studentlist[i].getfname()<<endl;
        cout<<"Last Name:   "<<studentlist[i].getlname()<<endl;
        cout<<"Student Age: "<<studentlist[i].getage()<<endl;
        cout<<"Student Gender: "<<studentlist[i].getsex()<<endl;
        cout<<"Student Birth City: "<<studentlist[i].getcob()<<endl;
        cout<< "\n";
    }
}

void searchstd()
{
    int num;
    cout<<"Enter ID of Student "<<endl;
    cin>>num;
    int temp;
    for(int i=0;i<3;i++)
    {
        studentlist[i]=temp; // here is the problem!!
        if(temp==num)
        {

        }
    }
}


/*std::array<student, 100> studentlist;*/

void main()
{
    /*
    student s;
    student std[100];
    */
    student s;

    int choice;

    do{

    cout << "-----------Menu------------" << endl;
        cout << "  1. Add a student " << endl;
        cout << "  2. Search for student by ID " << endl;
        cout << "  3. Display all students information   " << endl;
        cout << "  4. Remove a students  " << endl;
        cout << "  5. Display students aged between 34 - 50  " << endl;
        cout << "  6. Modify a student's information         " << endl;
        cout << "  7. Exit         " << endl;
        cout << "  Enter your choice 1, 2, 3, 4 ,5,6,7   " << endl;
    cin>>choice;

    switch(choice) {
        case 1:
            addstd();
           break;
        case 2:
            searchstd();
           break;
        case 3:
            displayinfo();
           break;
        case 4:
            break;
        case 5:
           break;
        case 6:
           break;
        case 7:
            exit(0);
            break;
        default:
           cout << "Please enter 1, 2, 3, 4, 5, 6 or 7 : " << endl << endl;
    }

    }while(choice!=8);

}

1 个答案:

答案 0 :(得分:3)

studentlist[i]=temp; // here is the problem!!

studentlist[]是类student的对象数组,tempint类型的变量。这是无效的。您正尝试将uninitialized int(可能具有任何值)分配给students数组。

我相信,通过查看您的实现,您正在寻找类似的内容(尽管可以改进此搜索):

int num;
cout<<"Enter ID of Student "<<endl;
cin>>num;

for(int i=0;i<3;i++)
{
    if(studentlist[i].getid() == num)
    {
        // found student with id == num
    }
}