简体   繁体   English

重新运行程序时EXC_BAD_ACCESS

[英]EXC_BAD_ACCESS when rerunning program

So this is a program I wrote for a CS lab in my class. 这是我在课堂上为CS实验室编写的程序。 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. 完成计算后,它询问用户是否要重新运行该程序,这只是main中的while循环。 Why do I get this error when I the program reruns? 重新运行程序时为什么会出现此错误?

Thread 1: EXC_BAD_ACCESS (code=1, address=0x7ffeb1d82bc8) 线程1:EXC_BAD_ACCESS(代码= 1,地址= 0x7ffeb1d82bc8)

it occurs at this line in the getPints function: 它发生在getPints函数的这一行:

bloodInFile >> a[i]; 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 检查您是否不在函数getPints的数组agetPints添加检查

 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. 因为如果你能有更多的线是MAX_HOURS在bloodDrive.txt文件。 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 我也没有看到你在某个地方初始化i ,我想它应该等于0( i=0 ),所以你的函数应该像这样

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. PS @JonathanLeffler说(我也100%同意他),当您不知道输入多少时,最好使用动态数组(例如矢量 )来解决此类问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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