Help passing string GetFileName(string prompt)

I am needing help being able to pass the fileName variable value from function to function. From Main(); I can call function GetFileName(string) and have a fileName returned without issue. Next function is fileOpen(); which is supposed to call the open function and open file to read. The issue I am having is that in the Header.cpp implementation file, there is a error in the line fileIn.open(fileName);. The fileName is red underlined and when I hover over it, the message says "identifier 'fileName' is undefined"

Can someone help me to solve my dilemma PLEASE??

Main(); is listed first,
Then Header.h file,
Then Header.cpp file.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include <fstream>
#include <iomanip>
#include <iostream>
#include <string>

#include "Header.h";


using namespace std;
using namespace flesch;


// Define named constants below

int main()
{
	string fileName = GetFileName("Please enter a file name (without the file type) to analyze: ");
	cout << fileName << endl;
	
	// THIS IS A TEST TO OPEN FILE VIA FUNCTION
	fileOpen();

	return 0;
}


HEADER.H FILE

#ifndef FLESCH_READABILITY_H_
#define FLESCH_READABILITY_H_

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

using namespace std;

namespace flesch
{
	/**
	This function will ask user for a file name
	to open for analysis

	@param	fileName This is the file name to open

	@return	Return the file name
	*/
	string GetFileName(string prompt);



	/**
	This function will open the file user indicated
	for analysis

	@param fileOpen	This is the file that is being opened

	@return void() function, no return
	*/
// THIS IS A TEST TO OPEN FILE VIA FUNCTION
	void fileOpen();

}




HEADER.CPP FILE

#include <iostream>

#include "Header.h"

using namespace std;

namespace flesch
{
	
	/**
	This function will ask user for a file name
	to open for analysis

	@param	fileName This is the file name to open

	@return	Return the file name
	*/
	string GetFileName(string prompt)
	{
		string fileName = "";
		// Prompt user for file name
		cout << prompt;
		cin >> fileName;

		// Return file name
		return fileName;
	}



	// THIS IS A TEST TO OPEN FILE VIA FUNCTION
	void fileOpen()
	{
		// Set (fileName) opening directive
		ifstream fileIn;

		// Set (fileName) closing directive
		ofstream fileOut;

		// Open (fileName) to read
		fileIn.open(fileName);
	}

}


How is your function fileOpen supposed to know the name of the file to open?
^ main() has a local fileName, GetFileName() function also has its own local fileName. fileOpen() does not know about these. One way to solve this is to give fileOpen() function a parameter; another way is to encapsulate all your functions under a class, and, upon construction of this class, remember the file name in a private variable.

I also wanted to point out that your comment on line 107 isn't quite right, and I'd remove both lines 107 and 108. An ofstream object is for writing to an output file. This fileOpen() function, as per its name, should only have the singular purpose of opening a file. Perhaps even rename the function to ParseFile() or so, if your ultimate goal is to parse something interesting from that file. Other operations should be in their own separate areas -- "separation of concerns". Good luck.

Topic archived. No new replies allowed.