value-returning function help

This is a discussion on value-returning function help within the C++ Programming forums, part of the General Programming Boards category; I have been trying to work on this program that takes a "necklace program" that I wrote and puts the ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    2

    value-returning function help

    I have been trying to work on this program that takes a "necklace program" that I wrote and puts the results of that program into a multiplication table. I am doing it using a value returning function but keep getting the following errors and have no clue what they mean. Could anyone give me some tips?
    error C2082: redefinition of formal parameter 'num1'
    error C2082: redefinition of formal parameter 'num2'

    {


    Here is my program.

    Code:
    # include <cmath>
    # include <iostream>
    using namespace std;
    # include <iomanip>
    
    
    
    int necklace( int, int);
    
    int main ()
    {	
    	cout << setw(5) << " ";
    	int count;
    	count = 1;
    	while ( count <= 15)
    	{ 
    		cout << setw(5) << count;
    		count++;
    	}
    	cout << endl;
    	count = 1;
    	cout << setw(5) << " ";
    	while (count <=15)
    	{	
    		cout << setw(5) << "___";
    		count++;
    	}
    	cout << endl;
    
    	int column, row;
    	column = 1;
    	while (column <=15)
    	{
    		cout << setw(3) << column << "|";
    		row = 1;
    		while ( row <=15)
    		{	int l;
    		l = necklace(row, column);
    		cout << setw(5) << l;
    
    
    
    
    		row++;
    		}
    		cout << endl;
    		column++;
    
    	}
    
    }
    
    
    
    	int necklace( int num1, int num2)
    	{	
    
    		int num3, num4, input1, input2, length, newNumber, newDigit;
    		bool notDone;
    
    		input1=num3;
    		input2=num4;
    
    		length=0;
    		notDone= true;  
    		while (notDone)
    
    		{	
    			newNumber = num1+num2;
    			newDigit  = newNumber &#37; 10;
    			length = length+1;
    			num3 = num4;
    			num4 = newDigit;
    
    			if (num3 == input1 && num4 == input2)
    			{
    				notDone= false;
    			}
    		}
    		return length;
    	}
    Last edited by hurriedfrenzy@h; 11-05-2008 at 01:57 AM. Reason: fixed one error

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,775
    Define the necklace function after defining the main function.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    2
    Ok I fixed that problem but now the output is stopping after the first line and not continuing on down each row. Here is the updated code.
    Code:
    # include <cmath>
    # include <iostream>
    using namespace std;
    # include <iomanip>
    
    
    
    int necklace( int, int);
    
    int main ()
    {	
    	cout << setw(5) << " ";
    	int count;
    	count = 1;
    	while ( count <= 9)
    	{ 
    		cout << setw(5) << count;
    		count++;
    	}
    	cout << endl;
    	count = 1;
    	cout << setw(5) << " ";
    	while (count <=9)
    	{	
    		cout << setw(5) << "___";
    		count++;
    	}
    	cout << endl;
    
    	int column, row;
    	column = 1;
    	while (column <=9)
    	{
    		cout << setw(3) << column << "|";
    		row = 1;
    		while ( row <=15)
    		{	int l;
    		l = necklace(row, column);
    		cout << setw(5) << l;
    
    
    
    
    		row++;
    		}
    		cout << endl;
    		column++;
    
    	}
    
    }
    
    
    
    	int necklace( int num1, int num2)
    	{	
    
    		int  input1, input2, length, newNumber, newDigit;
    		bool notDone;
    
    		input1=num1;
    		input2=num2;
    
    		length=0;
    		notDone= true;  
    		while (notDone)
    
    		{	
    			newNumber = num1+num2;
    			newDigit  = newNumber % 10;
    			length = length+1;
    			num1 = num2;
    			num2 = newDigit;
    
    			if (num1 == input1 && num2 == input2)
    			{
    				notDone= false;
    			}
    		}
    		return length;
    	}
    Last edited by hurriedfrenzy@h; 11-05-2008 at 02:06 AM. Reason: fixed problem

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    If num1 and num2 are going to always store a single digits then num1 is never going to be equal to 10-15 (input1) and notDone will always be false.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    using namespace std;
    # include <iomanip>
    You should NEVER use a "using namespace XXX" before an include.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem in returning value from the dll exported function
    By dattaforit in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2006, 03:30 AM
  2. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  3. returning pointers from a function
    By curlious in forum C++ Programming
    Replies: 2
    Last Post: 12-28-2003, 10:37 PM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 06:18 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21