1

Please have a look at below code: I have class facultyType and in main created a array of 250 object of class. in print method passing the Faculty(array of facultyType) but when calling its program getting stopped and no compile error. i want to iterate all facultyType in print method and want to access its member functions. In java doing same working fine but i am afraid why its not working here.

#include<iostream>
#include<string>
using namespace std;
class facultyType
{
private:
    string firstname;
    string lastname;
    string department;
    double salary;
    int serviceyears;
public:
    facultyType(){}
    void print(facultyType* faculty,int count);
    void setAll(facultyType faculty[],float percent,int count);
   //setter and getter
};
void facultyType::print(facultyType* faculty,int count)//need help here
{
    int i;
    for(i=0;i<count;i++)
    {
    //want to iterate all facultyType one by one
        facultyType f=faculty[i];//here the problem

        int serviceYear;
        serviceYear =f.getServiceYears();//not getting the exact values
        cout<<"service year "<<serviceYear<<endl;
        cout<<"dfhh"<<endl;
        if(serviceYear>=15) {
            cout<<f.getFirstName()<<"\n"<<f.getLastName()<<"\n"
                <<f.getDepartment();
            cout<<f.getSalary()<<"\n"<<f.getServiceYears()<<endl;
            cout<<"-------------------------------------------------\n";
        }
    }
}

int main()
{
    facultyType Faculty[250];
    facultyType f;
    int count=0;
    int status=0;
    string fname,lastname,depart;
    double sal;
    int serviceyears;
    while(status!=4)
    {
        cout<<"1. Add new Faculty member"<<endl;
        cout<<"2. increase all faculty member salary"<<endl;
        cout<<"3. Print Employee"<<endl;
        cout<<"4. Exit"<<endl;

        cin>>status;

        switch(status)
        {
            case 1:
            {
              cout<<"Enter first name : ";
                    cin>>fname;
                   //..other code for setter and getter values
                   Faculty[count]=newFaculty;
                   count++;
                    break;
                }

            case 3:{
                f.print(Faculty,count);//calling print method
                break;
            }
             case 4: break;
        }

    }

    return 0;
}

I know its dumbest questions , but i am new in c++. Please explain why its differ than Java. Thanks in advance.

1
  • Have you tried debugging (with gdb for example)? If so, can you tell us if gdb is showing any errors? Try setting some breakpoints in the print function to have proof you are actually getting to this point. Commented May 2, 2017 at 6:35

1 Answer 1

3

The problem is that your "getter" functions do not actually return anything. Before your edit facultyType::setServiceYears was implemented like this:

int facultyType::getServiceYears()
{
    serviceyears;
}

It should have been (adding the return keyword):

int facultyType::getServiceYears()
{
    return serviceyears;
}

This is a type of error that get caught by the compiler when warnings are enabled.

I compiled it on GNU/Linux using: g++ test.cpp -ggdb -O0 -Wall -Wextra -pedantic

Sign up to request clarification or add additional context in comments.

2 Comments

thanks it was a life saver, still i am thinking that much i dumb(;
@Roushan45 No problem. It's good practise to accept the answer that helped you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.