Could Anyone tell me why the changes made to the below variables are not being pulled through to main?
I'm pretty new to this so please keep it simple.
If you need more of my code let me know :D
void BannedWordsArrayCreate (string filePathInBanned, vector<string> bannedWords, vector<int> bannedWordsCount, vector<int> containsBannedWordsCount ) {
cout << "Please enter the file path for the banned word list. (no extension.): " << endl; //User enters file name
cout << "E.g. C:\\Users\\John\\banned" << endl;
cin >> filePathInBanned;
filePathInBanned += ".txt"; //Takes User defined file name and adds .txt
ifstream inFile;
inFile.open(filePathInBanned,ios::in); //opens file
if (!inFile) //if file cannot be opened: exits function.
{
cerr << "Can't open input file." << filePathInBanned << endl;
exit(1);
}
else if (inFile.is_open()) //if file opens: puts file into vector.
{
string bw = "nothing"; //temporary string used to signal end of file.
while(!inFile.eof() && bw != "")
{
inFile >> bw;
if (bw != "")
{
bannedWords.push_back(bw);
}
}
}
inFile.close();
cout << endl << "Done!" << endl << endl;
for(int i = 0; i < bannedWords.size(); i++)
{
bannedWordsCount.push_back(0);
containsBannedWordsCount.push_back(0);
}
}
This line...
void BannedWordsArrayCreate (string filePathInBanned,
vector<string> bannedWords, vector<int> bannedWordsCount,
vector<int> containsBannedWordsCount )
...needs to ask for variables by reference (with the &
token)...
void BannedWordsArrayCreate (string& filePathInBanned,
vector<string>& bannedWords, vector<int>& bannedWordsCount,
vector<int>& containsBannedWordsCount )
References are basically aliases or alternative names for the original variable (provided by the caller), so changes made "to the reference" are actually modifying the original variable.
In your original function, the function arguments are passed by value, which means the variables in the calling context are copied, and the function only gets to work on those copies - any modifications to the copies are lost when the function returns.
Separately, !inFile.eof()
is not used correctly. There are a lot of Stack Overflow Q/A about this issue, but summarily the eof()
flag can only be set by the stream after it knows what you're trying to convert (for example, if you try to read in a string and it can only find lots of whitespace, then it will fail and set eof
, but if you ask the stream for whatever the next character is (including whitespace) then it would return that character successfully without hitting/setting eof). You can simplify your input handling to:
if (!(std::cin >> filePathInBanned))
{
std::cerr << "you didn't provide a path, goodbye" << std::endl;
exit(1);
}
filePathInBanned += ".txt"; //Takes User defined file name and adds .txt
if (ifstream inFile(filePathInBanned))
{
string bw;
while (inFile >> bw)
bannedWords.push_back(bw);
// ifstream automatically closed at end of {} scope
}
else
{
std::cerr << "Can't open input file." << filePathInBanned << std::endl;
exit(1);
}