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); }