Return an array from a function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Hypnotik
    New Member
    • Jun 2007
    • 87

    Return an array from a function

    Hello everyone. I am writing a program which takes in information to various arrays. I input the info using "set" functions. I then need to output that info from an output function. I'm having problems getting to the information. I think I need to return a pointer to the beginning of the array, although I'm having alot of problems.

    Code:
    #include <iostream>
    using namespace std;
    struct Person
    {
    	char Fname[20],Lname[20];
    	float age;
    	int ssn[9],enumber[9];
    };
    class Employee
    {
    private:
    	int enumber[9],ssn[9];
    	float wage;
    	char job_title[10];
    	Person Per;
    public:
    	Employee();
    	~Employee(){};
    	void SetFname(int emp);
    	void SetLname(int emp);
    	void Setage();
    	void Setssn(int emp);
    	void Setenumber(int emp);
    	void Setwage();
    	void Setjob_title();
    	char GetFname();
    	char GetLname();
    	void Getage();
    	int Getssn();
    	int Getenumber();
    	void Getwage();
    	void Getjob_title();
    	void output();
    	void salary();
    	void flipflop();
    };
    Employee::Employee()
    {
    	int i=0,j=0;
    	cout<<"Constructor."<<endl;
    	wage=0;
    
    	for (i=0;i<=9;++i)
    	{
    		enumber[i]=0;
    		ssn[i]=0;
    	}
    
    }
    void Employee::SetFname(int emp)
    {	
    	cout<<"BEGIN SETFNAME FUNCTION."<<endl;
    	cout<<"Enter the first name of the employee."<<endl;
    	cin.ignore();
    	cin.getline (Per.Fname,20,'\n');
    	cout<<Per.Fname<<endl;
    }
    void Employee::SetLname (int emp)
    {
    	cout<<"BEGIN SETLNAME FUNCTION."<<endl;
    	cout<<"Enter the last name of the employee."<<endl;
    	cin.getline (Per.Lname,20,'\n');
    	cout<<Per.Lname<<endl;
    }
    void Employee::Setssn(int emp)
    {	
    	cout<<"SETSSN Function"<<endl;
    	cout<<"Enter the social security number of the employee."<<endl;
    	cin>>Per.ssn[emp];
    	cout<<Per.ssn[emp]<<endl;
    	
    }
    void Employee::Setenumber(int emp)
    {
    	int i;
    	cout<<"SETENUMBER FUNCTION."<<endl;
    	cout<<"Enter the employee number."<<endl;
    	cin>>Per.enumber[emp];
    	cout<<Per.enumber[emp]<<endl;
    }
    void Employee::output()
    {
    	int emp;
    	cout<<"Which employee's information would you like to view?"<<endl;
    	cin>>emp;
    	cout<<"Last name:        "<<GetLname()<<endl;
    	cout<<"First name:       "<<GetFname()<<endl;
    	cout<<"Employee number:  "<<Getenumber()<<endl;
    	cout<<"Employee ssn:		 "<<Getssn()<<endl;
    }
    int Employee::Getssn()
    {
    	cout<<"Getssn function"<<endl;
    	return (0);
    	//return (Per.ssn[value]);
    }
    int Employee::Getenumber()
    {
    	cout<<"Getenumber function"<<endl;
    	return(0);
    	//return (Per.age);
    }
    char Employee::GetLname()
    {
    	cout<<"GetLname function"<<endl;
    	return (0);
    	//return(Per.Lname);
    }
    char Employee::GetFname()
    {
    	cout<<"GetFname function"<<endl;
    	return (0);
    	//return (Per.Fname);
    }
    void main()
    {
    	int emp=-1,choice;
    	Employee empl[10];
    	while(emp<0||emp>9)
    	{
    	cout<<"Choose and employee 0-9"<<endl;
    	cin>>emp;
    	}
    	do
    	{
    	cout<<"Please select what you would like to edit from the menu:"<<endl;
    	cout<<"1) Enter the first name."<<endl<<"2) Enter the last name."<<endl<<"3) Enter the social security number."<<endl;
    	cout<<"4) Enter the employee number."<<endl<<"5) Enter all of the information."<<endl<<"6) Output the information for employee."<<endl;
    	cin>>choice;
    		switch(choice)
    		{
    		case 1:
    			empl[emp].SetFname(emp);
    			break;
    		
    		case 2:
    			empl[emp].SetLname(emp);
    			break;
    
    		case 3:
    			empl[emp].Setssn(emp);
    			break;
    
    		case 4:
    			empl[emp].Setenumber(emp);
    			break;
    		
    		case 5:
    			cout<<"Please enter all of the employees information."<<endl;
    			empl[emp].SetFname(emp);
    			empl[emp].SetLname(emp);
    			empl[emp].Setssn(emp);
    			empl[emp].Setenumber(emp);
    			break;
    
    		case 6:
    			cout<<"The employees information is: "<<endl;
    			empl[emp].output();
    			break;
    		}
    	}
    	while (choice<1||choice>6);
    }
    Thanks,
    J
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    This code:
    Originally posted by Hypnotik
    void Employee::outpu t()
    {
    int emp;
    cout<<"Which employee's information would you like to view?"<<endl;
    cin>>emp;
    cout<<"Last name: "<<GetLname()<< endl;
    cout<<"First name: "<<GetFname()<< endl;
    cout<<"Employee number: "<<Getenumber() <<endl;
    cout<<"Employee ssn: "<<Getssn()<<en dl;
    }
    is an Employee member function. You have access to Per:
    [code=cpp]
    void Employee::outpu t()
    {
    int emp;
    cout<<"Which employee's information would you like to view?"<<endl;
    cin>>emp;
    cout<<"Last name: "<<Per.Lname<<e ndl;
    cout<<"First name: "<<Per.Fname()< <endl;
    cout<<"Employee number: "<<Per.enumber( )<<endl;
    cout<<"Employee ssn: "<<Per.ssn()<<e ndl;
    }
    [/code]

    Besides your Employee::GetLn ame() does not return the name. It returns a binary zero. Other get member functions do the same.

    This one:
    Originally posted by Hypnotik
    void Employee::SetFn ame(int emp)
    {
    cout<<"BEGIN SETFNAME FUNCTION."<<end l;
    cout<<"Enter the first name of the employee."<<end l;
    cin.ignore();
    cin.getline (Per.Fname,20,' \n');
    cout<<Per.Fname <<endl;
    }
    doesn't use the int argment. Why have it?

    Also, your functions a loaded with displays and that tells me you are not using your debugger. Learn to use it.

    Fix these and then see where you are.

    Comment

    • Hypnotik
      New Member
      • Jun 2007
      • 87

      #3
      So within my output function I should be accessing the get functions by using (for example) per.getssn()? I have them returning zero because I couldn't get them to return the correct value, and it was my way of troubleshooting to ensure that a value was being returned.

      As for the debugger, this is my second c++ class and my instructor has not mentioned using it at all. I plan to ask him in our next meeting about debugger. I attempted to use it...and really didn't understand what was going on with it.

      Thanks,

      J

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Originally posted by Hypnotik
        So within my output function I should be accessing the get functions by using (for example) per.getssn()? I have them returning zero because I couldn't get them to return the correct value, and it was my way of troubleshooting to ensure that a value was being returned.
        That is correct. Your get functions need to return a copy of the class private data member. This is a little tough when the class data member is an array since you can't return an array from a function.

        What you can do is return the array name:
        [code=cpp]
        char* Employee::GetFn ame
        {
        return Fname;
        }
        [/code]

        This works because the array name is the address of element 0. Element 0 of the Fname array is a char so the name Fname is the address of a char, that is, a char*.

        Unfortunately, this exposes the array to the outside world allowing some hacker-type to use the returned char* to change the contents of the array without needing to use your member functions. Cases like this require returning a constant char* so prevent this from happening.

        The correct code would be:
        [code=cpp]
        const char* Employee::GetFn ame
        {
        return Fname;
        }
        [/code]

        In your main():
        [code=cpp]
        Employee emp;

        char buffer[80];
        cin.getline(buf fer,80); //get the name
        emp.SetFname(bu ffer); //makes a copy of buffer
        cout << emp.GetFname() << endl;
        [/code]

        I would not put the data entry code inside the member functions.

        Originally posted by Hypnotik
        As for the debugger, this is my second c++ class and my instructor has not mentioned using it at all. I plan to ask him in our next meeting about debugger. I attempted to use it...and really didn't understand what was going on with it.
        Don't wait on your instructor. Learn your debugger.

        Comment

        • Hypnotik
          New Member
          • Jun 2007
          • 87

          #5
          Originally posted by weaknessforcats
          What you can do is return the array name:
          [code=cpp]
          char* Employee::GetFn ame
          {
          return Fname;
          }
          [/code]

          This works because the array name is the address of element 0. Element 0 of the Fname array is a char so the name Fname is the address of a char, that is, a char*.

          Unfortunately, this exposes the array to the outside world allowing some hacker-type to use the returned char* to change the contents of the array without needing to use your member functions. Cases like this require returning a constant char* so prevent this from happening.

          The correct code would be:
          [code=cpp]
          const char* Employee::GetFn ame
          {
          return Fname;
          }
          [/code]
          When I do this it gives me the error that Fname is an undeclared identifier. If I return Per.Fname I get a bunch of garbage symbols when I print out the info entered .

          J

          Comment

          • phiefer3
            New Member
            • Jun 2007
            • 67

            #6
            Originally posted by Hypnotik
            When I do this it gives me the error that Fname is an undeclared identifier.

            J
            then try this instead:

            [CODE=cpp]const char* Employee::GetFn ame
            {
            return &Fname[0];
            }
            [/CODE]

            that will return the address of the 0 element, and will get around the identifier issue.

            Everything else should be the same.

            Comment

            • Hypnotik
              New Member
              • Jun 2007
              • 87

              #7
              When I use the & I'm receiving the same error. On the other hand &Per.Fname gives me the same garbage output.

              Comment

              • phiefer3
                New Member
                • Jun 2007
                • 67

                #8
                Originally posted by Hypnotik
                When I use the & I'm receiving the same error. On the other hand &Per.Fname gives me the same garbage output.
                did you do &Fname, or &Fname[0]? because it will make a big difference.

                If that doesn't work, then try this, it's a bit much, but:

                &(this->Per.Fname[0]);

                Comment

                • Hypnotik
                  New Member
                  • Jun 2007
                  • 87

                  #9
                  Originally posted by phiefer3
                  did you do &Fname, or &Fname[0]? because it will make a big difference.

                  I tried both, and they both tell me that Fname is an undeclared identifier.

                  Comment

                  • phiefer3
                    New Member
                    • Jun 2007
                    • 67

                    #10
                    Originally posted by Hypnotik
                    I tried both, and they both tell me that Fname is an undeclared identifier.
                    check my edit, and try that:

                    &(this->Per.Fname[0]);

                    if that doesn't work, then I don't know.

                    by the way, the way you're class is set up, it would might be easier to make your employee class inherit from the person class, instead of containing a personclass object.

                    Comment

                    • Hypnotik
                      New Member
                      • Jun 2007
                      • 87

                      #11
                      Originally posted by phiefer3
                      check my edit, and try that:

                      &(this->Per.Fname[0]);
                      Well that is atleast not giving me the error. Now I receive the about a line and a half of garbage symbols.

                      After I input the information, I print it out (within) that function to make sure it's in the right place...and it seems to be saving in the right place. But when I go to this function (SetFname) something is getting lost.

                      J

                      Comment

                      • weaknessforcats
                        Recognized Expert Expert
                        • Mar 2007
                        • 9214

                        #12
                        Sorry about missing Per. I didn't compile my code.

                        I did compile this code:
                        [code=cpp]
                        #include <iostream>
                        using namespace std;
                        struct Person
                        {
                        char Fname[20],Lname[20];
                        float age;
                        int ssn[9],enumber[9];
                        };
                        class Employee
                        {
                        private:
                        int enumber[9],ssn[9];
                        float wage;
                        char job_title[10];
                        Person Per;
                        public:
                        const char* GetFname();
                        void SetFname(char* arg);
                        };
                        const char* Employee::GetFn ame()
                        {
                        return Per.Fname;
                        }
                        void Employee::SetFn ame(char* arg)
                        {
                        strcpy(Per.Fnam e, arg);
                        }

                        int main()
                        {
                        Employee emp;
                        emp.SetFname("w eaknessforcats" );
                        cout << emp.GetFname() << endl;
                        }
                        [/code]

                        and it works.

                        There is no difference between Per.Fname and &Per.Fname[0]. It is just two ways of saying the same thing.

                        Comment

                        Working...