Pass by reference

I need to change this code from a return function to a pass by reference, but its not a math problem so I'm not sure what i need to do:

// File: Lab 22P1.cpp

#include <cstdlib>
#include <iostream>
using namespace std;

string getProductCode();

int main()
{
string productCode = "";

productCode = getProductCode();
cout << "Product code read from keyboard: " << productCode << endl;

system("pause");
return 0;
}

string getProductCode()
{
string code = "";
bool valid = false;
while (valid == false )
{
cout << "Enter the orange's 4-digit code. " << endl;
cout << "The last digit must be either 4 or 7." << endl;
cout << "Enter code here: ";
cin >> code;
if (code.length() == 4 && (code[3] == '4' || code[3] == '7'))
{
valid = true;
}
else
{
cout << "Invalid code. Please try again." << endl;
}
}
return code;
}
Last edited on
Although it does not make sense to do so, this is how you would do it:
1
2
3
4
5
void getProductCode(std::string &code)
{
    //...
    //no return statement
}
and would that go in place of the "return" statement listed? not sure where to put it...
You need to change your function declaration and definition. Since you did not put your code [code]between code tags[/code] I cannot tell you which line numbers to look at. You can edit your post and fix it if you want.
Im not sure if i understand what you are saying, this code doesn't run, but just to show you to see if im on the right path.


// File: Lab 22P1.cpp

#include <cstdlib>
#include <iostream>
using namespace std;

void getProductCode();

int main()
{
double productCode = 0;

productCode = getProductCode(string, code);
cout << "Product code read from keyboard: " << productCode << endl;

system("pause");
return 0;
}

void getProductCode(std::string &code)
{
string code = "";
bool valid = false;
while (valid == false )
{
cout << "Enter the orange's 4-digit code. " << endl;
cout << "The last digit must be either 4 or 7." << endl;
cout << "Enter code here: ";
cin >> code;

if (code.length() == 4 && (code[3] == '4' || code[3] == '7'))
{
valid = true;
}
else
{
cout << "Invalid code. Please try again." << endl;
}
}
}
Please edit your post so that your code is [code]between code tags[/code] so I can reference line numbers.
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
// File: Lab 22P1.cpp


#include <cstdlib>
#include <iostream>
using namespace std;

void getProductCode();

int main()
{
	double productCode = 0;
	
	productCode = getProductCode(string, code);	
	cout << "Product code read from keyboard: " << productCode << endl;

    system("pause");
    return 0;
}

void getProductCode(std::string &code)
{
	string code = "";
	bool valid = false;
	while (valid == false )
	{
		cout << "Enter the orange's 4-digit code. " << endl;
		cout << "The last digit must be either 4 or 7." << endl;
		cout << "Enter code here: ";
		cin >> code;
		
		if (code.length() == 4 && (code[3] == '4' || code[3] == '7'))
		{
			valid = true;
		}
		else
		{
			cout << "Invalid code.  Please try again." << endl;
		}
	}
}
Last edited on
Line 24 should pass a variable not a type and non-existant variable actually I'm not sure the line I'm on phone but when u call it and prototype are wrong
Last edited on
line 5?
You need to change line 8 the same way you changed line 21.

@giblit: I think you mean line 14, not 24
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
/ File: Lab 22P1.cpp

#include <cstdlib>
#include <iostream>
using namespace std;

void getProductCode(std::string &code)

int main()
{
	double productCode = 0;
	
	productCode = getProductCode(string, code);	
	cout << "Product code read from keyboard: " << productCode << endl;
	
	getProductCode(string, code)

    system("pause");
    return 0;
}

void getProductCode(std::string &code)
{
	string code = "";
	bool valid = false;
	while (valid == false )
	{
		cout << "Enter the orange's 4-digit code. " << endl;
		cout << "The last digit must be either 4 or 7." << endl;
		cout << "Enter code here: ";
		cin >> code;
		
		if (code.length() == 4 && (code[3] == '4' || code[3] == '7'))
		{
			valid = true;
		}
		else
		{
			cout << "Invalid code.  Please try again." << endl;
		}
	}
}
/
ok i changed line 8 like line 21, now i get error of the following: 26 9 E:\CIS 115\Lab22P1.cpp [Error] declaration of 'std::string code' shadows a parameter
Ok i finally see what you all were saying, but now I am still getting the same error, for line 23 :(

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
// File: Lab 22P1.cpp

#include <cstdlib>
#include <iostream>
using namespace std;

void getProductCode(string &);

int main()
{
	string productCode = 0;
	
	getProductCode(productCode);	
	cout << "Product code read from keyboard: " << productCode << endl;
	

    system("pause");
    return 0;
}

void getProductCode(string &code)
{
	string code = "";
	bool valid = false;
	while (valid == false )
	{
		cout << "Enter the orange's 4-digit code. " << endl;
		cout << "The last digit must be either 4 or 7." << endl;
		cout << "Enter code here: ";
		cin >> code;
		
		if (code.length() == 4 && (code[3] == '4' || code[3] == '7'))
		{
			valid = true;
		}
		else
		{
			cout << "Invalid code.  Please try again." << endl;
		}
	}
}

Remove "string" from the start of line 23 - the variable already exists (it is the parameter).
ok so i removed it, the program compiles, but it runs an error of termination...
hint...
 
string productCode = 0;


You've defined a string, but initialised it with an integer..

also do this at the top:
 
#include <string> 


Also, you get the user to enter the code and do the validation in the same function, so why are you even passing the code into that function?
mutexe wrote:
Also, you get the user to enter the code and do the validation in the same function, so why are you even passing the code into that function?
Read OP - professor requires it.
Last edited on
ah right.
thanks for your help! I finally think i have it correct!
Topic archived. No new replies allowed.