Please help me. I don't know what to do. I messed up my code but I know it's only in "Function: populateArray". What I need to do is I need to pass in 2 reference arguments, the ifstream (inFile), & a 1000 element array of words. When the function completes, the array will hold the words in the file. I put comments beside the places where I believe I made errors. I don't even know if my code is right. I am having the hardest time trying to figure this out. I also have no idea how to pass arrays through reference. Basically "Function populateArray" is supposed to take all of the words from the text file & save them in the array.
#include<iostream>
#include<fstream>
using namespace std;
return 0;
}
// Function: readFilename
void readFilename(ifstream &inFile, string &filename)
{
// Reads in file name.
cout << "Please enter the file name you wish to open." << endl;
getline(cin, filename);
inFile.open(filename.c_str());
// Displays error message if file is not found.
if (!inFile)
{
cout << "This file did not open properly and the program will now terminate.\nPlease make sure the spelling of the file is correct." << endl;
}
}
// Function: countNumberLines
void countNumberLines(ifstream &inFile, int &countLines)
{
// Variables
string line;
countLines = 0;
#include<iostream>
#include<fstream>
usingnamespace std;
// Function Prototypes
void readFilename(ifstream&, string&);
void countNumberLines(ifstream&, int&);
void countNumberChars(ifstream&, int&);
void populateArray(ifstream&, string&); // Error?
int main()
{
// Variables
ifstream inFile;
string filename;
int countLines;
int countChars;
int index;
string array[index];
// Function Calls
readFilename(inFile, filename);
countNumberLines(inFile, countLines);
countNumberChars(inFile, countChars);
populateArray(inFile, array[index]); // Error?
return 0;
}
// Function: readFilename
void readFilename(ifstream &inFile, string &filename)
{
// Reads in file name.
cout << "Please enter the file name you wish to open." << endl;
getline(cin, filename);
inFile.open(filename.c_str());
// Displays error message if file is not found.
if (!inFile)
{
cout << "This file did not open properly and the program will now terminate.
\nPlease make sure the spelling of the file is correct." << endl;
}
}
// Function: countNumberLines
void countNumberLines(ifstream &inFile, int &countLines)
{
// Variables
string line;
countLines = 0;
while(!inFile.eof())
{
getline(inFile,line);
countLines++;
}
inFile.close();
}
// Function: countNumberChars
void countNumberChars(ifstream &inFile, int &countChars)
{
// Variables
string character;
countChars = 0;
while(!inFile.eof())
{
getline(inFile,character);
countChars++;
}
inFile.close();
}
// Function: populateArray
void populateArray(ifstream &inFile, string &array[index]) //Error?
{
int index = 0; //Error?
while(getline(inFile,array[index])) //Error?
{
index++; //Error?
}
inFile.close();
}
Lines 9 and 72 don't match up.
I think u shouldn't use a name like 'array' because it's causing confusion. It looks like u want to create an array of strings, pass that array (of strings) into the function and then load each element (of an array of strings) with some characters.
Line 9 is only expecting to receive 1 string object. To pass an array of strings declare line 9 like u did on line 19 but for string array[] instead of the 'index' variable. That way the function knows to expect an array of strings.
Same thing goes for line 72.
Then with ur function call on line 25, when u pass strMyArray[index], u are not passing the entire array but rather only the element of the array at the element 'index'. U only need to pass in strMyArray (or 'array' as u called it)
I'd also say that u probably want to merge the countlines/countchars and populatearray into 1 function. It is computationally expensive to have to keep opening and closing the same file when 1 getline can do all 3 things u want to do there.