473,395 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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.

Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <iostream>
  3. using namespace std;
  4. struct Person
  5. {
  6.     char Fname[20],Lname[20];
  7.     float age;
  8.     int ssn[9],enumber[9];
  9. };
  10. class Employee
  11. {
  12. private:
  13.     int enumber[9],ssn[9];
  14.     float wage;
  15.     char job_title[10];
  16.     Person Per;
  17. public:
  18.     Employee();
  19.     ~Employee(){};
  20.     void SetFname(int emp);
  21.     void SetLname(int emp);
  22.     void Setage();
  23.     void Setssn(int emp);
  24.     void Setenumber(int emp);
  25.     void Setwage();
  26.     void Setjob_title();
  27.     char GetFname();
  28.     char GetLname();
  29.     void Getage();
  30.     int Getssn();
  31.     int Getenumber();
  32.     void Getwage();
  33.     void Getjob_title();
  34.     void output();
  35.     void salary();
  36.     void flipflop();
  37. };
  38. Employee::Employee()
  39. {
  40.     int i=0,j=0;
  41.     cout<<"Constructor."<<endl;
  42.     wage=0;
  43.  
  44.     for (i=0;i<=9;++i)
  45.     {
  46.         enumber[i]=0;
  47.         ssn[i]=0;
  48.     }
  49.  
  50. }
  51. void Employee::SetFname(int emp)
  52. {    
  53.     cout<<"BEGIN SETFNAME FUNCTION."<<endl;
  54.     cout<<"Enter the first name of the employee."<<endl;
  55.     cin.ignore();
  56.     cin.getline (Per.Fname,20,'\n');
  57.     cout<<Per.Fname<<endl;
  58. }
  59. void Employee::SetLname (int emp)
  60. {
  61.     cout<<"BEGIN SETLNAME FUNCTION."<<endl;
  62.     cout<<"Enter the last name of the employee."<<endl;
  63.     cin.getline (Per.Lname,20,'\n');
  64.     cout<<Per.Lname<<endl;
  65. }
  66. void Employee::Setssn(int emp)
  67. {    
  68.     cout<<"SETSSN Function"<<endl;
  69.     cout<<"Enter the social security number of the employee."<<endl;
  70.     cin>>Per.ssn[emp];
  71.     cout<<Per.ssn[emp]<<endl;
  72.  
  73. }
  74. void Employee::Setenumber(int emp)
  75. {
  76.     int i;
  77.     cout<<"SETENUMBER FUNCTION."<<endl;
  78.     cout<<"Enter the employee number."<<endl;
  79.     cin>>Per.enumber[emp];
  80.     cout<<Per.enumber[emp]<<endl;
  81. }
  82. void Employee::output()
  83. {
  84.     int emp;
  85.     cout<<"Which employee's information would you like to view?"<<endl;
  86.     cin>>emp;
  87.     cout<<"Last name:        "<<GetLname()<<endl;
  88.     cout<<"First name:       "<<GetFname()<<endl;
  89.     cout<<"Employee number:  "<<Getenumber()<<endl;
  90.     cout<<"Employee ssn:         "<<Getssn()<<endl;
  91. }
  92. int Employee::Getssn()
  93. {
  94.     cout<<"Getssn function"<<endl;
  95.     return (0);
  96.     //return (Per.ssn[value]);
  97. }
  98. int Employee::Getenumber()
  99. {
  100.     cout<<"Getenumber function"<<endl;
  101.     return(0);
  102.     //return (Per.age);
  103. }
  104. char Employee::GetLname()
  105. {
  106.     cout<<"GetLname function"<<endl;
  107.     return (0);
  108.     //return(Per.Lname);
  109. }
  110. char Employee::GetFname()
  111. {
  112.     cout<<"GetFname function"<<endl;
  113.     return (0);
  114.     //return (Per.Fname);
  115. }
  116. void main()
  117. {
  118.     int emp=-1,choice;
  119.     Employee empl[10];
  120.     while(emp<0||emp>9)
  121.     {
  122.     cout<<"Choose and employee 0-9"<<endl;
  123.     cin>>emp;
  124.     }
  125.     do
  126.     {
  127.     cout<<"Please select what you would like to edit from the menu:"<<endl;
  128.     cout<<"1) Enter the first name."<<endl<<"2) Enter the last name."<<endl<<"3) Enter the social security number."<<endl;
  129.     cout<<"4) Enter the employee number."<<endl<<"5) Enter all of the information."<<endl<<"6) Output the information for employee."<<endl;
  130.     cin>>choice;
  131.         switch(choice)
  132.         {
  133.         case 1:
  134.             empl[emp].SetFname(emp);
  135.             break;
  136.  
  137.         case 2:
  138.             empl[emp].SetLname(emp);
  139.             break;
  140.  
  141.         case 3:
  142.             empl[emp].Setssn(emp);
  143.             break;
  144.  
  145.         case 4:
  146.             empl[emp].Setenumber(emp);
  147.             break;
  148.  
  149.         case 5:
  150.             cout<<"Please enter all of the employees information."<<endl;
  151.             empl[emp].SetFname(emp);
  152.             empl[emp].SetLname(emp);
  153.             empl[emp].Setssn(emp);
  154.             empl[emp].Setenumber(emp);
  155.             break;
  156.  
  157.         case 6:
  158.             cout<<"The employees information is: "<<endl;
  159.             empl[emp].output();
  160.             break;
  161.         }
  162.     }
  163.     while (choice<1||choice>6);
  164. }
  165.  
  166.  
Thanks,
J
Jun 28 '07 #1
11 2313
weaknessforcats
9,208 Expert Mod 8TB
This code:
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;
}
is an Employee member function. You have access to Per:
Expand|Select|Wrap|Line Numbers
  1. void Employee::output()
  2. {
  3.     int emp;
  4.     cout<<"Which employee's information would you like to view?"<<endl;
  5.     cin>>emp;
  6.     cout<<"Last name:        "<<Per.Lname<<endl;
  7.     cout<<"First name:       "<<Per.Fname()<<endl;
  8.     cout<<"Employee number:  "<<Per.enumber()<<endl;
  9.     cout<<"Employee ssn:         "<<Per.ssn()<<endl;
  10. }
  11.  
Besides your Employee::GetLname() does not return the name. It returns a binary zero. Other get member functions do the same.

This one:
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;
}
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.
Jun 29 '07 #2
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
Jul 1 '07 #3
weaknessforcats
9,208 Expert Mod 8TB
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:
Expand|Select|Wrap|Line Numbers
  1. char* Employee::GetFname
  2. {
  3.     return Fname;
  4. }
  5.  
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:
Expand|Select|Wrap|Line Numbers
  1. const char* Employee::GetFname
  2. {
  3.     return Fname;
  4. }
  5.  
In your main():
Expand|Select|Wrap|Line Numbers
  1. Employee emp;
  2.  
  3. char buffer[80];
  4. cin.getline(buffer,80);    //get the name
  5. emp.SetFname(buffer);  //makes a copy of buffer
  6. cout << emp.GetFname() << endl;
  7.  
I would not put the data entry code inside the member functions.

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.
Jul 1 '07 #4
What you can do is return the array name:
Expand|Select|Wrap|Line Numbers
  1. char* Employee::GetFname
  2. {
  3.     return Fname;
  4. }
  5.  
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:
Expand|Select|Wrap|Line Numbers
  1. const char* Employee::GetFname
  2. {
  3.     return Fname;
  4. }
  5.  
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
Jul 1 '07 #5
When I do this it gives me the error that Fname is an undeclared identifier.

J
then try this instead:

Expand|Select|Wrap|Line Numbers
  1. const char* Employee::GetFname
  2. {
  3.     return &Fname[0];
  4. }
  5.  
that will return the address of the 0 element, and will get around the identifier issue.

Everything else should be the same.
Jul 1 '07 #6
When I use the & I'm receiving the same error. On the other hand &Per.Fname gives me the same garbage output.
Jul 1 '07 #7
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]);
Jul 1 '07 #8
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.
Jul 1 '07 #9
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.
Jul 1 '07 #10
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
Jul 1 '07 #11
weaknessforcats
9,208 Expert Mod 8TB
Sorry about missing Per. I didn't compile my code.

I did compile this code:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3. struct Person
  4. {
  5.     char Fname[20],Lname[20];
  6.     float age;
  7.     int ssn[9],enumber[9];
  8. };
  9. class Employee
  10. {
  11. private:
  12.     int enumber[9],ssn[9];
  13.     float wage;
  14.     char job_title[10];
  15.     Person Per;
  16. public:
  17.     const char* GetFname();
  18.     void SetFname(char* arg);
  19. };
  20. const char* Employee::GetFname()
  21. {
  22.      return Per.Fname;
  23. }
  24. void Employee::SetFname(char* arg)
  25. {
  26.     strcpy(Per.Fname, arg);
  27. }
  28.  
  29. int main()
  30. {
  31.     Employee emp;
  32.     emp.SetFname("weaknessforcats");
  33.     cout << emp.GetFname() << endl;
  34. }
  35.  
and it works.

There is no difference between Per.Fname and &Per.Fname[0]. It is just two ways of saying the same thing.
Jul 1 '07 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Andrew Poulos | last post by:
If I'm searching for an occurance of a value in a multi-dimensional array how can I get it's index returned as an array, if found? For example, if: foo = new Array(); foo = , 5, , 9, 10]; ...
4
by: Isaac | last post by:
Hi mates I want to know a simple program of return array from function ? Do I need to use pointer to return the address of the first element in an array. Isaac
6
by: Neo | last post by:
Dear All, I want to know how a subroutine should return an array of values to the main program. From the main program, I call a sub-routine 'get_sql' which then fetches data from oracle db using...
8
by: M. Moennigmann | last post by:
Dear all: I would like to write a function that opens a file, reads and stores data into an 2d array, and passes that array back to the caller (=main). The size of the array is not known before...
23
by: Nascimento | last post by:
Hello, How to I do to return a string as a result of a function. I wrote the following function: char prt_tralha(int num) { int i; char tralha;
31
by: CeZaR | last post by:
Hi, How can i specify the return type of a function returning a managed array of chars. If i try to write: "char __gc func()" i get an error! How can i do that? Thanks!
4
by: Woody Splawn | last post by:
How would I pass an array back to a sub routine from a function? That is, I have a function that looks like this Public Function arrayTest() As Array Dim states() As String = { _ "AZ", "CA",...
7
by: nafri | last post by:
hello all, I want to create a function that returns the first element of the Array that is input to it. However, the Input Array can be an Array of points, double, or anyother type, which means...
7
by: ashu | last post by:
look at code #include<stdio.h> int *mult(void); int main(void) { int *ptr,i; ptr=mult; for(i=0;i<6;i++) { printf("%d",*(ptr++));
18
by: Pedro Pinto | last post by:
Hi there once more........ Instead of showing all the code my problem is simple. I've tried to create this function: char temp(char *string){ alterString(string); return string;
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.