-
Just a little snag
Okay something I need to know is it possible to call a value from a void function to another void function?
What I want to do is for the number value in sumTwoPrimes function and send it to void prime to check if it's prime or not and it will send back 'n' or 'y' meaning if it's not prime or is prime. including the value of i and ans. I think I put the code into this post correctly for once. :)
The error I get from primeAns1 and primeAns2 in the function of sumTwoPrimes is error: value not ignored as it ought to be.
ALSO: THIS is not the entire program. only the two void functions.
Code:
void prime(unsigned int &isPrime, char &primeAns)
{
unsigned int i;
if (isPrime == 0 || isPrime == 1)
{
primeAns = 'n';
}
for (i = 2; i < isPrime; i++)
{
if ((isPrime % i) == 0)
{
primeAns = 'n';
break;
}
if (i == (isPrime - 1))
{
primeAns = 'y';
}
}
}
void sumTwoPrimes(unsigned int &isPrime, char &primeAns, char&primeAns1, char &primeAns2)
{
unsigned int i;
unsigned int ans;
for (i = isPrime; i < isPrime; i--)
{
ans = isPrime % i;
primeAns1 = prime(ans, primeAns);
primeAns2 = prime(i, primeAns);
if (primeAns1 == 'y' && primeAns2 == 'y')
{
break;
}
}
}
-
No, make the function return a result.
Or pass a reference to where you want the answer stored.
-
I thought I did pass a reference because I have char &primeAns in both functions.
-
So perhaps
prime(ans, primeAns1);
prime(i, primeAns2);
-
Oh geez, lol.. That was a big Doh! Thanks salem.
-
overloading?
I'm just studying functions and overloading and call by reference in my books now. So can I just confirm what Salem is saying is to overload the function?
Thank you in advance :)
-
> is to overload the function?
No, just make your use of the function match it's declaration.
-
Oh right,
now I get you.
Thanks :)