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