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.