Nov 20, 2014 at 6:11pm UTC
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 Nov 20, 2014 at 6:13pm UTC
Nov 20, 2014 at 6:29pm UTC
and would that go in place of the "return" statement listed? not sure where to put it...
Nov 20, 2014 at 6:31pm UTC
You need to change your function declaration and definition. Since you did not put your code [co de]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.
Nov 20, 2014 at 6:46pm UTC
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;
}
}
}
Nov 20, 2014 at 6:53pm UTC
Please edit your post so that your code is [co de]between code tags[/code] so I can reference line numbers.
Nov 20, 2014 at 7:01pm UTC
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 Nov 20, 2014 at 7:08pm UTC
Nov 20, 2014 at 7:06pm UTC
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 Nov 20, 2014 at 7:09pm UTC
Nov 20, 2014 at 7:10pm UTC
You need to change line 8 the same way you changed line 21.
@giblit: I think you mean line 14, not 24
Nov 20, 2014 at 10:27pm UTC
Remove "string" from the start of line 23 - the variable already exists (it is the parameter).
Nov 21, 2014 at 2:12pm UTC
ok so i removed it, the program compiles, but it runs an error of termination...
Nov 21, 2014 at 2:55pm UTC
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 Nov 21, 2014 at 2:55pm UTC
Nov 21, 2014 at 5:43pm UTC
thanks for your help! I finally think i have it correct!