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

the garbage you are getting is because you declared the array to have 20 elements but you are only filling 6 of them. the rest are uninitialized. you could use a vector in this case and that will fix having to know the size because it can re size itself to hold just the data you need. if that's not an option then you could write a second loop in your input function that will start of where you ended filling the array from the file and make the value 0. then in your output function you could put an if statement in your loop to test if the data in index i is equal to 0. if it is then skip it.

Edited 6 Years Ago by NathanOliver: n/a

thanks oliver....i figures that out shortly after posting. The problem I am running into now is comparing 2 arrays to get a desired output. the first file looks like
25 277.00
35 300.00
first column is the age, second is the premium

secon file contains
1234578 43 smith
id, age and name

I have to compare the age in the second file to the ages in the first file and return the corrseponding premium.....if age (of second) <= age in first then return corresponding premium....I cant figure this part out....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>
#include <string>

using namespace std;

const int Max_Array = 6;

int LoadArrays (ifstream& inputdata, int age[], double premium[]);
int LoadArrays (ifstream& inputdata1, long policy[], int actualage[], string name[]); 
void printArray (string name[], int actualage[], double premium[], ostream& outputfile);
void getpremium (int age[], int actualage[], int premium[], ofstream& outputfile);

int main()
{
	int age[Max_Array];
	double premium[Max_Array];
	long policy[Max_Array];
	int actualage[Max_Array];
	string name [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);
	inputdata.close();
	
	ifstream inputdata1 ("PJ704_policy.txt");
	
	if (!inputdata1)
	{
		cerr <<"Could not open the input data file.\n";
		return -1;
	}
	LoadArrays (inputdata1, policy, actualage, name);

	outputfile <<setw(10)<<"Name"<<setw(20)<<"Age"<<setw(14)<<"Premium"<<endl<<endl;
	
	printArray(name, actualage, 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 (string name[], int actualage[], double premium[], ostream& outputfile)
{
	for (int i = 0; i < Max_Array; i++)
	{
		outputfile <<setw(20)<< name[i] <<setw(10)<<actualage[i] <<setw(15)<<getpremium<<endl;
	}
}
int LoadArrays (ifstream& inputdata1, long policy[], int actualage[], string name[])
{
	int index = 0;

	while ( index < Max_Array && inputdata1 >> policy[index] >> actualage[index] >> name[index])
	{
		index ++;
	}
	if ( index == Max_Array && inputdata1 >> ws && inputdata1.good())
	{
		cerr <<"Too many data in input file.\n";
		exit (1);
	}
	else if (!inputdata1.eof() && inputdata1.fail())
	{
		cerr <<"Bad data in input file.\n";
		exit (2);
	}

	return index;
}
void getpremium (int age[], int actualage[], int premium[], ofstream& outputfile)
{
	for (int i = 0; i < Max_Array; i++)
	{
		if (actualage[i] <= age[i])
			outputfile << premium[i];

	}
}
int LoadArrays (ifstream& inputdata, int age[], double premium[])
{
	int index = 0;

	while ( index < Max_Array && inputdata >> age[index] >> premium[index])
	{
		index ++;
	}

In my opinion, this loop is more confusing than it needs to be. If you want to use Max_Array like you are, then a for loop is what you want.

This article has been dead for over six months. Start a new discussion instead.