简体   繁体   中英

EXC_BAD_ACCESS when rerunning program

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

Check that you are not out of bound of array a in function getPints add check here

 while (!bloodInFile.eof())
 {
   if(i >= h)
       break;
   bloodInFile >> a[i];
   i++;
 }

Because if you can have more lines that MAX_HOURS in bloodDrive.txt file. Also I don't see that you initialize i somewhere i suppose it should be equal to 0 ( i=0 ), so you're function should looks like that

void getPints(double a[], int h)
{
    int i = 0;
    ifstream bloodInFile;

    bloodInFile.open("bloodDrive.txt");

    if (!bloodInFile)
        cout << "Cannot open file." << endl;

    while (!bloodInFile.eof())
    {
        if(i > h)
            break;
        bloodInFile >> a[i];
        i++;
    }

    bloodInFile.close();
}

PS as @JonathanLeffler said (and I agree with him 100%) it's better to use dynamic array ( vector eg) for such problem when you don't know how much input could be.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM