Reusable function
I have a program that runs fine, but repeats the same code 4 times with different values. I am attempting to write a function that is reusable so I can eliminate the duplicate code. The first error I am getting is at the function call: cannot convert parameter 6 from 'const int' to 'int []'.
void testFunction(int, int, bool, int, const int, int [ ], char);
void testProgram()
{
//----Test Case 1
int guess1 = 0;
int guessCount1 = 0;
bool correctAnswer1 = false;
int num1 = 499;
const int arraySize1 = 9;
int testCaseArray1[ arraySize1 ] = { 500, 250, 375, 438, 469, 485, 493, 497, 499 };
char choice1 = 'Y';
testFunction(0, 0, false, 499, 9, arraySize1, 'Y');
}
void testFunction(int guess1, int guessCount1, bool false, int, int, int [ ], char 'Y')
{
cout << "\n\nI have a number between 1 and 1000.\n";
cout << "Can you guess my number?\n";
cout << "Please type your first guess. > ";
for( int index1 = 0; index1 < arraySize1; index1++ )
{
guess1 = testCaseArray1[ index1 ];
cout << guess1 << "\n";
guessCount1++;
if( guess1 == num1 )
{
cout << "\n\nExcellent! You guessed the number!";
if( guessCount1 < 10 )
{
cout << "\nIt took you " << guessCount1 << " guess(es).";
cout << "\nEither you know the secret or you got lucky!";
}
if( guessCount1 == 10 )
{
cout << "\nIt took you " << guessCount1 << " guesses.";
cout << "\nAhah! You know the secret!";
}
if( guessCount1 > 10 )
{
cout << "\nIt took you " << guessCount1 << " guesses.";
cout << "\nYou should be able to do better!";
}
cout << "\n\nWould you like to play again (y or n)?";
cout << "\n " << choice1;
if( toupper( choice1 ) == 'Y' )
{
}
if( toupper( choice1 ) == 'N' )
{
cout << "\n\nThanks for playing!";
correctAnswer1 = true;
}
}
if( guess1 < num1 )
{
cout << "\nToo low. Try again. > ";
}
if( guess1 > num1 )
{
cout << "\nToo high. Try again. > ";
}
}
}
[2561 byte] By [
ericelysia] at [2007-11-11 10:27:19]

# 1 Re: Reusable function
Hi,
two of your errors are here:
testFunction(0, 0, false, 499, 9, arraySize1, 'Y'); // here you probably want to pass your array , not its size
}
void testFunction(int guess1, int guessCount1, bool false, int, int, int [ ], char 'Y') // These are duff declarations of parameters
// should probably be sth like:
//void testFunction(int guess1, int guessCount1, bool myBool, int myInt1, int myInt2, int myArray[ ], char myChar='Y')
{
cout << "\n\nI have a number between 1 and 1000.\n";
cout << "Can you guess my number?\n";
There might be more, but they hurt my eye ;-)
Cheers,
D
# 2 Re: Reusable function
Thank you for the corrections. Now the compiler is telling me that the following are undeclared identifiers: arraySize1, testCaseArray1, num1, choice1, and correctAnswer1, starting with arraySize1 in the for loop.
void testFunction(int, int, bool, int, const int, int [ ], char);
//-----------------------
// test program begin here
void testProgram()
{
//----Test Case 1
int guess1 = 0;
int guessCount1 = 0;
bool correctAnswer1 = false;
int num1 = 499;
const int arraySize1 = 9;
int testCaseArray1[ arraySize1 ] = { 500, 250, 375, 438, 469, 485, 493, 497, 499 };
char choice1 = 'Y';
testFunction(0, 0, false, 499, arraySize1, testCaseArray1, 'Y');
}
void testFunction(int myGuess1, int myGuessCount1, bool myBool, int myInt1, int myInt2, int myArray[ ], char myChar='Y')
{
cout << "\n\nI have a number between 1 and 1000.\n";
cout << "Can you guess my number?\n";
cout << "Please type your first guess. > ";
for( int index1 = 0; index1 < arraySize1; index1++ )
# 3 Re: Reusable function
Hi,
you declare your variables in testProgram, but use them in testFunction.
They're out of scope.
Either you make them global, which is legal, but should (my opinion!) be avoided in times of object oriented programming, or you declare the variable you need inside testFunction.
Cheers,
D
# 4 Re: Reusable function
OK, right. I am still not able to pass the array in for some reason.
void testProgram()
{
//----Test Case 1
//int guess1 = 0;
//int guessCount1 = 0;
//bool correctAnswer1 = false;
//int num1 = 499;
const int arraySize1 = 9;
int testCaseArray1[ arraySize1 ] = { 500, 250, 375, 438, 469, 485, 493, 497, 499 };
char choice1 = 'Y';
testFunction(0, 0, false, 499, arraySize1, testCaseArray1, 'Y');
}
void testFunction(int myGuess1, int myGuessCount1, bool myBool, int myInt1, int myInt2, int myArray[ ], char myChar='Y')
{
cout << "\n\nI have a number between 1 and 1000.\n";
cout << "Can you guess my number?\n";
cout << "Please type your first guess. > ";
int num1 = myInt1;
const int arraySize1 = myInt2;
int guess1 = myGuess1;
int testCaseArray1[ ] = myArray;
int guessCount1 = 0;
char choice1 = myChar;
bool correctAnswer1 = myBool;
for( int index1 = 0; index1 < arraySize1; index1++ )
# 5 Re: Reusable function
I think I need a for loop to assign each element of myArray[] to testCaseArray1[].
# 6 Re: Reusable function
Can you explain what I am not understanding here?
void testFunction(int, int, bool, int, const int, int [ ], char); //function prototype
void testProgram()
{
//----Test Case 1
//int num1 = 499;
const int arraySize1 = 9;
int testCaseArray1[ arraySize1 ] = { 500, 250, 375, 438, 469, 485, 493, 497, 499 };
char choice1 = 'Y';
testFunction(0, 0, false, 499, arraySize1, testCaseArray1, 'Y');
}
void testFunction(int myGuess1, int myGuessCount1, bool myBool, int myInt1, int myInt2, int myArray[ ], char myChar='Y')
{
int num1 = myInt1;
const int arraySize1 = myInt2;
int guess1 = myGuess1;
int testCaseArray1[ arraySize1 ];
int guessCount1 = 0;
char choice1 = myChar;
bool correctAnswer1 = myBool;
cout << "\n\nI have a number between 1 and 1000.\n";
cout << "Can you guess my number?\n";
cout << "Please type your first guess. > ";
/*
for( int index = 0; index < arraySize1; index++ )
{
testCaseArray[ index ] = myArray[ index ];
}
*/
# 7 Re: Reusable function
what are you not understanding?
# 8 Re: Reusable function
I have no clue what you are asking, but some observations:
First off, why do you need to copy all your parameters to locals? Just use the parameters you passed. Also, consider better names. MyInt does not tell me a thing about how you will use this value.
Second, memcpy is a better (well, faster) way to copy simple (built in data type) arrays around, instead of a sluggish for loop. (Avoid memcpy on objects until you understand the issues).
Finally, I did not see any inputs. Where are your cin statements (read from the user) ?
And a design note, functions should generally do one task. This might be a complex task, or a simple one, but moving a monolith from main into a single function does not really make your code "modular" at all. Consider breaking up your tasks a little more to organize your code and your thoughts so you can more easily decide what needs doing next.
jonnin at 2007-11-11 21:06:00 >

# 9 Re: Reusable function
Adding the the useful tips from jonnin, when you have a large number of parameters, you should probably pack them in a single class or struct.
Danny at 2007-11-11 21:06:52 >
