Hi ! Sorry it didn't go well...but happy that you're making an effort to understand (this is the real point of an assignment). Maybe post the next one up when you hit a wall and we can help you out XD
Since there's no description on what the prof wanted I'm going to guess that you were supposed to open a file for reading and then read the contents. You're almost there. You have the correct ifstream which is needed for opening a file but didn't include the header file where its definition is kept.
#include <fstream>
Line 14 calls a function to open the file for reading and u pass in 'fileN' for the file name and 'inf' which is meant to be the ifstream object...the problem is that 'inf' was never declared. Remember that anything u pass into a function has to be declared. How else does the compiler know what 'inf' is ? is it an integer? a float?
Your function is declared and defined correctly with references for the objects (since objects can take up a lot of memory)
void openFile(ifstream& inf, string& fileN) {
So, after u fix that fstream include the next thing is opening the file. Line 28 is good for user input. I'll just note that the file name has to be exact including the extension for it to work. ex:
myfile.txt
Last but not least is reading the contents of the file. There are many ways to do this so here is a simple way:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
if (inf.is_open())
{
string strFileData;
cout << "File is open." << endl;
int i = 0; //to keep track of the # of lines read
while(inf) //keep looping as long as we are in the file
{
inf.getline(strFileData, 80, '\n'); //read up to 80 characters OR up to a newline
// and store those characters into strFileData
//print the content of strFileData and line position for each loop
cout << "Line " << i << " is: " << strFileData << endl;
}
|
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
|
#include <iostream>
using namespace std;
//declare functions
void openFile(ifstream& inf, string& fileN);
int main() {
string sentence, fileN;
//open the file
openFile(inf, fileN);
inf.close();
return 0;
}
//*************************openFile*******************************
// Name: openFile
// Description: opens file
// Parameters: ifstream& inf, string& fileN
// Return: none
//***************************************************************
void openFile(ifstream& inf, string& fileN) {
cout << "Please enter file name >>" << endl;
cin >> fileN;
inf.open(fileN.c_str());
if (inf.is_open())
cout << "File is open." << endl;
else {
cerr << "File not found - terminating." << endl;
// exit(0);
}
}
|
One last thing to think about, 'inf' is only used in the function 'openFile' therefore if doesn't make sense to declare it in main(). Same goes with fileN. Though, maybe you were supposed to have 2 functions - 1 to open the file and 1 to read/print the contents. In this case these declarations in main() make sense. It's important to keep variables as local as possible so that they don't obfuscate the code.
ps: u forgot the [/code] at the end