Thread: Just a little snag

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    59

    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;
    				}
    		}
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    No, make the function return a result.
    Or pass a reference to where you want the answer stored.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    59
    I thought I did pass a reference because I have char &primeAns in both functions.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    So perhaps

    prime(ans, primeAns1);
    prime(i, primeAns2);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Feb 2005
    Posts
    59
    Oh geez, lol.. That was a big Doh! Thanks salem.

  6. #6
    Registered User
    Join Date
    Feb 2003
    Posts
    62

    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

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > is to overload the function?
    No, just make your use of the function match it's declaration.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Feb 2003
    Posts
    62
    Oh right,
    now I get you.

    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Windows upgrade snag
    By Abiotic in forum Tech Board
    Replies: 4
    Last Post: 10-26-2003, 04:40 PM