HELP!! Function Definition problem? Syntax Error I think..

This is a discussion on HELP!! Function Definition problem? Syntax Error I think.. within the C++ Programming forums, part of the General Programming Boards category; Hi Guys, I was wondering if any of you could help me out.. I am supposed to modify this code ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    11

    HELP!! Function Definition problem? Syntax Error I think..

    Hi Guys,
    I was wondering if any of you could help me out.. I am supposed to modify this code to user-defined functions but it keeps giving me an error for the first function definition after int main() so it is not compiling.. can any of you help me figure this problem out? I know the function goes after int main() but it is just not working.. any help would be appreciated.. thanks

    After the code:
    int CountNegatives(int count)
    {

    Error For Line 79: a function definition is not allowed here before "{" token

    Code:
    #include <iostream>
    #include <iomanip>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    //function prototypes
    int CountNegatives();
    double AverageNonNegatives();
    int SumRange(int,int);
    
    int main()
    {
        ifstream inFile;
        int i, choice, num, sum, num1, num2, count;
        double average;
    
        do
        {
                 cout << "1 - Count Negatives" << endl;
                 cout << "2 - Average Non-negatives" << endl;
                 cout << "3 - Sum Numbers" << endl;
                 cout << "4 - Quit" << endl;
                 cout << "Choice: " << endl;
                 cin >> choice;
                 
                 if(choice == 1)
                 {          
                     //function call for CountNegatives
                     count = CountNegatives();
                     
                     cout << "There are " << count << " negative numbers "
                     << "in the file \"pa6.numbers\"\n\n";
                 }
                 if(choice == 2)
                 {   
                     //function call for AverageNonNegatives
                     average = AverageNonNegatives();
                     
                    if (count > 0)
                    {
                        average = double(sum)/count;
                        cout << "The average of the " << count
                             << " non-negative numbers is "
                             << average << endl << endl;
                    }
                    else
                    {
                        cout << "No entries in the input file !!\n";
                        cout << "Average NOT calculated.\n\n";
                    }
                 if (choice == 3)
                 {
                    cout << "Enter beginning number: ";
                    cin >> num1;
                    cout << "Enter ending number: ";
                    cin >> num2;
                    
                    //function call for SumRange
                    sum = SumRange (num1,num2);
                      
                 if (choice == 4)
                 {
                     cout << "Thanks -- Bye !!!" << endl;
                 }
                 else
                 {
                     cout << "Invalid Menu Choice -- Try Again !!\n\n";
                 }
        } while (choice != 4);
        //System("Pause");
        return 0;
    }
    
    // function definitions
    
    int CountNegatives(int count)
    {
        int count;
        // count all negative numbers in the input file "pa6.numbers"
        inFile.open("pa6.numbers.cpp");
        if (inFile.fail())
        {
            cout << "Error opening input file -- pa5.numbers!\n";
            cout << "Program terminated.\n\n";
            exit(1);
        }
        count = 0;
        // priming read
        inFile >> num;
        while(!inFile.eof())
        {
            if (num < 0)
            count++;
        // repeat priming read
            inFile >> num;
        }
        inFile.close();
        inFile.clear();
        
        return count;
        exit(1);
    }
    
    double AverageNonNegatives(double average)
    {
        // average all non-negative numbers in the input file "pa6.numbers"
        inFile.open("pa6.numbers.cpp");
        if (inFile.fail())
        {
            cout << "Error opening input file -- pa6.numbers!\n";
            cout << "Program terminated.\n\n";
            exit(1);
        }
        count = 0;
        sum = 0;
        // priming read
        inFile >> num;
        while(!inFile.eof())
        {
            if (num >= 0)
            {
                count++;
                sum = sum + num;
            }
        // repeat priming read
        inFile >> num;
        }
        inFile.close();
        inFile.clear();
        
        return average;
        exit(1);
    }
    
    int SumRange(int num1, int num2)
    {
        double sum;
        // sum all numbers (inclusive) in a range (input by user)    
        sum = 0;
        for(i=num1;i<=num2;i++)
            sum = sum + i;
        cout << "The sum of the integers between " << num1
             << " and " << num2 << " (inclusive) is "
             << sum << endl << endl;
        
        return sum;
        exit(1);
    }

  2. #2
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    int CountNegatives(int count)
    int CountNegatives();

    There are inequalities between your prototype and the implementation of the function.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    You are also missing some closing braces.

  4. #4
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    yeah, just compiled it there and saw that too.

    You've got errors too in your functions. ifstream instances declared in main aren't transferrable to functions unless they are globally declared or passed into the function. I'm talking about

    ifstream inFile;

    in: CountNegatives and AverageNonNegatives.

    Same goes for pretty much all of these:

    ifstream inFile;
    int i, choice, num, sum, num1, num2, count;
    double average;

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    11

    i'm so lost...

    so do i have to declare the inFile variable again? also where am i missing brackets? i'm checking over it i don't see it.. thanks for your help guys

  6. #6
    Registered User
    Join Date
    Jun 2002
    Posts
    230
    Code:
    #include <iostream>
    #include <iomanip>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    //function prototypes
    int CountNegatives();
    double AverageNonNegatives();
    int SumRange(int,int);
    
    int main()
    {
        ifstream inFile;
        int i, choice, num, sum, num1, num2, count;
        double average;
    
        do
        {
                 cout << "1 - Count Negatives" << endl;
                 cout << "2 - Average Non-negatives" << endl;
                 cout << "3 - Sum Numbers" << endl;
                 cout << "4 - Quit" << endl;
                 cout << "Choice: " << endl;
                 cin >> choice;
                 
                 if(choice == 1)
                 {          
                     //function call for CountNegatives
                     count = CountNegatives();
                     
                     cout << "There are " << count << " negative numbers "
                     << "in the file \"pa6.numbers\"\n\n";
                 }
                 if(choice == 2)
                 {   
                     //function call for AverageNonNegatives
                     average = AverageNonNegatives();
                     
                    if (count > 0)
                    {
                        average = double(sum)/count;
                        cout << "The average of the " << count
                             << " non-negative numbers is "
                             << average << endl << endl;
                    }
                    else
                    {
                        cout << "No entries in the input file !!\n";
                        cout << "Average NOT calculated.\n\n";
                    }
    
                 if (choice == 3)
                 { 
                    cout << "Enter beginning number: ";
                    cin >> num1;
                    cout << "Enter ending number: ";
                    cin >> num2;
                    
                    //function call for SumRange
                    sum = SumRange (num1,num2);
                      
                 if (choice == 4)
                 {
                     cout << "Thanks -- Bye !!!" << endl;
                 }
                 else
                 {
                     cout << "Invalid Menu Choice -- Try Again !!\n\n";
                 }
        } while (choice != 4);
        //System("Pause");
        return 0;
    }
    
    // function definitions
    
    int CountNegatives(int count)
    {
        int count;
        // count all negative numbers in the input file "pa6.numbers"
        inFile.open("pa6.numbers.cpp");
        if (inFile.fail())
        {
            cout << "Error opening input file -- pa5.numbers!\n";
            cout << "Program terminated.\n\n";
            exit(1);
        }
        count = 0;
        // priming read
        inFile >> num;
        while(!inFile.eof())
        {
            if (num < 0)
            count++;
        // repeat priming read
            inFile >> num;
        }
        inFile.close();
        inFile.clear();
        
        return count;
        exit(1);
    }
    
    double AverageNonNegatives(double average)
    {
        // average all non-negative numbers in the input file "pa6.numbers"
        inFile.open("pa6.numbers.cpp");
        if (inFile.fail())
        {
            cout << "Error opening input file -- pa6.numbers!\n";
            cout << "Program terminated.\n\n";
            exit(1);
        }
        count = 0;
        sum = 0;
        // priming read
        inFile >> num;
        while(!inFile.eof())
        {
            if (num >= 0)
            {
                count++;
                sum = sum + num;
            }
        // repeat priming read
        inFile >> num;
        }
        inFile.close();
        inFile.clear();
        
        return average;
        exit(1);
    }
    
    int SumRange(int num1, int num2)
    {
        double sum;
        // sum all numbers (inclusive) in a range (input by user)    
        sum = 0;
        for(i=num1;i<=num2;i++)
            sum = sum + i;
        cout << "The sum of the integers between " << num1
             << " and " << num2 << " (inclusive) is "
             << sum << endl << endl;
        
        return sum;
        exit(1);
    }
    The two red braces dont have closing braces.
    C++ Rules!!!!
    ------------
    Microsoft Visual Studio .NET Enterprise

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> where am i missing brackets?
    You have good indentation in your code, so you can see when the code loses a level of indentation without a closing brace.

  8. #8
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Yes, you will have to declare the inFile variable again. Because you declared it in main, it only exists in main. Observe:

    Code:
    int main( void )
    {
      int thing = 1;
      std::cout<< thing; // Fine!
    }
    Code:
    void whatever( void )
    {
      std::cout<< thing; // While this variable is declared in main, its scope is not extended to this function.
    }
    
    int main( void )
    {
      int thing = 1;
      whatever();
    }
    Code:
    void whatever( int thing )
    {
      std::cout<< thing; // This is 'passed' into the function.
    }
    
    int main( void )
    {
      int thing = 1;
      whatever( thing );
    }
    Make sense? Just because things are declared in main doesn't mean they are accessible to functions which are being used in main.

  9. #9
    Registered User
    Join Date
    Oct 2006
    Posts
    11

    OK I'm getting it a little bit.. Function Definition problem??

    OK I finally got my function definition to run properly but now my code that i had for 2) Average Non Negative Numbers are not working!!

    In my code, i am reading from file pa6.numbers.cpp which is:
    34 67 -2 0 52 13 8 7 14 31 -90 49 64 82 -67 33 -43 41 42
    199 -2006 18 15 6 -9 12 16 -60 57 45 48 -57 1 4 -5 -14

    When choice==2, it should print out:
    "The average of the 26 non-negative numbers is 36.8462."

    Before i made the code with user-defined functions that was the answer i got.. now i get
    "The average of 2089878893 non-negative numbers is 0.00113328"

    HELP!! I'm LOST.. b/c i did not change any of the code when the whole if statement was in main.. now that is in a user defined function definition AFTER main, it screwed that statement up.. anyone???

    Thanks guys.

    Felix


    Code:
    #include <iostream>
    #include <iomanip>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    //function prototypes
    int CountNegatives();
    double AverageNonNegatives();
    int SumRange(int, int);
    
    int main()
    {
        ifstream inFile;
        int i, choice, num, sum, num1, num2, count;
        double average;
    
        do
        {
                 cout << "1 - Count Negatives" << endl;
                 cout << "2 - Average Non-negatives" << endl;
                 cout << "3 - Sum Numbers" << endl;
                 cout << "4 - Quit" << endl;
                 cout << "Choice: " << endl;
                 cin >> choice;
                 
                 if(choice == 1)
                 {          
                     //function call for CountNegatives
                     count = CountNegatives();
                     
                     cout << "There are " << count << " negative numbers "
                     << "in the file \"pa6.numbers\"\n\n";
                 }
                 else if(choice == 2)
                 {   
                     //function call for AverageNonNegatives
                     average = AverageNonNegatives();
                     
                    if (count > 0)
                    {
                        average = double(sum)/count;
                        cout << "The average of the " << count
                             << " non-negative numbers is "
                             << average << endl << endl;
                    }
                    else
                    {
                        cout << "No entries in the input file !!\n";
                        cout << "Average NOT calculated.\n\n";
                    }
                 }
                 else if (choice == 3)
                 {
                    cout << "Enter beginning number: ";
                    cin >> num1;
                    cout << "Enter ending number: ";
                    cin >> num2;
                    
                    //function call for SumRange
                    sum = SumRange (num1,num2);
                 }
                 else if (choice == 4)
                 {
                     cout << "Thanks -- Bye !!!" << endl;
                 }
                 else
                      cout << "Invalid Menu Choice -- Try Again !!\n\n";
        } while (choice != 4);
        //System("Pause");
        
    
        return 0;
    }
    
    // function definitions
    
    int CountNegatives()
    {
        int count, num;
        ifstream inFile;
        
        // count all negative numbers in the input file "pa6.numbers"
        inFile.open("pa6.numbers.cpp");
        if (inFile.fail())
        {
            cout << "Error opening input file -- pa5.numbers!\n";
            cout << "Program terminated.\n\n";
            exit(1);
        }
        count = 0;
        // priming read
        inFile >> num;
        while(!inFile.eof())
        {
            if (num < 0)
            count++;
        // repeat priming read
            inFile >> num;
        }
        inFile.close();
        inFile.clear();
        
        return count;
        exit(1);
    }
    
    double AverageNonNegatives()
    {
        int num, count, sum;
        double average;
        ifstream inFile;
        
        // average all non-negative numbers in the input file "pa6.numbers"
        inFile.open("pa6.numbers.cpp");
        if (inFile.fail())
        {
            cout << "Error opening input file -- pa6.numbers!\n";
            cout << "Program terminated.\n\n";
            exit(1);
        }
        
        inFile >> num;
        count = 0;
        sum = 0;
        while (!inFile.eof())
        {
            if(num >= 0)
            {
                count++;
                sum = num + sum;
            }
            inFile >> num;
        }
        inFile.close();
        inFile.clear();
        
        return average;
        exit(1);
    }
        
    int SumRange(int num1, int num2)
    {
        int i, sum;
        // sum all numbers (inclusive) in a range (input by user)    
        sum = 0;
        for(i=num1;i<=num2;i++)
            sum = sum + i;
        cout << "The sum of the integers between " << num1
             << " and " << num2 << " (inclusive) is "
             << sum << endl << endl;
        
        return sum;
        exit(1);
    }

  10. #10
    Registered User
    Join Date
    Oct 2006
    Posts
    11

    Different output for Choice=2.

    Now when i choose choice 2, I am getting "The average of the 10 non-negative numbers is 1"

    When there are 26 non negative numbers with an average of 36.8462.

    AHHH I'm about to go nuts!! can anyone help??

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    You need to pay attention to your local variables and how you use them. A variable that you create and set inside a function like AverageNonNegatives does not update variables with the same name declared in main. So you are using count and sum inside main when they haven't been set.

    Try using the average variable you already have in AverageNonNegatives, and actually set a value in it before you return it. Then use the return value of the function instead of doing calculations inside the if in main. Notice how you did this correctly for CountNegatives.

  12. #12
    Registered User
    Join Date
    Oct 2006
    Posts
    11
    Ahh, i see I re-arranged everything, but and I get the right average. But the count of choice=2, I keep getting Count = 10. When count is supposed to be 26.

    I changed it to :
    Code:
    //******************************************************************************
    //Author: Felix Gunawan
    //CLID: gxf6472
    //Class: CMPS 150 Section 6
    //Assignment: pa6
    //Date Assigned: Monday, October 30, 2006
    //Due Date: 10:00 PM, Friday, November 3, 2006
    //Description: This program provides a menu of options for the user using a
    //             do {} while loop and a while/for loop inside it.  The user can 
    //               pick 1-4 to Count Negatives, Average Non-negatives, Sum number, 
    //               or quit.  This program is the edited pa5.cpp version for 
    //               user-defined functions
    //Certification of Authenticity:
    //I certify that this assignment is entirely my own work.
    //******************************************************************************
    
    #include <iostream>
    #include <iomanip>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    //function prototypes
    int CountNegatives();
    double AverageNonNegatives();
    int SumRange(int, int);
    
    int main()
    {
        ifstream inFile;
        int i, choice, num, sum, num1, num2, count;
        double average;
    
        do
        {
                 cout << "1 - Count Negatives" << endl;
                 cout << "2 - Average Non-negatives" << endl;
                 cout << "3 - Sum Numbers" << endl;
                 cout << "4 - Quit" << endl;
                 cout << "Choice: " << endl;
                 cin >> choice;
                 
                 if(choice == 1)
                 {          
                     //function call for CountNegatives
                     count = CountNegatives();
                     
                     cout << "There are " << count << " negative numbers "
                     << "in the file \"pa6.numbers\"\n\n";
                 }
                 else if(choice == 2)
                 {   
                     //function call for AverageNonNegatives
    
                     average = AverageNonNegatives();
                        cout << "The average of the " << count
                        << " non-negative numbers is "
                        << average << endl << endl;
                 }
                 else if (choice == 3)
                 {
                    cout << "Enter beginning number: ";
                    cin >> num1;
                    cout << "Enter ending number: ";
                    cin >> num2;
                    
                    //function call for SumRange
                    sum = SumRange (num1,num2);
                 }
                 else if (choice == 4)
                 {
                     cout << "Thanks -- Bye !!!" << endl;
                 }
                 else
                      cout << "Invalid Menu Choice -- Try Again !!\n\n";
        } while (choice != 4);
        //System("Pause");
        
    
        return 0;
    }
    
    // function definitions
    
    int CountNegatives()
    {
        int count, num;
        ifstream inFile;
        
        // count all negative numbers in the input file "pa6.numbers"
        inFile.open("pa6.numbers.cpp");
        if (inFile.fail())
        {
            cout << "Error opening input file -- pa5.numbers!\n";
            cout << "Program terminated.\n\n";
            exit(1);
        }
        count = 0;
        // priming read
        inFile >> num;
        while(!inFile.eof())
        {
            if (num < 0)
            count++;
        // repeat priming read
            inFile >> num;
        }
        inFile.close();
        inFile.clear();
        
        return count;
        exit(1);
    }
    
    double AverageNonNegatives()
    {
        int num, count, sum;
        double average;
        ifstream inFile;
        
        // average all non-negative numbers in the input file "pa6.numbers"
        inFile.open("pa6.numbers.cpp");
        if (inFile.fail())
        {
            cout << "Error opening input file -- pa6.numbers!\n";
            cout << "Program terminated.\n\n";
            exit(1);
        }
        
        inFile >> num;
        count = 0;
        sum = 0;
        while (!inFile.eof())
        {
            if(num >= 0)
            {
                count++;
                sum = num + sum;
            }
            inFile >> num;
        }
        inFile.close();
        inFile.clear();
        
        average = double(sum)/count;
        return average;
        return count;
        exit(1);
    }
        
    int SumRange(int num1, int num2)
    {
        int i, sum;
        // sum all numbers (inclusive) in a range (input by user)    
        sum = 0;
        for(i=num1;i<=num2;i++)
            sum = sum + i;
        cout << "The sum of the integers between " << num1
             << " and " << num2 << " (inclusive) is "
             << sum << endl << endl;
        
        return sum;
        exit(1);
    }

  13. #13
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > return average;
    > return count;
    > exit(1);
    You can only return one value. To return another value like count, make it an argument of AverageNonNegatives
    Code:
    double AverageNonNegatives(int &count)
    > exit(1);
    You don't need this line, as once a return is seen, the function returns to the calling function.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  2. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 02:49 PM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  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