on line 32 how would i go about inputting a file name to be called by my function which im using a const char pointer for the file name

line 33) error C2664: 'calculationOutput' : cannot convert parameter 1 from 'char' to 'const char *'

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

using namespace std;

void calculationOutput(const char* inputFile);//,const char* outputFile);
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;
char inputFile;	

int main()
{
	for(int count=0; count!=2; count++)
	{
		cout << "enter the input files name: ";
		cin >> inputFile;
		calculationOutput(inputFile);
	}
		return 0;

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

	//declaration of variables sensitive to calculations
	int highestMonthTemp=0;
	int highestAreaTemp=0;
	int highestMonth=0;
	int highestArea=0;
	int extra=0;
	int numRecords=0;


	//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++;
		}
		if(i>records)
		{
			extra++;
		}
	}

	//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;
}

Recommended Answers

All 7 Replies

Take in the input as a std::string and use the .c_str() method of the string object to get the const char * representation.

Or just change InputFile's declaration to char InputFile[256] or some other size.Still using a string like jonsca said is a lot better.

char inputFile;

You have declared inputFile as a single character.
So unless an until you only want to open a file which is a single character long, You can leave it the way it is and go ahead on changing your function.

Or I guess you could change inputFile to

char *inputFile;
char inputFile;

Or I guess you could change inputFile to

char *inputFile;

That will most likely cause a crash when you try to write to an uninitialized pointer like that.

Yep, Thats a mistake.
I should have suggested an array.
MY BAD.

thank you all i ended up going with the char[256] answer though i agree it would've been a better option to go with the string i just forgot how to take in the input as a std::string

Yes, if you have spaces you'll need to use getline.

std::string input;
getline(cin,input); 
//or if your string is guaranteed not to have spaces, cin >> input; will work
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.