4 Replies - 1204 Views - Last Post: 30 January 2010 - 10:40 PM Rate Topic: -----

#1 Kain1115   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 30-January 10

Can't Close My Input and Output Files

Posted 30 January 2010 - 03:46 PM

My homework problem requires me to take input from a file, calculate some data, and then output it to another file. The part that I think I am struggling with is that it requires me to make a separate function to open those files.

a. Function openFiles: This function opens the input and output files, and sets the output of the floating-point numbers to two decimal places in a fixed decimal format with a decimal point and trailing zeros.

Here Is The Code For That Function
#include "ClassAvg.h"

void openFiles()
{
	ifstream inData;
	ofsteam outData;
	 
	 inData.open(Ch7_Ex4Data.txt);
	 outData.open(gpaCalcOutput.txt);
	 
	 outData << setprecision(2);
	 outData << fixed;
}



ClassAvg.h is a header file that has <iostream>, <iomanip>, <fstream>, <cstring>, and <cstdlib> inside of it.

When I try to compile and run my program I am getting an error that says : `inData' undeclared (first use this function)
in my main. Here is the code for my main:
 

#include "ClassAvg.h"


using namespace std;

int main(int argc, char *argv[])
{
	openFiles();
	
	int countFemale, countMale;
	double sumFemaleGPA, sumMaleGPA, femaleAverage, maleAverage;
	
	initialize(countFemale, countMale, sumFemaleGPA, sumMaleGPA);
	
	sumGrades (countFemale, countMale, sumFemaleGPA, sumMaleGPA);
	
	averageGrade (countFemale, countMale, sumFemaleGPA, sumMaleGPA, femaleAverage, maleAverage);
	
	printResults(femaleAverage, maleAverage);
	
	inData.close();
	outData.close();
	
	system("PAUSE");
	return EXIT_SUCCESS;
}



I am not sure if it matters but this is my first time using a header file and I may have created it wrong so here is the code for that as well:
#ifndef ClassAvg_h
#define ClassAvg_h

#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstring>
#include <cstdlib>

void openFiles ();
void initialize (int &, int &, double &, double &);
void sumGrades (int &, int &, double &, double &);
void averageGrade (int &, int &, double &, double &, double &, double &);
void printResults (double, double);

#endif



If anyone can help me figure out why it will not let me close my files and what I've done wrong it would be greatly appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Can't Close My Input and Output Files

#2 Martyr2   User is online

  • Programming Theoretician
  • member icon

Reputation: 5307
  • View blog
  • Posts: 14,165
  • Joined: 18-April 07

Re: Can't Close My Input and Output Files

Posted 30 January 2010 - 04:34 PM

You can't define inData and outData IN the function because once the function ends, those variables will go out of scope and are no longer available to your main function. What you need to do is define inData and outData in main, pass them to your open files function where you open them and set the precision and return. This keeps the variable's in the scope of main and thus can be closed and used in main.

:)

This post has been edited by Martyr2: 30 January 2010 - 04:35 PM

Was This Post Helpful? 1
  • +
  • -

#3 Kain1115   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 30-January 10

Re: Can't Close My Input and Output Files

Posted 30 January 2010 - 04:50 PM

View PostMartyr2, on 30 Jan, 2010 - 03:34 PM, said:

You can't define inData and outData IN the function because once the function ends, those variables will go out of scope and are no longer available to your main function. What you need to do is define inData and outData in main, pass them to your open files function where you open them and set the precision and return. This keeps the variable's in the scope of main and thus can be closed and used in main.

:)


Now I'm getting this error: In file included from main.cpp
I changed my code to this
 
int main(int argc, char *argv[])
{
	ifstream inData;
	ofsteam outData;
	
	openFiles(inData, outData);
	
  



and i changed my head file like this

void openFiles (ifstream &, ofsteam &);



and the change to openFiles
#include "ClassAvg.h"

void openFiles(ifstream inData, ofstream outData)
{
	
	 
	 inData.open(Ch7_Ex4Data.txt);
	 outData.open(gpaCalcOutput.txt);
	 
	 outData << setprecision(2);
	 outData << fixed;
}



Did I mess up my header file? The original is in my first post and the only change I made i just posted here =(

This post has been edited by Kain1115: 30 January 2010 - 04:51 PM

Was This Post Helpful? 0
  • +
  • -

#4 jjl   User is offline

  • Engineer
  • member icon

Reputation: 1270
  • View blog
  • Posts: 4,998
  • Joined: 09-June 09

Re: Can't Close My Input and Output Files

Posted 30 January 2010 - 06:24 PM

when you define indata and outdata in your funciton, yeah your create and open it but when the function ends those variables are destroyed.

heres an example (pass the ifle by reference into the function
#include <iostream>
#include <fstream>

using namespace std;

enum
{
	NOT_OPEN,
	OPEN
};

template<class file_type>
bool open_file(file_type &file_handle, const char *filename)
{
	file_handle.open(filename);

	if(!file_handle.is_open())
		return NOT_OPEN;	
	else
		return OPEN;
}

int main()
{
	ifstream in;

	bool status = open_file<ifstream>(in,"file.txt");

	if(status)
	{
		cout<<"OPEN"<<endl;
		in.close();
	}
	else
		cout<<"CLOSED"<<endl;

	cin.ignore();
	cin.get();

	return 0;

}


This post has been edited by ImaSexy: 30 January 2010 - 06:26 PM

Was This Post Helpful? 0
  • +
  • -

#5 Martyr2   User is online

  • Programming Theoretician
  • member icon

Reputation: 5307
  • View blog
  • Posts: 14,165
  • Joined: 18-April 07

Re: Can't Close My Input and Output Files

Posted 30 January 2010 - 10:40 PM

View PostKain1115, on 30 Jan, 2010 - 03:50 PM, said:

View PostMartyr2, on 30 Jan, 2010 - 03:34 PM, said:

You can't define inData and outData IN the function because once the function ends, those variables will go out of scope and are no longer available to your main function. What you need to do is define inData and outData in main, pass them to your open files function where you open them and set the precision and return. This keeps the variable's in the scope of main and thus can be closed and used in main.

:)


Now I'm getting this error: In file included from main.cpp
I changed my code to this
 
int main(int argc, char *argv[])
{
	ifstream inData;
	ofsteam outData;
	
	openFiles(inData, outData);
	
  



and i changed my head file like this

void openFiles (ifstream &, ofsteam &);



and the change to openFiles
#include "ClassAvg.h"

void openFiles(ifstream inData, ofstream outData)
{
	
	 
	 inData.open(Ch7_Ex4Data.txt);
	 outData.open(gpaCalcOutput.txt);
	 
	 outData << setprecision(2);
	 outData << fixed;
}



Did I mess up my header file? The original is in my first post and the only change I made i just posted here =(



Remember, your function definition has to match your declaration and you are missing the ampersands there in the actual function signature. You included them in the .h, but you also have to include them in them where you actually define the function.

;)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1