Cannot get this code to work for the life of me, (Core Dumped) in unix server, or "process returned -1073741819 (0xC0000005)" using code blocks

0

I was doing this project for my class, and I cannot fix this error for the life of me, any help would be greatly appreciated, I apologize for the sloppy code, and also for how this will be formatted as I'm new to posting. Everyone seems to be getting this error with pointers but I am not using pointers in this program

#include<iostream>
#include<iomanip>
#include<cstdio>
#include<string>
#include<fstream>
#include<cstdlib>


using namespace std;

#define in_file input_file_name;
#define out_file "animalsout.dat"

    ifstream animalfile;
    ofstream outputfile;


void instructions();
int openFiles();
int leastAnim(int[]);
int mostAnim(int[]);
double avg(int[]);
void closeFiles();
void outputArrays(string[], int[]);
void finalOutput(string[], int, int, double);
int main(){
    string animalNames[100];
    int animalTotals[100];
    int i=0;
    string name;
    int zoonum, posofleast, posofmost;
    double avganim;

    instructions();

    openFiles();

while(!animalfile.eof()){
    animalfile>>animalNames[i];

    for(int x=0; x<=9; x++){
    animalfile>>zoonum;
    animalTotals[i]=zoonum+animalTotals[i];


    }
    i++;

}


    closeFiles();
    posofleast=leastAnim(animalTotals);
    posofmost=mostAnim(animalTotals);
    avganim=avg(animalTotals);
    outputArrays(animalNames, animalTotals);
    finalOutput(animalNames, posofleast, posofmost, avganim);


    return 0;

}

void instructions(){
    cout<<"This program will take the names of animals and add together how many of each animal there are at 10 different zoos, then sort the numbers and names into two arrays which are parallel\n";

}

int openFiles(){
    string file_name="animals.dat";


    animalfile.open(file_name.c_str( ));

    if(animalfile.fail()){
        cerr<<"*** ERROR: Cannot open "<<file_name<<" for input"<<endl;
        exit(1);
        return EXIT_FAILURE;
    }

    outputfile.open(out_file);

    if(outputfile.fail()){
        cerr<<"*** ERROR: Cannot open "<<out_file<<" for output."<<endl;
        exit(1);
        return EXIT_FAILURE;
    }

}

int leastAnim(int animalTotals[100]){
    int posofleast=0;

    for(int i=0; i<sizeof(animalTotals); i++){
        if(animalTotals[i]<animalTotals[posofleast]){
            posofleast=i;
        }
        else{

        }
    }

    return posofleast;
}

int mostAnim(int animalTotals[100]){
    int posofmost=0;
    int arraylength;

    for(int i=0; i<sizeof(animalTotals); i++){
        if(animalTotals[i]>animalTotals[posofmost]){
            posofmost=i;
        }
        else{

        }
    }

    return posofmost;
}

double avg(int animalTotals[100]){
    double avg,
    total;
    for(int i=0; i<sizeof(animalTotals); i++){
        total+=animalTotals[i];
    }
    avg=total/sizeof(animalTotals);
    return avg;
}

void closeFiles(){
    animalfile.close();
    outputfile.close();
}

void outputArrays(string animalNames[100], int animalTotals[100]){
    cout<<"Names\t\t\t\t"<<"Total\n";
    for(int i=0; i<sizeof(animalTotals); i++)
        cout<<animalNames[i]<<"\t\t\t\t"<<animalTotals[i]<<endl;
}

void finalOutput(string animalNames[100], int posofleast, int posofmost, double avganim){
    cout<<"Animal with the least amount: \t\t"<<animalNames[posofleast]<<endl;
    cout<<"Animal with the most amount: \t\t"<<animalNames[posofmost]<<endl;
    cout<<"Average animals at all zoos: \t\t"<<avganim<<endl;
}

Here is the input file contents aswell:

Grants zebra
2 3 0 2 0 2 4 0 5 2
African penguin
12 7 0 0 0 6 14 0 0 0
Aardvark
1 2 0 0 1 1 2 0 0 2
Wyoming Toad
2 0 0 0 0 0 1 0 0 0
Red kangaroo
2 3 0 4 0 2 4 0 2 2
Saddle billed stork
2 1 2 2 1 0 0 2 1 2
Massi giraffe
2 3 1 2 2 2 1 2 3 2
African spurred tortoise
0 1 2 1 2 0 0 1 0 2
Gila monster
2 0 1 0 1 0 2 1 0 1
Tropical forest snake
0 0 1 2 1 0 0 0 1 0
Western lowland gorilla
20 2 3 0 2 1 0 3 2 0
Dart frog
2 1 3 0 2 2 3 4 0 2
Black-capped lorikeet
0 0 4 2 2 3 0 2 4 0
Tammar wallaby
0 2 0 0 2 3 1 1 0 0
Northern bald eagle
2 1 1 0 0 2 1 0 0 1
Loggerhead sea turtle
0 1 0 0 0 2 1 0 0 0
Emu
2 2 0 0 2 1 2 1 0 2
White rhino
0 3 2 0 0 0 0 0 2 0
Scimitar horned_oryx
0 0 5 0 3 2 7 3 0 0
Chinese alligator
0 0 1 0 0 0 0 1 0 0
Blue throated macaw
0 0 2 1 0 0 0 2 0 1

EDIT: I've figured it's probably an issue with the loop, particularly storing the totals, with the above setup I receive the error described in the title, but with this configuration, I get no error at all, the program just hangs.

  while(!animalfile.eof()){
    animalfile>>animalNames[i];

    for(int x=0; x<=9; x++){
    animalfile>>zoonum;
   if(i>0){
        animalTotals[i]=zoonum+animalTotals[i-1];
        }
        else{
            animalTotals[i]=zoonum;
        }
    }
    i++;

}
project
coredump
asked on Stack Overflow Dec 15, 2019 by Derpyykiin • edited Dec 15, 2019 by Derpyykiin

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0