help on function defition

This is a discussion on help on function defition within the C++ Programming forums, part of the General Programming Boards category; anytime I try to compile I get an error message saying "Local funtion definitions are illegal" , I think it ...

  1. #1
    Unregistered
    Guest

    help on function defition

    anytime I try to compile I get an error message saying "Local funtion definitions are illegal" , I think it has to do with the function prototype but I'm not sure what to do.


    #include <iostream.h>

    void top_score(char [][8],char [][8], int [][3], int, int);
    void main(void)
    {

    const int exam = 3;
    const int student= 4;
    char last_name[student][8];
    char first_name[student][8];
    int scores[student][exam];
    int j=0,top,counter=0;
    float average=0;




    for(int i=0; i<student; i++){
    cout<<"Enter first name for student "<<i+1<<": ";
    cin>>first_name[i];
    cout<<"Enter last name for student "<<i+1<<": ";
    cin>>last_name[i];
    }
    for(i=0;i<student; i++){
    for( j=0;j<exam; j++){
    cout<<"Enter score #"<<j+1<<"for "<<first_name[i]<<" "<<last_name[i]<<": ";
    cin>>scores[i][j];

    }
    top_score(first_name, last_name, scores, student, exam);
    }


    void top_score(char first[][8],char last[][8],int grades[][exam],int students,int exams){

    cout<<endl;
    top=grades[0][0];
    for(i=0;i<students;i++){
    for(j=0;j<exams;j++){
    if(grades[i][j]>top)
    top=grades[i][j];
    }

    cout<<"Student First Name: "<<first_name[i]<<" Student Last Name: "<<last_name[i]<<" Top Exam Score: "<<top<<"\n";
    top=0;
    }
    }

    for(i=0;i<student;i++){
    for(j=0;j<exam;j++){
    average=average+scores[i][j];
    counter++;

    }

    }

    cout<<"\n"<<"The Total Class: Average "<<average/counter<<endl;

    }

  2. #2
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,825
    > int grades[][exam]

    exam is out of scope in that. For that matter, almost all of your variables in your function are, too. Also, main returns int, not void.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,799
    That is because you are putting the code for the function inside the main function. You need to move that code outside of the main function. Try this instead:

    Code:
    #include <iostream.h> 
    
    void top_score(char [][8],char [][8], int [][3], int, int);
    
    void main(void) 
    { 
        const int exam = 3; 
        const int student= 4; 
        char last_name[student][8]; 
        char first_name[student][8]; 
        int scores[student][exam]; 
        int j=0,top,counter=0; 
        float average=0; 
    
        for(int i=0; i<student; i++)
        { 
            cout<<"Enter first name for student "<<i+1<<": "; 
            cin>>first_name[i]; 
            cout<<"Enter last name for student "<<i+1<<": "; 
            cin>>last_name[i]; 
        }
    
        for(i=0;i<student; i++)
        { 
            for( j=0;j<exam; j++)
            { 
                cout<<"Enter score #"<<j+1<<"for "<<first_name[i]
                     << " " <<last_name[i]<<": "; 
                cin>>scores[i][j]; 
            } 
            top_score(first_name, last_name, scores, student, exam); 
        } 
    
        for(i=0;i<student;i++)
        { 
            for(j=0;j<exam;j++)
            { 
                average=average+scores[i][j]; 
                counter++; 
            } 
        } 
    
        cout<<"\n"<<"The Total Class: Average " << average/counter << endl;
    
    }
    
    void top_score(char first[][8],char last[][8],int grades[][exam],int students,int exams)
    { 
    
        cout<<endl; 
        top=grades[0][0]; 
        for(i=0;i<students;i++)
        { 
            for(j=0;j<exams;j++)
            { 
                if(grades[i][j]>top) top=grades[i][j]; 
            } 
    
            cout<<"Student First Name: "<<first_name[i]<<" Student Last Name: "
                 <<last_name[i]<<" Top Exam Score: "<<top<<"\n"; 
            top=0; 
        }
    
    }
    There may be other errors, but this should address the error you mentioned.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,825
    > That is because you are putting the code for the function inside the main function. You need to move that code outside of the main function. Try this instead:


    Oh yeah, that too... I shoulda counted more carefully...

    Anyways, the errors I talked about are still there.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  4. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 01:28 PM
  5. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 06:05 PM

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