Modifying code to reference function instead of return function

I'm using a function getProductCode to read a product code from the
keyboard. This function uses a return statement to send the product code to main.
My assignment is to Modify the program. I can not use return statement to send product code from getProductCode to main. Instead, I have to use pass-by-reference to achieve the same goal.
Every time I try to execute the code a window pops up that says "Lessons22Lab26P1.exe has stopped working; Windows can check online for a solution to the problem"
This only occurs when I enter a four digit number that ends in 4 or 7 however if I enter in any other value it will display what it is suppose to display "Invalid code. Please try again."

The following is my 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
#include <cstdlib> 
#include <iostream> 
using namespace std; 
 
string getProductCode(string &); 
 
int main() 
{ 
 string productCode = ""; 
 
 productCode = getProductCode(productCode); 
 cout << "Product code read from keyboard: " << productCode << endl; 
 
 system("pause"); 
 return 0; 
} 
 
string getProductCode(string &productCode)
{ 
 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 >> productCode;
  if (productCode.length() == 4 && (productCode[3] == '4' || productCode[3] == '7'))
 { 
 valid = true; 
 } 
 else 
 { 
 cout << "Invalid code. Please try again." << endl; 
 } 
 }
} 





This was the original code given:

#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;
}
You are telling that your function will return something, but is not returning anything. Either return string from function or change definition to return nothing.
how do i change definition to return nothing?
closed account (j3Rz8vqX)
type name ( parameter1, parameter2, ...) { statements }

Change the type to void for both the prototype and function header.

http://www.cplusplus.com/doc/tutorial/functions/
YES! thank you so much! you guys are great :)
closed account (j3Rz8vqX)
Your welcome
A shot in the dark here.

I am working on the exact same problem, and I am running into the same error message that OP has.

I have tried changing the prototype and function names to void, but that did not work. I even copy/pasted OP's code and tried the same, but I get another error with the syntax.

Can someone please explain what I am doing wrong?

Here is my 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
#include <cstdlib>
#include <iostream>
using namespace std;

string getProductCode(string &);

int main()
{
string code = "";
string productCode = "";

productCode = getProductCode(code);

cout << "Product code read from keyboard: " << productCode << endl;

system("pause");
return 0;
}


string getProductCode(string & c)
{
string code = "";
string productCode = "";

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
closed account (j3Rz8vqX)
I misread the OP as well, the solution would be to add
return productCode;//Line 38 after valid = true;
The whole point of the problem is to not use a Return statement.

The problem with my code is that, although it works great when I enter a number that does NOT end in 4 or 7, I get a Windows error message that the program has crashed anytime the number entered end with 4 or 7.

Why is the program crashing?

Thanks for responding BTW. :)
Last edited on
closed account (j3Rz8vqX)
productCode = getProductCode(code);

You were expecting a string to return, so I would assume you'd want a return value of string...


Instead, I have to use pass-by-reference to achieve the same goal

Since you do not want a returning string, implement a void for the prototype and headers return types.

Modify the below line.
productCode=code;//Line 38 after valid = true;

Modify your call function to not expect a return.
Last edited on
Topic archived. No new replies allowed.