Passing Parallel Arrays through a function

Hi everyone. I was wondering if it was possible to pass parallel arrays through a function. I've looked around in a couple of my go to sources, but I can't seem to turn up anything yet. I just thought I would ask if someone knew if this was possible.

thanks

You surely can, but you'll need an argument for each array.

The other option would be to have some kind of zip iterator (like Boost's http://www.boost.org/doc/libs/release/libs/iterator/doc/zip_iterator.html).

The best thing, though, is to avoid parallel arrays whenever possible, and use a single array of structs that aggregate related information.
I really wish I could avoid them, but this is for a homework assignment where parallel arrays are a requirement. Thank you for that.
Just make an argument for each array, then. You only need one size argument.

1
2
3
4
5
6
7
8
void foozle( double bogs[], string gorfs[], int ziphs[], int size )
{
  for (int n = 0; n < size; n++)
  {
    if (bogs[n] < ziphs[n]) 
      cout << gorfs[n];
  }
}

Good luck!
So I haven't reached the for loop yet, because I am trying to set up the arguments. However, I get a few error messages.

error C2365: 'test_addToFile' : redefinition; previous definition was 'data variable'
error C2065: 'string' : undeclared identifier
error C2146: syntax error : missing ')' before identifier 'names'
error C2182: 'test_addToFile' : illegal use of type 'void'
error C2059: syntax error : ')'

The syntax errors confuse me, because as far as I can tell, things look fine.
I can't find a straight forward explanation of "illegal use of type void"? Again, I tried following the syntax above, but the compiler doesn't agree. Here is my code below. I feel like I've been following instructions I've been receiving to the teeth, but nothing seems to be working.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*This is an outline for a simple program I am building that simulates a sales company.
 It first gets the name of the employees, along with the sales figures they made this quarter. 
The names and sales figures will then be written into a file, and will later be read back to the screen should the user choose to do so. 
I left my comments as to help avoid confusion.  */
#include<iostream> 
#include<iomanip>
#include<fstream> 
#include<string> 

void testShow_Menu(); // stub for menu
void test_addToFile(string names[], string sales[], int size); // stub for adding names and sales to file 
void test_showSalesReport(); // stub for showing sales report 

const int SIZE = 10;


using namespace std; 

int main()
{
	testShow_Menu(); // stub to test Show_Menu function 

}
/*
Written on Oct 22, 2015. 
Precondition: As the program powers on, the menu is diplayed 
giving vthe choice to add to file, view sales or exit. 
Post Condition: User makes a selection, passing the choice to a 
switch statement calling the appropriate function*/
void testShow_Menu()
{
	string fileName;
	string names[SIZE]; 
	string sales[SIZE]; 
	cout << "Enter a file name: ";
	getline(cin, fileName);

	cout << "Welcome To our Sales Program!" << endl; 
	cout << "What would you like to do?" << endl;
	cout << endl; 
	cout << setw(20) << "1 - Add to sales " << endl;
	cout << setw(25) << "2 - View sales report " << endl;
	cout << setw(11) << "3 - Exit" << endl; 

	int choice; 
	cin >> choice; 
	switch (choice)
	{
	case 1:
		//test_addToFile();
		break;
	case 2:
		test_showSalesReport();
		break;
	case 3: 
		break; 
	default: 
		cout << "Invalid choice. Please enter a number 1-3 or type 3 to exit. "; // prevents invalid input into program. 
	}
}

void test_addToFile(string names[], string sales[], int size)
{
	string testName[SIZE], testSales[SIZE];
	ofstream out_file;
	ifstream in_file;
	cout << "This is add to file"; 
Last edited on
closed account (48T7M4Gy)
For a start you need to shift line 17 up to before line 11 to overcome the 'lack of knowledge about 'string'.
Oh right. I need to get in the habit of declaring namespace at the top of my programs. I found a couple of small errors on my own once I fixed that. Thanks for that. I'll likely have more questions soon, but thank you for your help.
So here is how I am declaring the call to function test_addToFile().

test_addToFile(names[], sales[], SIZE)

My understanding is I am going to assign these arguments values through names, and sales, and SIZE is the size of each array. However, it's expecting an expression now. I'm not quite sure what it's talking about...
closed account (48T7M4Gy)
1. Relocate using namespace std;
2. Closing brace required at new line 68
3. Line 12 should end with {}; Brace pair holds the stub code, i.e. it is there but empty.
Last edited on
Figured that out shortly after I posted that. Thank you.
closed account (48T7M4Gy)
Figured that out shortly after I posted that.

Perhaps next time let everyone know your problem is solved. Saves wasting time better spent helping someone else :(
Topic archived. No new replies allowed.