Hi Everyone,
I have to read data from a file into an array. I have that part figured out. I can't get my function to work. I have to write a sumArray, avgSales, highSales, and lowSales. Here is what I got so far. Could someone please help me with one of the functions. I think I can get the rest if I have a god example to follow. Thanks in advance.

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

//Constant array size declaration.
const int LOCATIONS = 20;

//Function Prototypes
void readData(int a[], int size, double& data);
double sumArray(double& totalSales);

int main()
{ 
   cout.setf(ios::fixed);
   cout.setf(ios::showpoint);
   cout.precision(2);
   ifstream in;
   char inFile[256];
   double a[LOCATIONS] = {0}, sales, totalSales = 0;
   int data = 0;

   //Prompts the user for the input file name.
   cout << "Enter the input file name: ";
   cin >> inFile;

   // Opens the stream and connects to the file.
   in.open(inFile);

   //Checks to see if the input file opened properly.
   //Displays an error message if file not opened.
   if(in.fail( ))
   {
      cout << "Input file opening failed.\n";
      exit(1);

      //Closes file explicitly.
      in.close( );
   }
   

   //Finds and prints the total sales amount.
   sumArray(totalSales);
   cout << "The total sales are $" << totalSales << endl;

   
   //Reads in and displays data.
   while(in >> a[data])
   {
      sales = a[data];
      data++;

   }
      cout << "Number Of Locations In File: " << a[0] << endl << endl;

   for(int index = 1; index < data; index++)
   {
      cout << "Location Number " << index << ": $" << a[index] << endl;
   }

}


   
double sumArray(double& totalSales)  
{ 
   ifstream in;
   double a[LOCATIONS] = {0}, sales;
   int data = 0;

   //Reads in and displays data.
   while(in >> a[data])
   {
    for(int index = 1; index < data; index++)
    {
      totalSales = totalSales + a[index];
      index++;
      return(totalSales); 
       
    }
   } 
   
}

My total sales output is $0.00, so I know that it's not reading in correctly. Please guide me.

Ancient Dragon commented: Thanks for using code tags +15

line 29: open() wants a const char* for the filename and you passed a std::string object. I'm supprised your compiler did not object and spew out some nasty error messages. It should be this: in.open(inFile.c_str()); line 39: unnecessary -- if open() failed the stream can not be closed.

line 73: input file was never opened so lines 73-82 will probably never get executed.

lines 75-80 should not be inside the while loop beginning at line 73 because total sales will get counted several times. Move that for loop outside and below the while loop.

Actually, in sumArray() you don't need that double array at all because you don't do anything with it. A better solution is to read the array into memory once in main() then pass the array to sumArray() function. Just add another parameter to sumArray().

line 29: open() wants a const char* for the filename and you passed a std::string object. I'm supprised your compiler did not object and spew out some nasty error messages. It should be this: in.open(inFile.c_str()); //This is the way the book showed us, it works fine. It reads and prints the data. I'm not sure how to use what you have suggested.

line 39: unnecessary -- if open() failed the stream can not be closed.//Our teacher advised us to always close a file.

line 73: input file was never opened so lines 73-82 will probably never get executed.//So how do I go abut doing that. Do I have it all set up wrong?

lines 75-80 should not be inside the while loop beginning at line 73 because total sales will get counted several times. Move that for loop outside and below the while loop.// I did that because I thought that it would read from the text file while there was data to be read.

Actually, in sumArray() you don't need that double array at all because you don't do anything with it. A better solution is to read the array into memory once in main() then pass the array to sumArray() function. Just add another parameter to sumArray(). //Could you please provide an example on how to do this. My book seems vague and I'm really struggling.

Thanks for all the feedback. Could you please provide some type of example that may help me understand this better. I seem to do better when I can see how something is supposed to work. I'm not asking you to write the code, just some type of example. Thanks again.

here's what the functions would look like

double sumArray(double a[LOCATIONS], double& totalSales) 
{

}

and here is how to call it from main () on line 44. And don't forget to change the function prototype on line 12 too.

sumArray(a, totalSales);

BTW: you should name arrays something other than one-letter names.

Thanks, I did change the array name to sales. Thanks for the tips.

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.