help with arrays and functions

This is a discussion on help with arrays and functions within the C++ Programming forums, part of the General Programming Boards category; See attached program please ?!? I don't know why I am getting these errors: error C2601: 'FUNCaveweight' : local function ...

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    27

    Question help with arrays and functions

    See attached program please ?!?

    I don't know why I am getting these errors:

    error C2601: 'FUNCaveweight' : local function definitions are illegal

    error C2601: 'maxminLengths' : local function definitions are illegal

    fatal error C1004: unexpected end of file found

    Attached Files Attached Files

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    only compiler error I get is that main is not closed properly. there is no closing brace to main. you close the while loop after the return statement in main. place the return statement after the brace it is currently before, and then close main with a closing brace.

    Code:
    }
    	return EXIT_SUCCESS;
    }
    instead of
    Code:
    return EXIT_SUCCESS;
    }
    I haven't looked over your code other than that, so I'm not sure if you have other errors.

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    1. Cut these 2 lines out & place them BEFORE main:
    Code:
    int FUNCaveweight(int Weight[100], string Species[100], string species);
    int maxminLengths (int Length[100], string Species[100], string species);
    2. You need a brace before your return from main;
    i.e.
    Code:
         {
         return EXIT_SUCCESS;
    {
    instead of your
    Code:
         return EXIT_SUCCESS;
         {
    Now it should compile OK. It won't do what you wanted it to do, but at least it will compile so you can start debugging the logic.


    By the way, in C++ it is customary to omit the "void" parameter;
    i.e.:
    Code:
    int main()
    rather than
    Code:
    int main (void)

  4. #4
    Registered User
    Join Date
    Mar 2003
    Posts
    27

    logic?

    can you help with why the logic isn't correct?

  5. #5
    Registered User
    Join Date
    Mar 2003
    Posts
    27

    I am so lost...

    I have spent 14+ hours, got help from my professor, and STILL don't have a clue what I'm doing!

    HELP!!
    Attached Files Attached Files

  6. #6
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    I took out some extraneous braces, rearranged the format a bit to make it easier to read, & added some comments to get you started...

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <iomanip>
    #include <string>
    using namespace std;
    
    //Project to input data on various fish
    //including species, weight and length
    //Determine ave weight, min & max length
    //and number of fish per species
    
    int FUNCaveweight(int Weight[100], string Species[100], string species);
    int maxminLengths (int Length[100], string Species[100], string species);
    
    int main (void)
    {
    	//declare
    	int Length [100];
    	int Weight [100];
    	string Species [100];
    	string species;
    	int l=0, pounds, ounces, aveweight=0, totalweight=0, count=0, minLength=1000, maxLength=0;
    	
    	//get input
    // this goes on forever, since Length is just the ADDRESS of the memory location containing Length[0]
    // did you really want this to run over & over again?
    	while (Length>=0)
    	{
    // in the for loop you enter your data into the arrays; looks ok
    		for (l=0;l<100;l++)
    		{
    			cout<<"Enter species of fish, length in inches and weight in pounds and ounces:"<<endl;
    			cout<<"Enter -1 as length to end."<<endl;
    			cin>>Species[l]>>Length[l]>>pounds>>ounces;
    			Weight[l]=pounds*16+ounces;
    		}
    
    // the first problem here is that Length and Species are not int variables, they are memory addresses
    // but even after you fix that, what are you doing here?  You're just calling the function over and
    // over again, not doing anything at all with the values that the function is returning.
    		//max & min lengths
    		maxminLengths (Length, Species, "bass");
    		maxminLengths (Length, Species, "bluegill");
    		maxminLengths (Length, Species, "perch");
    		maxminLengths (Length, Species, "walleye");
    		maxminLengths (Length, Species, "pike");
    
    // same problems here as in the preceding 5 lines
    		//aveweight
    		FUNCaveweight (Weight, Species, "bass");
    		FUNCaveweight (Weight, Species, "bluegill");
    		FUNCaveweight (Weight, Species, "perch");
    		FUNCaveweight (Weight, Species, "walleye");
    		FUNCaveweight (Weight, Species, "pike");
    
    		//format
    		cout.setf(ios::left);
    		cout.setf(ios::adjustfield);
    
    
    // in main, totalweight=0, minLength=1000, maxLength=0; note that these variables in main
    // are not affected by anything you do to variables with the same names in the other functions
    
    		//get returned info from functions
    		aveweight=totalweight/count;
    		cout<<species<<aveweight/16<<" lb "<<aveweight%16<<" ounces "<<minLength<<" in. to "<<maxLength<<" in. "<<count<<" fish"<<endl;
    	}
    
    		return EXIT_SUCCESS;
    }
    
    
    //function for aveweight
    int FUNCaveweight(int Weight[100], string Species[100], string species)
    {
    	int totalweight=0;
    	int t=0, count=0;
    	for (t=0;t<100;t++)
    	{
    		if (Species[t]==species)
    			totalweight+=Weight[t];
    		count++;
    	}
    	return totalweight/count;
    }
    
    
    //function for max and min lengths
    
    int maxminLengths(int Length[100], string Species[100], string species)
    {
    	int maxLength=0, minLength=1000, l=0;
    	for (l=0;l<100;l++)
    	{
    		if (Length[l]<maxLength)
    			maxLength=Length[l];
    		if (Length[l]>minLength)
    			minLength=Length[l];
    		if (maxLength==0)
    			maxLength=minLength;
    	}
    	return maxLength, minLength;
    
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. arrays, functions, HELP
    By beginner1 in forum C Programming
    Replies: 4
    Last Post: 05-20-2009, 04:29 PM
  2. Manipulating Character arrays in functions.
    By kbro3 in forum C++ Programming
    Replies: 11
    Last Post: 08-16-2008, 03:24 AM
  3. functions using arrays
    By trprince in forum C Programming
    Replies: 30
    Last Post: 11-17-2007, 06:10 PM
  4. Arrays and Functions
    By KunoNoOni in forum Game Programming
    Replies: 12
    Last Post: 10-04-2005, 10:41 PM
  5. Arrays out-of-bounds in functions only?
    By KneeLess in forum C Programming
    Replies: 5
    Last Post: 11-03-2004, 06:46 PM

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