So this is a program I wrote for a CS lab in my class. It was modified so that it would take in input from a text file and output to another text file. After it does the calculations, it asks the user if they want to rerun the program, which is just a while loop in main. Why do I get this error when I the program reruns?
Thread 1: EXC_BAD_ACCESS (code=1, address=0x7ffeb1d82bc8)
it occurs at this line in the getPints function:
bloodInFile >> a[i];
#include <iostream>
#include <string>
#include <iomanip>
#include <math.h>
#include <fstream>
using namespace std;
const int MAX_HOURS = 7;
void getPints(double a[], int h);
double getAverage(double a[], int h);
double getHigh(double a[], int h);
double getLow(double a[], int h);
void displayInfo(double a, double b, double c);
int main()
{
string again = "yes";
double pints[MAX_HOURS];
double getHigh(double pints[], int MAX_HOURS);
while (again == "yes")
{
getPints(pints, MAX_HOURS);
getHigh(pints, MAX_HOURS);
displayInfo(getAverage(pints, MAX_HOURS), getHigh(pints, MAX_HOURS), getLow(pints, MAX_HOURS));
cout << "Do you want to run again (yes or no)? ";
cin >> again;
}
return 0;
}
void getPints(double a[], int h)
{
int i;
ifstream bloodInFile;
bloodInFile.open("bloodDrive.txt");
if (!bloodInFile)
cout << "Cannot open file." << endl;
while (!bloodInFile.eof())
{
bloodInFile >> a[i];
i++;
}
bloodInFile.close();
}
double getAverage(double a[], int h)
{
int i;
double totalPints = 0;
double averagePints;
for (i = 0; i <= h - 1; i++)
totalPints = totalPints + a[i];
averagePints = totalPints / i;
return averagePints;
}
double getHigh(double a[], int h)
{
int i;
double highest = a[0];
for (i = 1; i < h; i++)
{
if (a[i] > highest)
highest = a[i];
}
return highest;
}
double getLow(double a[], int h)
{
int i;
double lowest = a[0];
for (i = 1; i < h; i++)
{
if (a[i] < lowest)
lowest = a[i];
}
return lowest;
}
void displayInfo(double a, double b, double c)
{
ofstream bloodOutFile;
bloodOutFile.open("bloodResults.txt");
bloodOutFile << "Average pints: " << setprecision(1) << showpoint<< fixed << a << endl;
bloodOutFile << "Highest pints: " << setprecision(1) << showpoint<< fixed << b << endl;
bloodOutFile << "Lowest pints: " << setprecision(1) << showpoint<< fixed << c << endl;
}