I'm having problems with my program. here's what i need it to do...

  • Prompt the user for the name of the master file, and input it
  • Open the master file and read the names into an array (using the readNames function)
  • Print each name in the array to the screen (to verify the values)
  • Give the user the choice of calculating the averages and sums of each file directly (using the functions from Part 1) or by first loading the numbers into an array (using the functions from Part 2).
  • If the user chooses the functions from Part 1:
    • Loop through each element in the array of names
    • Call getSum and getAverage on each element, and print the results
  • If the user chooses the functions from Part 2:
    • Read all numbers into a 2-dimensional array using the readNumbers function from Part 3 (or read each of 5 1-dimensional arrays using the readNumbers function from Part 2).
    • Loop through each element in the 2-dimensional array (or each of the 5 1-dimensional arrays)
    • Call getSum and getAverage on each array, and print the results

here is what I have...the first function works but the other's do not...Any help would be greatly appreciated...

#include <iostream>  //Basic library of terms used by C++
#include <fstream>  //Allows the transfer of data to and from files
#include <string>
#define MAX_NUM 100  //Max Number of numbers
using namespace std;
double getAverage(string);
double getSum(string);
void readNumbers(string, double[100]);
double getSum(double[100]);
double getAverage(double[100]);
void readNames(string, string[5]);
void readNumbers(string[5], double[5][100]);
/*My own functions...*/
void file_sum_and_average_one(string);
void file_sum_and_average_two(string);
void master_file_sum_and_average(string);
int main()
{
 char selection;
 string filename;
 cout<<"Welcome! Please make your selection below"<<endl;//program opening
 cout<<"1.Read from one file and calculate the sum and average"<<endl;//choice one
 cout<<"2.Read from one file and calculate the sums and averages using arrays"<<endl;//two
 cout<<"3.Read from a master file and find the sum and average"<<endl; //three
 cout<<"4.Quit program"<<endl;//quits program
 cin>> selection;
 
    switch(selection)
    do{
     case '1':
      cout << "Please enter your filename" << endl;
      cin >> filename;
   file_sum_and_average_one(filename);
  break;
       
     case '2':
      cout << "Please enter your filename" << endl;
      cin >> filename;
      file_sum_and_average_two(filename);
     break;
     
     case '3':
      cout << "Please enter your filename" << endl;
      cin >> filename;
      master_file_sum_and_average(filename);
     break;
     
     case '4':
      cout << "That's all folks" << endl;
     break;
        default:
      cout <<"That is not a possible choice...try again"<< endl;
    }while (selection != 4);
 
 
 
}
double getSum(string filename)    //the get sum function
{
 double temp=0;     //sets a double temp to 0     
 double total=0;     //sets the initial total = 0
 double counter=0;    //sets the initial counter = 0
 ifstream file_in;    //opens ability to read in file
 file_in.open(filename.c_str()); //opens file
 while(!file_in.eof())   //as long as we're not at the end of the file....do
 {
  file_in >> temp;   //reads file into a temporary file     
  total = total + temp;  
  counter++;     //adds one to the counter
 }
    return total;     //our sum of the file 
} 
double getAverage(string filename) //the get average function
{
 double temp=0; 
 double counter=0;
 double total=0;
 double average=0;
 ifstream file_in;
 file_in.open(filename.c_str());
 
 while(!file_in.eof())
 {
  file_in >> temp; 
  counter++;
 }
  
  total=getSum(filename);
  average=total/counter;
  return average;
}
 

void readNumbers(string filename, double numArray[100])
{
 int counter=0;
 ifstream file_in;
 file_in.open(filename.c_str());
 while(!file_in.eof())
 {
  file_in >> numArray[counter];
  counter++;
  
 }
}
double getSum(double numArray[100])
{
 double total=0;
 for(int i=0; i<=100;i++)
  {
  total = total + numArray[i];
  }
 return total;
  
}
double getAverage(double numArray[100])
{
 double total=0;
 double counter = 0; //keep it as double since mismatched types are weird on division
 for(int i=0; i<=100;i++)
  
  {
  total = total + numArray[i];
  }
 double average = total/counter;
 return average;
}
void readNames(string masterfile, string filearray[5])
{
 ifstream file_in;
 file_in.open(masterfile.c_str());
 for(int i=0; i<5; i++)
 {
  file_in >> filearray[i];
 }
}
 
void readNumbers(string filearray[5], double numarray[5][100])
{
 
 for(int i=0; i<5; i++)
 {
  ifstream file_in;
  file_in.open(filearray[i].c_str());
  for(int j=0; j<100; j++)
  {
   file_in >> numarray[i][j];
  }
  file_in.close();
 }
}
void file_sum_and_average_one(string filename)
{
 double sum = getSum(filename);
 double average=getAverage(filename);
 cout << "The sum of the numbers in this file is " << sum <<"." << endl;
 cout << "The average of the numbers in this file is " << average <<"."<<endl;
}
void file_sum_and_average_two(string filename)
{
 double array[100];
 readNumbers(filename, array);
 double sum=getSum(array);
 double average=getAverage(array);
 cout << "The sum of the numbers in this file is " << sum <<"." << endl;
 cout << "The average of the numbers in this file is " << average << "." << endl;
}
void master_file_sum_and_average(string filename)
{
 string filearray[5];
 double numarray[5][100];
 readNames(filename, filearray);
 readNumbers(filearray, numarray);

>> cin >> filename;
If there are spaces in the filename and/or path the above will not work because it stops at the first space. In that case use getline().

while(!file_in.eof())   //as long as we're not at the end of the file....do
 {
  file_in >> temp;   //reads file into a temporary file     
  total = total + temp;  
  counter++;     //adds one to the counter
 }

The above is the incorrect way to code the loop -- do not use eof() because it will go through the loop one time too many.
Here is the correct way to code it.

while( file_in >> temp )
 {
  total = total + temp;  
  counter++;     //adds one to the counter
 }

>> for(int i=0; i<=100;i++)
The above loop is incorrect -- it counts one too many times. remove the '=' symbol, like this
for(int i=0; i < 100;i++)

>> double average = total/counter;
the value of counter is 0, so your program will get a divide by zero error at runtime.

there are probably other errors, but I stopped reading. fix these and repost if you have more questions.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.