Passing multiple arguments to function

I am working on a program that uses several 2d arrays and several functions to modify this data. It is a bit cumbersome to continually pass multiple arrays. All of these are related so ideally it would be nice to pass one thing. Global would solve, but I know there is a better way. I have limited experience with classes and structs. I have researched quite a lot, but am still unsure of the best way without asking directly. Here is the gist of my code, modified for simplicity.

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
int GetData( string[][2], double[][2], double[][2], double[][2] );

int main() {
	string names[20][2];
	double nums1[20][2], nums2[20][2], nums3[20][2], nums4[20][2],
	inTotal[2];
	int size;
	
	size = GetData( names, nums1, nums2, nums3 );
}
int GetData( string names[][2], double num1[][2], double nums2[][2], double nums3[][2] ) {
	string input;
	int count = 0;
	bool more = true;
		
	while (more) {
		count++;
		cout << " Please enter a name";
		getline( cin, names[count][0] );

		cout << "enter number 1";
		getline(cin, input);
		nums1[count][0] = InputValidate(input);

		cout << "enter number 2";
		getline(cin, input);
		nums2[count][0] = InputValidate(input);

		cout << "enter number 3";
		getline(cin, input);
		nums3[count][0] = InputValidate(input);
	}
	return count;
}


Obviously this has an infinite loop as it stands because I removed some things.
Seems like you want a struct here.

The idea is a struct is a new variable type (like an int, or string, or whatever)... except it's made of a bunch of other vars. You can pass it around and treat it like 1 thing, rather than a bunch of separate things.

You also seem to want a variable size container, like a vector. A vector is basically a resizable array. Couple that with your struct to make a resizable array of structs, where each element in the array contains all the names/nums.

Also... why do these need to be 2D arrays?

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
#include <vector>  // <- for vector

struct Data  // come up with a better name for this
{
    std::string name;
    double num1;  // come up with a better name for this
    double num2;  // come up with a better name for this
    double num3;  // come up with a better name for this
};

typedef vector<Data> DataVec;  // this allows us to just say "DataVec"
    // instead of "std::vector<Data>"

DataVec GetData()   // return the array of 
{
    DataVec output;     // the array we are building
    
    string input;
    bool more = true;
    while(more)
    {
        Data dat;   // create a new 'dat' variable, which is an object of our 'Data'
                    // struct

        // populate our 'dat' struct
        cout << " Please enter a name";
        getline( cin, dat.name );

        cout << "enter number 1";
        getline(cin, input);
        dat.num1 = InputValidate(input);

        cout << "enter number 2";
        getline(cin, input);
        dat.num2 = InputValidate(input);

        cout << "enter number 3";
        getline(cin, input);
        dat.num3 = InputValidate(input);
        
        // now that 'dat' contains all our data, put it in our vector
        output.push_back(dat);      // <- increases the size of our array by 1, by
                                    //   appending 'dat' at the end of it
    }
    
    // now that our array is built, return it
    return dat;
}


int main()
{
    DataVec data = GetData();  // <- all there is to it.  Now you have an array of data
}




EDIT:

Alternatively... using an array instead of 'num1', 'num2', 'num3', etc. (if you find yourself doing that, you are probably doing something wrong):

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

[code]
#include <vector>  // <- for vector

struct Data  // come up with a better name for this
{
    std::string name;
    double nums[3];  // come up with a better name for this
};

typedef vector<Data> DataVec;  // this allows us to just say "DataVec"
    // instead of "std::vector<Data>"

DataVec GetData()   // return the array of 
{
    DataVec output;     // the array we are building
    
    string input;
    bool more = true;
    while(more)
    {
        Data dat;   // create a new 'dat' variable, which is an object of our 'Data'
                    // struct

        // populate our 'dat' struct
        cout << " Please enter a name";
        getline( cin, dat.name );

        for(int i = 0; i < 3; ++i)
        {
            cout << "enter number " << (i+1);
            getline(cin, input);
            dat.nums[i] = InputValidate(input);
        }
        
        // now that 'dat' contains all our data, put it in our vector
        output.push_back(dat);      // <- increases the size of our array by 1, by
                                    //   appending 'dat' at the end of it
    }
    
    // now that our array is built, return it
    return dat;
}


int main()
{
    DataVec data = GetData();  // <- all there is to it.  Now you have an array of data
}
Last edited on
Okay I feel its a little over my head at this point, but I will work with this and I'll figure it out and learn by using it. The reason for the 2d arrays is they essentially become parallel, well maybe not. My program is debt payoff calculator comparing the avalanche method to the snowball method. So one column of the array is sorted by interest rate, the other sorted by balance. At the time I thought it would be less cluttered with the 2d. Here is the actual code.

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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
bool YorN();
double InputValidate(string);
//ResultDisplay();
//OutputSnow();
//OutputAva();
//SortBal();
//Rollover();
//CalcInt();
//CalcPayoff();
void SortRates( string[][2], double[][2], double[][2], double[][2], int);
int GetData( string[][2], double[][2], double[][2], double[][2] );
//void Welcome();

int main() {
	system("cls");
	int size;
	int months[2];
	string names[20][2];
	double balance[20][2], rates[20][2], pymnts[20][2], inAccru[20][2], inTotal[2];
	//Welcome();
	size = GetData( names, balance, rates, pymnts );
        SortRates( names, balance, rates, pymnts, size );

	

	
}

int GetData( string names[][2], double balance[][2], double rates[][2], double pymnts[][2] ) {
	string input;
	int count = 0;
	bool more = true;
		
	while (more) {
		count++;
		cout << " Please enter a name for your loan.\n\n ";
		getline( cin, names[count][0] );
		system("cls");
		cout << " Please enter the principal balance.\n\n " 
		     << names[count][0] << "\n $";
		getline(cin, input);
		balance[count][0] = InputValidate(input);
		system("cls");
		cout << " Please enter the interest rate. Example 4.5,"
		     << "enter 0 for zero.\n\n " << names[count][0] << "\n $"
		     << balance[count][0] << "\n ";
		getline(cin, input);
		rates[count][0] = InputValidate(input);
		system("cls");
		cout << " Please enter the minimum monthly payment.\n\n "
		     << names[count][0] << "\n $" << balance[count][0] << "\n " 
		     << rates[count][0] << "%\n $";
		getline(cin, input);
		pymnts[count][0] = InputValidate(input);
		system("cls");
		cout << "\n\n " << names[count][0] << "\n $" << balance[count][0] 
		     << "\n " << rates[count][0] << "%\n $" << pymnts[count][0];
		cout << "\n\n Do you have another to add? (y/n):  ";
		more = YorN();
		cin.clear();
		cin.ignore();
	}
	return count;
}

double InputValidate(string sInput) {
	double num = 0;
	bool again = true;
 
	while (again) {
		system("cls");
		stringstream(sInput) >> num;
		if ( !isdigit(sInput[0]) ) {
			cout << "\n Please enter a number. \n";
			getline(cin, sInput);
		} else if ( num < 0 ) {
			cout << "\n Please enter a positive number. \n";
			getline(cin, sInput);
		} else {
			again = false;
		}                       
	} 
	return num;
}	

bool YorN() {
	char answer;
	bool again = true;
	
	while(again) {
		cin >> answer;
		system("cls");
		answer = tolower(answer);
		if (answer == 'y') {
			again = true;
			break;
		} else if (answer == 'n') {
			again = false;
			break;
		} else {
			cout << "Please enter (y/n). Do you have another?  ";
		}
		
	}return again;
}

void SortRates( string names[][2], double balance[][2], double rates[][2], double pymnts[][2], int size) {
	int index, scan, row = 0;
	string unNm;
	double unBal, unRt, unPy;
	
	for (index = 1; index < size; index++) {
		unRt = rates[index][0];
		unNm = names[index][0];
		unBal = balance[index][0];
		unPy = pymnts[index][0];
		scan = index;
		while (scan > 0 && rates[scan-1][0] < unRt) {
			rates[scan][0] = rates[scan - 1][0];
			names[scan][0] = names[scan - 1][0];
			balance[scan][0] = balance[scan - 1][0];
			pymnts[scan][0] = pymnts[scan - 1][0];
			--scan;
		}
		rates[scan][0] = unRt;
		names[scan][0] = unNm;
		balance[scan][0] = unBal;
		pymnts[scan][0] = unPy;
	}
	//CalcPayoff(row);
}
Last edited on
Topic archived. No new replies allowed.