user3429735
user3429735

Reputation: 5

searching an array of type class

I am working on this C++ program that controls student information. I have been able to add and display the information of the student. I need to search the the array for a specific student by his ID, but it seems there is a problem assigning the array of type class to int variable in order to search for the 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);

}

Upvotes: 0

Views: 393

Answers (1)

Natan Streppel
Natan Streppel

Reputation: 5866

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

studentlist[] is an array of objects of the class student, and temp is a variable of type int. This is invalid. You are trying to assign an uninitialized int, which could have any value, to an array of students.

I believe, by looking at your implementation, that you are looking for something like this (although this search could be improved):

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
    }
}

Upvotes: 3

Related Questions