0

When running my C++ program with xCode, it is facing a SIGABRT error every time the program is run. The program runs fine, it just outputs this error at the very end of execution.

I have tried commenting out different parts of my different functions, looked in the debugger at all the variable values, and everything seems to check out, I just can't figure out why my program crashes at the very end.

Here is the code:


#include <iostream>
#include <fstream>
#include <string>
#include <ctime>
int output(int, int[]);
void input(int, int[], int&, int&, int&, int&, double&, int );
void display(int, int, int , int , double);

using namespace std;

int main()
{
    int const SIZE = 5000;
    int numArray[SIZE] = {0};
    int lowest, highest;
    int total =0;
    double average = 0;
    int count = 0;
    
    int fileSize = output(SIZE, numArray);
    input(SIZE, numArray, lowest, highest, total, count, average, fileSize);
    
    display(count, lowest, highest, total, average);
      
    return 0;
}

int output(int SIZE, int numArray[])
{
    ofstream outputFile;
    int random;
   
    unsigned seed = time(0);
    srand(seed);
    int const MIN = 4000;
    int const MAX = 5000;

    outputFile.open("numbers.txt");
    
    int fileSize = (rand() % (MAX - MIN + 1) + MIN);
    cout << fileSize << endl;
    
    for (int x = 0; x < fileSize ; x++)
    {
        random = (rand() % (MAX - MIN + 1) + MIN);
        outputFile << random << endl;
        numArray[x] = random;

    }
    
    for (int x = 5000; x >= fileSize ; x--)
    {
        numArray[x] = 0;
    }
    
    outputFile.close();
    
    return (fileSize);
}

void input(int SIZE, int numArray[], int& lowest, int& highest, int& total, int& count, double& average, int fileSize)
{
    string fileName;
    ifstream inputFile;

    cout << "Enter the file name: ";
    getline (cin, fileName);

    inputFile.open(fileName);

    lowest = numArray[0];
    highest = numArray[0];

    for (int i = 0; i < fileSize; i++)
    {
        count = i+1;
        total += numArray[i];
        average = static_cast<double> (total)/fileSize;
        cout << numArray[i] << endl;

        if (numArray[i] < lowest)
            lowest = numArray[i];

        if (numArray[i] > highest)
            highest = numArray[i];
    }
    inputFile.close();
}

void display(int count, int lowest, int highest, int total, double average)
{

    
    cout << "The total count of numbers in the Array: " << count <<endl;
    cout << "The lowest number in the array: " << lowest << endl;
    cout << "The highest number in the array: " << highest << endl;
    cout << "The total of the numbers in the array: " << total << endl;
    cout << "The average of the numbers in the array: " << average << endl;
    
    return;
}

  • 1
    Btw; Why are you using the crappy old C random functions? There are much better [options](https://en.cppreference.com/w/cpp/numeric/random) available in modern C++. – Jesper Juhl May 03 '23 at 02:23
  • This is a school assignment, so I am required to use these specific functions. – Mason Thomas May 03 '23 at 02:24
  • 1
    Find a better school then. – Jesper Juhl May 03 '23 at 02:26
  • 1
    I don't see how this is a productive comment. – Mason Thomas May 03 '23 at 02:40
  • 2
    `numArray[5000]` is out of bounds. `for (int x = 5000; x >= fileSize ; x--)` Use AddressSanitizer if you can to try and help find out of bounds issues. – Retired Ninja May 03 '23 at 02:43
  • Why are you using "C" style arrays. C++ has std::vector/std::array and range based for loops just to avoid this kind of problems. – Pepijn Kramer May 03 '23 at 03:34
  • As said you have a lot of "C" style in your code : learn C++ from : [a recent C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) or from [learncpp.com](https://www.learncpp.com/) that site is decent and pretty up to date. Then use [cppreference](https://en.cppreference.com/w/) for reference material (and examples). When you have done all that keep using [C++ core guidelines](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines) to keep up to date with the latest best practices since C++ keeps evolving. – Pepijn Kramer May 03 '23 at 03:35
  • Also have a look at : std::count_if, std::min_element, std::max_element. The header. static constexpr std::size_t min_file_size = 4000ul; for defining constants etc.. etc.. – Pepijn Kramer May 03 '23 at 03:39
  • I would love to use the new C++ functions, but my professor is very strict about the usage of certain functions, and I must follow the textbook for full points – Mason Thomas May 03 '23 at 03:53
  • @MasonThomas To be fair to your professor I've seen worse. – john May 03 '23 at 05:50
  • @MasonThomas You seem to think that variables in functions must have the same name as the corresponding variables in the calling function. You realize that's not true? – john May 03 '23 at 05:51
  • Those are pretty strange functions. Both `output` and `input` ignore the `SIZE` parameter, assuming that the array is at least 5000 elements long. `output` is a function that creates a file of random numbers *and* stores the same numbers to an array. The array is passed to `input`, which asks for a file name, opens a file, and then ignores the file contents entirely while outputting the array contents and doing some calculations. – molbdnilo May 03 '23 at 07:26

0 Answers0