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