Issue with String Concatentation

I'm trying to concatenate a bunch of strings into a single string within a function, which I can then return to the main function. The code I am using is as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
string basic_new, hra_new, spl_new, lta_new, conv_new, pf_new, grat_new, varpay_new, supann_new, nps_new;
	basic_new = to_string(basic2_i);
	hra_new = to_string(hra2_i);
	spl_new = to_string(spl2_i);
	lta_new = to_string(lta2_i);
	conv_new = to_string(conv2_i);
	pf_new = to_string(pf2_i);
	grat_new = to_string(grat2_i);
	varpay_new = to_string(varpay2_i);
	supann_new = to_string(supann2_i);
	nps_new = to_string(nps2_i);

	stringstream xx;
	string delimit = ",";

	xx << basic_new << delimit << hra_new << delimit << spl_new << delimit << lta_new << delimit << conv_new << delimit;
	xx << pf_new << delimit << grat_new << delimit << varpay_new << delimit << supann_new << delimit << nps_new;

	string sal_new = xx.str();

	return sal_new;


Unfortunately, the return is coming blank. How do I resolve this?
How do I resolve this?

Provide complete code and we might be able to tell you.
Uh... it's a big program. I'm pasting it here:

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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
#include <algorithm>
#include <iomanip>

using namespace std;

double& max(double& a, double& b)
{
	return a > b ? a : b;
}

double& grat_on_wage(double& x, double& y)
{
	double grat_x, grat_y, grat_z;
	grat_x = max(x, y);
	grat_y = grat_x * 5;
	grat_z = grat_y / 104;

	return grat_z;
}

string& salary_new(int& basic1, int& hra1, int& spl1, int& lta1, int& conv1, int& pf1, int& grat1, int& varpay1, int& supann1, int& nps1, double& increment, int& supann2_e, int& nps2_e)
{
	double ctc2, varpay2, tfp2, basic2, hra2, lta2, pf2, conv2, spl2, supann2, nps2;
	double basic2_plch, basic1_d;

	int ctc1 = basic1 + hra1 + spl1 + lta1 + conv1 + pf1 + grat1 + varpay1 + supann1 + nps1;

	ctc2 = ctc1 * (1 + (increment / 100));
	varpay2 = 0.15 * ctc2;
	tfp2 = ctc2 - varpay2;
	spl2 = 0;
	supann2 = 0;
	nps2 = 0;

	basic2_plch = 0.35 * tfp2;
	basic1_d = double(basic1);
	basic2 = max(basic2_plch, basic1_d);

	hra2 = 0.85 * basic2;
	lta2 = basic2 / 12;
	pf2 = 0.12 * basic2;
	conv2 = basic2 / 12;

	if (supann2_e == 1)
	{
		supann2 = 0.15 * basic2;
	}

	if (nps2_e == 1)
	{
		nps2 = 0.1 * basic2;
	}

	double wage2_plch;
	wage2_plch = 0.5 * tfp2;

	for (spl2 = 0; spl2 < 5000000; spl2++)
	{
		double incl2 = basic2 + spl2 + lta2;
		double grat2 = grat_on_wage(incl2, wage2_plch);
		double remainder2 = tfp2 - basic2 - hra2 - lta2 - pf2 - conv2 - supann2 - nps2 - spl2 - grat2;
		if (remainder2 < 1)
		{
			break;
		}
	}

	int ctc2_i, tfp2_i, basic2_i, hra2_i, spl2_i, lta2_i, conv2_i, pf2_i, grat2_i, supann2_i, nps2_i, varpay2_i, thp2;

	ctc2_i = int(round(ctc2));
	varpay2_i = int(round(varpay2));
	tfp2_i = ctc2_i - varpay2_i;
	basic2_i = int(round(basic2));
	hra2_i = int(round(hra2));
	spl2_i = int(round(spl2));
	lta2_i = int(round(lta2));
	conv2_i = int(round(conv2));
	pf2_i = int(round(pf2));
	supann2_i = int(round(supann2));
	nps2_i = int(round(nps2));
	grat2_i = tfp2_i - basic2_i - hra2_i - lta2_i - spl2_i - conv2_i - pf2_i - supann2_i - nps2_i;
	thp2 = tfp2_i - supann2_i - nps2_i - 2 * (pf2_i + grat2_i);

	string basic_new, hra_new, spl_new, lta_new, conv_new, pf_new, grat_new, varpay_new, supann_new, nps_new;
	basic_new = to_string(basic2_i);
	hra_new = to_string(hra2_i);
	spl_new = to_string(spl2_i);
	lta_new = to_string(lta2_i);
	conv_new = to_string(conv2_i);
	pf_new = to_string(pf2_i);
	grat_new = to_string(grat2_i);
	varpay_new = to_string(varpay2_i);
	supann_new = to_string(supann2_i);
	nps_new = to_string(nps2_i);

	stringstream xx;
	string delimit = ",";

	xx << basic_new << delimit << hra_new << delimit << spl_new << delimit << lta_new << delimit << conv_new << delimit;
	xx << pf_new << delimit << grat_new << delimit << varpay_new << delimit << supann_new << delimit << nps_new;

	string sal_new = xx.str();

	return sal_new;
}

int main()
{
	ifstream inputFile;
	inputFile.open("DummyData.csv");

	if (!inputFile)
	{
		return (cout << "Cannot open file. Please try again.\n"), 1;
	}

	for (string line; getline(inputFile,line); )
	{
		vector <string> v;
		istringstream ss(line);

		for (string temp; getline(ss, temp, ','); )
		{
			v.push_back(temp);
		}

		int basic_c = stoi(v[0]);
		int hra_c = stoi(v[1]);
		int spl_c = stoi(v[2]);
		int lta_c = stoi(v[3]);
		int conv_c = stoi(v[4]);
		int supann_c = stoi(v[5]);
		int nps_c = stoi(v[6]);
		int pf_c = stoi(v[7]);
		int grat_c = stoi(v[8]);
		int varpay_c = stoi(v[9]);
		double increment_q = stoi(v[10]);
		int supann_s = stoi(v[11]);
		int nps_s = stoi(v[12]);
		
		string salary_2;

		salary_2 = salary_new(basic_c, hra_c, spl_c, lta_c, conv_c, pf_c, grat_c, varpay_c, supann_c, nps_c, increment_q, supann_s, nps_s);

		cout << basic_c << " " << hra_c << " " << varpay_c << "\n";
		cout << salary_2 << "\n";
	}

	return 0;
}
Change return type from string& to just string.
Don't ignore the error messages.

DON'T RETURN REFERENCES.

string& salary_new(int& basic1, int& hra1, ...)
should just be
string salary_new(int& basic1, int& hra1, ...)
and similarly with the other functions.


As you can see, that is NOT in your original code sample ... which is why you should post something complete.



I fudged your input with
1
2
3
	//ifstream inputFile;
	//inputFile.open("DummyData.csv");
	istringstream inputFile( "1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13" );

and (after removing the & from your function line as above) it gave
1 2 10
18,15,13,2,2,2,0,9,0,0



Last edited on
Thanks! Removing the & worked.
Can you post a data file sample please.
L26 - don't return by ref replace string& salary_new(...) with string salary_new(...)
Topic archived. No new replies allowed.