ok the HW assignment i was working on i got it back and it crashed on closure. guess why cause i had put the cin.get() at the end and always just exited by clicking x so it just never occured to me it wasnt working. so of course the teacher hit enter and it died trying to close.
so i have no idea why a program would execute just fine and THEN die trying to close. the little bits of commented out code here and there was me trying to fix the problem where it reads the first bit of data in the file then skips over the second one so thats why.
but mainly why does it die closing?
(yes i know this is my whole assignment but eh not sure what part is killing it)
Code:
/********************************************************************************
* FILENAME :school_1.cpp *
* PROGRAMMER :DELETED FOR SECURITY REASONS *
* PURPOSE :Determine whether file is readable and if so read it and *
* :perform certain calculations with numbers *
*******************************************************************************/
#include <fstream>
#include <string>
#include <iostream>
using namespace std;
//function prototypes
double calc_average(int count,int file_data[]);
int smallest_number(int file_data[],int count);
int load_array(ifstream& in, int file_data[], int arraysize);
int count_abv_avg(int file_data[],int count,double avg);
void display(double avg,int count_abv);
void print_array(int file_data[],int count);
int main()
{
//variable intialization
bool exit = false;
double avg;
int smallest;
int count_abv = 0;
int count = 0;
const int arraysize = 99;
int file_data[arraysize];
ifstream in("onehundred.dat"); // OPEN FILE HERE
//main program control loop
while(exit == false)
{
if(in.is_open())
{
count = load_array(in, file_data, arraysize);
if(count == -1)
exit = true;
else
{
print_array(file_data,count);
avg = calc_average(count,file_data);
count_abv = count_abv_avg(file_data,count,avg);
smallest = smallest_number(file_data,count);
display(avg, count_abv);
exit = true;
}
}
else
{
cout << "Your file does not exist please exit and try again.\n";
exit = true;
}
}
//cout << "\nPRESS ANY KEY TO CONTINUE\n";
//cin.get();
in.close();
return 0;
}//it says it DIES HERE
/********************************************************************************
* PURPOSE :Display all neccessary outputs *
* PASSED IN :avg, count_abv *
* RETURNED :Nothing *
*******************************************************************************/
void display(double avg,int count_abv)
{
cout << "\nThe average of the array:\t" << avg
<< "\nThe number of numbers above the average:\t" << count_abv << endl;
}
/********************************************************************************
* PURPOSE :Display all data found in file *
* PASSED IN :file_data[], count *
* RETURNED :Nothing *
*******************************************************************************/
void print_array(int file_data[],int count)
{
cout << "The data in this File\n" << file_data[0] << "VALUE OF COUNT" << count<<endl;
for(int i = 0;i < count;i++)
cout << file_data[i] << "Value if I:" << i << endl;
}
/********************************************************************************
* PURPOSE :Calculate average of data found in file *
* PASSED IN :count,file_data[] *
* RETURNED :average *
*******************************************************************************/
double calc_average(int count,int file_data[])
{
int total = 0;
double average = 0;
int i;
for(i = 0;i < count;i++)
total += file_data[i];
average = total/i;
return average;
}
/********************************************************************************
* PURPOSE :Reads the file data into array and determines *
* :the number of elements in array *
* PASSED IN :&in,file_data[],arraysize *
* RETURNED :i *
*******************************************************************************/
int load_array(ifstream& in, int file_data[], int arraysize)
{
int elementsize = 0;
//in >> file_data[0];
//cout << file_data[0];
if(!(in >> file_data[elementsize]))
{
cout << "File is Empty\n";
elementsize = -1;
}
elementsize++;
//cout << "0 ELEMENT" << file_data[elementsize] << endl;
// while(in >> file_data[elementsize++] && elementsize <= arraysize)
// ;
for(;in>>file_data[elementsize] && elementsize < arraysize;elementsize++);
if(elementsize > arraysize+1)
{
elementsize = -1;
cout << "There are too many elements for this array\n";
}
return elementsize;
}
/********************************************************************************
* PURPOSE :Determines how many numbers in file are above average *
* PASSED IN :file_data[],count,avg *
* RETURNED :count_abv *
*******************************************************************************/
int count_abv_avg(int file_data[],int count,double avg)
{
int count_abv = 0;
for(int i = 0;i < count;i++)
if(file_data[i] > avg)
count_abv++;
return count_abv;
}
/********************************************************************************
* PURPOSE :determines the smallest number in the file *
* PASSED IN :file_data, count *
* RETURNED :smallest *
*******************************************************************************/
int smallest_number(int file_data[],int count)
{
int smallest = file_data[0];
for(int i = 0;i < count;i++)
if(smallest > file_data[i])
smallest = file_data[i];
cout << "\nThis is the smallest number in the file:\t" << smallest << endl;
return smallest;
}