I have been tasked with writing a program that will read in from 2 files and determine a premium rate depending on age. I have written part of the program and I am simply trying to make sure that what I am reading in is correct. The input file has 2 sets of data, column 1 is an age and column 2 is a premium rate. Problem is when I run the program, I am getting some garbage at the end of the file. Can anyone determine why I am getting this garbage? code is as follows
//ACME Life Insurance premium program
//will input data from 2 external files
//and determine the premium amount depending on age.
#include <iostream>
#include <iomanip>
#include <cmath>
#include <fstream>
using namespace std;
const int Max_Array = 20;
int LoadArrays (ifstream& inputdata, int age[], double premium[]);
void printArray (int age[], double premium[], ostream& outputfile);
int main()
{
int age[Max_Array];
double premium[Max_Array];
ifstream inputdata ("PJ704_rates.txt");
if (!inputdata)
{
cerr <<"Could not open the input data file.\n";
return -1;
}
ofstream outputfile ("PJ704_output_Nealey.txt");
if (!outputfile)
{
cerr <<"Could not open the output file.\n";
return -2;
}
outputfile <<fixed<<showpoint<<setprecision(2);
LoadArrays (inputdata, age, premium);
printArray(age, premium, outputfile);
return 0;
}
int LoadArrays (ifstream& inputdata, int age[], double premium[])
{
int index = 0;
while ( index < Max_Array && inputdata >> age[index] >> premium[index])
{
index ++;
}
if ( index == Max_Array && inputdata >> ws && inputdata.good())
{
cerr <<"Too many data in input file.\n";
exit (1);
}
else if (!inputdata.eof() && inputdata.fail())
{
cerr <<"Bad data in input file.\n";
exit (2);
}
return index;
}
void printArray (int age[], double premium[], ostream& outputfile)
{
for (int i = 0; i < Max_Array; i++)
{
outputfile <<setw(10) << age[i] <<setw(15)<<premium[i]<<endl;
}
}
What I am getting in the outputfile is
25 277.00
35 287.50
45 307.75
55 327.25
65 357.00
70 455.00
-858993460-92559631349317830000000000000000000000000000000000000000000000.00
-858993460-92559631349317830000000000000000000000000000000000000000000000.00
-858993460-92559631349317830000000000000000000000000000000000000000000000.00
-858993460-92559631349317830000000000000000000000000000000000000000000000.00
-858993460-92559631349317830000000000000000000000000000000000000000000000.00
-858993460-92559631349317830000000000000000000000000000000000000000000000.00
-858993460-92559631349317830000000000000000000000000000000000000000000000.00
-858993460-92559631349317830000000000000000000000000000000000000000000000.00
-858993460-92559631349317830000000000000000000000000000000000000000000000.00
-858993460-92559631349317830000000000000000000000000000000000000000000000.00
-858993460-92559631349317830000000000000000000000000000000000000000000000.00
-858993460-92559631349317830000000000000000000000000000000000000000000000.00
-858993460-92559631349317830000000000000000000000000000000000000000000000.00
-858993460-92559631349317830000000000000000000000000000000000000000000000.00