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.
#include <fstream>
#include <iomanip>
#include <iostream>
#include <string>
#include "Header.h";
usingnamespace std;
usingnamespace 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>
usingnamespace 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"
usingnamespace 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);
}
}
^ 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.