I wrote a program executed it and it returned the expected output i goto modify it to work with multiple input files and now it returns garbage and crashes anyone have any idea?

-the output file is commented because i am not using it yet there are no references to it in the code that could cause issue

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

using namespace std;

void calculationOutput(const char* inputFile);
ifstream inFile;
ofstream outFile;
char inputFile[11];


	

int main()
{
	calculationOutput("data01.dat");
	calculationOutput("data02.dat");

	return 0;

}
void calculationOutput(const char* inputFile)//, const string* outputfile)

Recommended Answers

All 4 Replies

I wrote a program executed it and it returned the expected output i goto modify it to work with multiple input files and now it returns garbage and crashes anyone have any idea?

-the output file is commented because i am not using it yet there are no references to it in the code that could cause issue

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

using namespace std;

void calculationOutput(const char* inputFile);
ifstream inFile;
ofstream outFile;
char inputFile[11];


	

int main()
{
	calculationOutput("data01.dat");
	calculationOutput("data02.dat");

	return 0;

}
void calculationOutput(const char* inputFile)//, const string* outputfile)

We need your calculationOutput code and maybe a sample of the input and resultant garbage.

here is the input file
data01
20 24 32 35 40 50 50 45 40 35 30 25
30 38 40 45 50 60 60 55 45 40 35 32
60 60 55 50 45 40 40 40 45 50 55 60
45 45 40 30 25 20 15 20 25 30 40 45

data02
00 24 32 35 40 50 50 93 40 35 30 25
30 38 65 45 50 60 60 55 45 40 35 32
60 60 55 50 45 40 40 40 45 50 55 60
45 45 40 86 25 20 15 20 25 30 40 45
34 24 32 35 40 40 40 45 40 35 30 25
30 38 74 45 88 50 50 39 45 40 35 32
60 55 76 50 34 40 40 40 45 50 55 55
45 45 40 30 20 20 10 67 25 30 40 45
88 24 76 35 40 50 50 45 40 35 30 20
30 38 40 45 50 65 65 33 45 40 35 32
60 60 55 99 45 40 40 40 45 50 55 55
45 45 99 30 25 20 15 25 25 30 40 50


here is the code

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

using namespace std;

void calculationOutput(const char* inputFile);
ifstream inFile;
ofstream outFile;
char inputFile[10];


	

int main()
{
	calculationOutput("data01.dat");
	calculationOutput("data02.dat");

	return 0;

}
void calculationOutput(const char* inputFile)//, const string* outputfile)
{
	//opens and test for exsistence of data files
	inFile.open(inputFile);
	if(! inFile)
	{
		cout << "No such input/output file exsists";
	}

	const int records = 10;
	const int month = 12;
	int monthArray[records][month];
	int averagedArea[month];
	int sum[month];
	int summedMonths[month];
	int averaged[month];
	string monthN;
	int highestMonthTemp=0;
	int highestAreaTemp=0;
	int highestMonth=0;
	int highestArea=0;
	int extra=0;
	int numRecords=0;


//*******************************not altered below**********************************************
	//pulls in data and counts number of records
	for(int i=0; i<records && inFile; i++)
	{
		for(int j=0; j<month; j++)
		{
			int hold=0;
			inFile >> hold;

			if(!inFile) break;
			monthArray[i][j] = hold;
		}
		if(inFile)
		{
			numRecords++;
		}
	}

	//Averages the areas
	for(int i=0; i<numRecords; i++)
	{
		for(int j=0; j<month; j++)
		{
			sum[i] = monthArray[i][j]+sum[i];
		}
		averagedArea[i] = sum[i]/month;
	}

	//compares to find the highest area and the temperature
	for(int i=0; i<numRecords; i++)
	{
		
		if(averagedArea[i]>averagedArea[highestArea])
		{
			highestArea=i;
			highestAreaTemp=averagedArea[i];
		}
	}

	//calculates the totals for the months and stores them
	for(int i=0; i<month; i++)
	{
		for(int j=0; j<numRecords; j++)
		{
			summedMonths[i]=summedMonths[i]+monthArray[i][j];
		}
	}

	//calculates the averages and stores that
	for(int i=0; i<month; i++)
	{
		averaged[i]=summedMonths[i]/numRecords;
	}
	
	//finds the month with the highest temperature and stores the temp
	for(int i=0; i<month; i++)
	{
		if(averaged[i]>averaged[highestMonth])
		{
			highestMonth=i;
			highestMonthTemp=averaged[i];
		}
	}

	if(highestMonth == 0)
		monthN = "January";
	if(highestMonth == 1)
		monthN = "February";
	if(highestMonth == 2)
		monthN = "March";
	if(highestMonth == 3)
		monthN = "April";
	if(highestMonth == 4)
		monthN = "May";
	if(highestMonth == 5)
		monthN = "June";
	if(highestMonth == 6)
		monthN = "July";
	if(highestMonth == 7)
		monthN = "August";
	if(highestMonth == 8)
		monthN = "September";
	if(highestMonth == 9)
		monthN = "October";
	if(highestMonth == 10)
		monthN = "November";
	if(highestMonth == 11)
		monthN = "Decemeber";

	//output
	for(int i=0; i<numRecords; i++)
	{
		cout << i << " " << sum[i] << endl;
	}
	cout << "The area with the highest average temperature is #" << highestArea << " with a temperature of " << highestAreaTemp <<endl;
	cout << monthN << " had the highest average temperature of " << highestMonthTemp <<endl;
	cout << "The number of data records that were read in and not used were: " << extra << endl << endl;
}

sample output
lines 138-142 was me testing a piece forgot to remove though shouldnt affect output

solved garbage output problem.

but refuses second file with a file does not exsist error?


modified block

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

using namespace std;

void calculationOutput(const char* inputFile);
ifstream inFile;
ofstream outFile;
const int records = 10;
const int month = 12;
int monthArray[records][month];
int averagedArea[month];
int sum[month];
int summedMonths[month];
int averaged[month];
string monthN;
	

int main()
{
	calculationOutput("data01.dat");
	calculationOutput("data02.dat");

	return 0;

}
void calculationOutput(const char* inputFile)//, const string* outputfile)
{
	//opens and test for exsistence of data files
	inFile.open(inputFile);
	if(! inFile)
	{
		cout << "No such input/output file exsists";
	}

	int highestMonthTemp=0;
	int highestAreaTemp=0;
	int highestMonth=0;
	int highestArea=0;
	int extra=0;
	int numRecords=0;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.