0

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);

}
user3429735
  • 5
  • 1
  • 2
  • I think you should do studentlist[i].id=temp – Mantosh Kumar Mar 19 '14 at 17:11
  • No @tmp you're wrong. You cannot access a field that is `private` outside of the class. The right syntax is: `studenlist[i].setid(temp);` –  Mar 19 '14 at 17:15
  • Thanks @fiskerXO for correction :)...I just quickly looked it and thought it is ctype struct. – Mantosh Kumar Mar 19 '14 at 17:21
  • No problem:) everybody makes fancy mistakes:D –  Mar 19 '14 at 17:22
  • It seems odd that adding a student "adds" three of them. In a similar approach, a search looks at the first three and a display shows the first three. It might be better to make this a vector and use push_back and erase to achieve dynamic student addition and removal, display the whole "list", and search the container until either 1) the student is found or 2) the search reaches the end. Check out vector details at: http://www.cplusplus.com/reference/vector/vector/ – CPlusPlus OOA and D Mar 19 '14 at 17:32

1 Answers1

3
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
    }
}
Community
  • 1
  • 1
Natan Streppel
  • 5,759
  • 6
  • 35
  • 43