Thread: Puhhhlease Help Me!!

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

    Array Errors... can anyone explain?

    I dont know how to use arrays... that is a given... but I am trying. I keep getting errors in my program when I refer to an Array for some reason... mostly with errors like:

    error C2040: '<' : 'int [100]' differs in levels of indirection from 'const int'
    error C2446: '>' : no conversion from 'const int' to 'int *'

    or:

    error C2065: 'ave' : undeclared identifier
    error C2373: 'ave_bal' : redefinition; different type modifiers
    : see declaration of 'ave_bal'
    error C2601: 'ave_bal' : local function definitions are illegal

    and it is messing up my program so bad I cant even run it at this point. Here is my program... still a HUGE work in progress.

    Code:
    #include <iostream.h>
    #include <iomanip.h>
    
    //function prototype
    
    double ave_bal(int, int);
    
    //main function
    int main ()
    {
    	
    	
    	//variable declarations
    	
    	int n, j;
    	int ID [100] = {0};
    	int bal [100] = {0};
    	double ave_bal;
    	double sum_bal;
    	
    	//Loop to input the ID and balance
    
    	for (n=0; n<=99; n++)
    	{
    
    	
    	cout<<"Please enter the account ID and account balance, -1 to to stop: "<<endl;
    	cin<<ID<<bal;
    
    	
    	//Loop to make sure data is within limits
    
    	if (ID < 1000 || ID	> 9999)
    	{	
    		cout<<"Invalid ID, please reenter: "<<endl;
    	}
    	
    	if (bal > 10000)
    	{
    		cout<<"Invalid Balance, please reenter: "<<endl;
    	}
    		
    	
    	
    	//display output in table form
    
    	cout<<"ID Number"<<setw(16)<<"Balance"<<setw(20)<<"Difference from Average"<<endl;
    
    		for ( ID = 0; ID < n; ID = ID + 1)
    		{
    			cout<<setw(4)<<ID + 1<<setprecision(2)
    				<<setiosflags(ios::fixed | ios::showpoint)
    				<<setw(16)<<bal<<setw(20)<<setprecision(2)
    				<<setiosflags(ios::fixed | ios:: showpoint)
    				<<ave<<endl;
    		}
    	
    	
    	
    	
    	return 0;
    	}
    	
    	
    	//function to determine average balance
    	double ave_bal(int ID [], int num_bal)
    	{	
    		double subtotal = 0;
    		double total;
    
    		for (int j = 0; j< num_bal; j++)
    		{
    			subtotal += ID [j];
    		}
    			total = subtotal / num_bal;
    
    		return total;
    
    	}
    }
    The program is suppsed to take in data entered by the user to figure the average account balance of all balances entered, and then print out a table showing the ID#, that ID's Balance, and the difference from the average that ID is. I know to most of you, this is really easy... but I am a definite n00b and am having a hard time with it. I am down to 14 errors in my program now though

    Thanks for any help or pointers you can give me! I definitely need it!!
    Last edited by GrlNewB; 04-10-2003 at 07:27 PM.

  2. #2
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    For starters...

    If you declare an array...
    int ID [100] ;

    You need to do something like this:

    cin >> ID[n]; // Always refer to a specific element in the array

    SUGGESTION:
    Don't write the whole thing and then compile. Write a little, then compile. Eliminate errors before repeating. This will help you track-down your errors, and will make it less likely to get 14 errors at a time.

    [EDIT]
    And, what kind of a title is Puhhhlease Help Me!!? Try to be more specific in the future, because not everyone reads all posts. You want to catch the attention of someone who may know the answer to your question. Some people ignore "help me" titles!
    Last edited by DougDbug; 04-10-2003 at 07:17 PM.

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    24
    hey... thanks for the help. I am sorry about my subject... I am getting kinda freaked out about the whole thing. I posted an earlier topic called "array functions" but it went dead really fast. Wont happen again

    Thanks for the tip about writing code and compiling it then adding more to it... that would really help my situation because I have so many errors its hard to tell one from the other and figure out where the exact location of the problem is.

    I still seem to be having a problem with "const int to int" errors, and I am not exactly sure why. I made the change you mentioned and put a variable inside my brackets for the array, but obviously, that did not fix my other, just as frequent problem, the "const int to int" one.

    Thanks again for your help... and sorry about the subject... I realize why many skipped over my topic now.

  4. #4
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    More observations...

    error C2065: 'ave' : undeclared identifier
    Maybe change
    <<ave<<endl;
    to this:
    <<ave_bal<<endl; (?)
    error C2373: 'ave_bal' : redefinition; different type modifiers
    : see declaration of 'ave_bal'
    error C2601: 'ave_bal' : local function definitions are illegal
    You have a both a variable and a function with the same name. Change one of the names
    How about:
    double find_ave_bal(int, int);

    You can use nouns for variable names and verbs for functions.

    When you get to classes... class / object names are usually nouns too.

  5. #5
    Registered User
    Join Date
    Mar 2003
    Posts
    24
    Wow... that really helped my program!! Lol... only one error remains...

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

    and it is pointing to my function:

    Code:
    //function to determine average balance
    
    	double find_ave_bal(int ID [], int num_bal)
    	{	
    		double subtotal = 0;
    		double total;
    
    		for (int j = 0; j< num_bal; j++)
    		{
    			subtotal += ID [j];
    		}
    			total = subtotal / num_bal;
    
    		return total;
    
    	}
    }
    I tried putting the name "find_ave_bal" in my main program but got even more errors with that. I am still not sure why I am getting this error. When I call the function, I am calling it like this:

    Code:
    {
    			cout<<setw(4)<<ID + 1<<setprecision(2)
    				<<setiosflags(ios::fixed | ios::showpoint)
    				<<setw(16)<<bal<<setw(20)<<setprecision(2)
    				<<setiosflags(ios::fixed | ios:: showpoint)
    				<<find_ave_bal<<endl;
    		}
    I was thinking... do I need to change the "find_ave_bal" to something like:

    Code:
    <<find_ave_bal (int, int)<<endl;
    I am not really sure about that



    [edit]

    it seems like no matter what I do, I still keep getting the "local function definitions are illegal" error. I changed some things around and it didnt seem to fix it. Any ideas?
    Last edited by GrlNewB; 04-10-2003 at 07:57 PM.

  6. #6
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    local function definition in my book always means one thing!

    A missing brace somewhere so that a function you think has ended actually hasnt. check the function immediately above the one its complaining about.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  7. #7
    Registered User
    Join Date
    Mar 2003
    Posts
    24
    lol... this is the code right before it and I cant see any problems. Of course, I have been staring at this program for a couple of hours now and am probably just oblivious to it now

    can you see it?

    Code:
    cout<<"ID Number"<<setw(16)<<"Balance"<<setw(20)<<"Difference from Average"<<endl;
    
    		for ( ID[j] = 0; ID[j] < n; ID[j] = ID[j] + 1)
    		{
    			cout<<setw(4)<<ID [j] + 1<<setprecision(2)
    				<<setiosflags(ios::fixed | ios::showpoint)
    				<<setw(16)<<bal<<setw(20)<<setprecision(2)
    				<<setiosflags(ios::fixed | ios:: showpoint)
    				<<find_ave_bal (ID [j], num_bal)<<endl;
    		}
    		
    	return 0;
    	}
    GrlNewB

  8. #8
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    Hmmm....

    If you don't find the missing brace, maybe you should post the whole thing again. Stoned_Coder knows his shtuff!

    You mght be doing something wrong when you "call" the functin. Leave out the variable "types".

    This won't work:
    cout << double find_ave_bal(int ID [], int num_bal)

    This is OK:
    cout << find_ave_bal(ID [], num_bal)

    [EDIT]
    BTW - If you leave-out that function. Does the rest of the program compile and work as expected?
    Last edited by DougDbug; 04-10-2003 at 08:46 PM.

  9. #9
    Registered User
    Join Date
    Mar 2003
    Posts
    24
    cool... I will make the changes! Thanks!


    [edit]

    ok... made the change to the array like you mentioned and am now getting this error in addition to my previous "local function" error

    error C2059: syntax error : ']'

    bummer
    Last edited by GrlNewB; 04-10-2003 at 08:46 PM.

  10. #10
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    Actually, I'm not sure you can pass ID[] to a function... Let me look into that....

  11. #11
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065
    Originally posted by DougDbug
    Actually, I'm not sure you can pass ID[] to a function... Let me look into that....
    Sure you can.

    This compiles fine:
    Code:
    #include <iostream>
    using std::cout;
    using std::endl;
    
    void Test(int array[], int size)
    {
        for(int i=0;i<size;i++) cout<<array[i]<<endl;
    }
    
    int main(void)
    {
       int int_array[5];
       for(int j=0;j<5;j++) int_array[j]=j*2;
       Test(int_array,5);
       return(0);
    }

  12. #12
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    Thumbs up Thanks for the example jdinger

    I knew somebody would know that without looking it up.

    But, I was right to question what I had posted because...
    This is NOT OK:
    cout << find_ave_bal(ID [], num_bal);

    You pass a pointer, and ID is a pointer ID[] is nothing in this statement!

  13. #13
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    Wink This should help

    This gets a bit confusing, because "n" is a user number which is sort-of hidden from the user who has an account number.

    I would have written and debugged the stuff in main() first, and then worked on find_ave_bal()... But, that function seems to be causing your compile errors.

    So, what you want to pass to find_ave_bal() is an array of balances, NOT an array of users!

    Here, main() is just a "driver function" to test find_avg_bal() which seems to be working for me.
    Code:
    #include <iostream.h>
    #include <iomanip.h>
    
    //function prototype
    double find_ave_bal(int[], int);
    
    //main function
    int main ()
    {    
         //variable declarations    
         int n;
         int ID[100] = {0}; ;  	
         int num_bal;   
         int bal[100] = {0};
         double ave_bal;
        	 
    //    Some sample data for debugging average function average = $200
         bal[0] = 100;   // User #0 balance = $100
         bal[1] = 200;   // User #1 balance = $200
         bal[2] = 300;   // User #3 balance = $300 
    
         ID[0] = 1000;	// User #0 is Account #1000
         ID[1] = 1005;	// User #1 is Account #1005
         ID[2] = 2000;	// User #2 is Account #2000
    
          num_bal = 3;
     
         ave_bal = find_ave_bal(bal, num_bal);	// In C++ bal is a pointer to bal[]  
         cout << "Average = " << ave_bal;   
            
    return 0;
    }
        
        
    //function to determine average balance
    double find_ave_bal(int bal[], int num_bal)
    {   
         double subtotal = 0;
       
         for (int j = 0; j <= num_bal; j++)  // Add
              subtotal += bal[j];
    
    return subtotal / num_bal;
    }
    Last edited by DougDbug; 04-10-2003 at 10:37 PM.

  14. #14
    Registered User
    Join Date
    Mar 2003
    Posts
    24
    Wow... you are awesome! Thanks for all your help!!

Popular pages Recent additions subscribe to a feed