The maximum length of a word is 20, so the character array needs to be 21. The maximum numbers of words is 100, if there are more than 100 words I need to return a message saying something like "The stats did not use the words after the 100th word"
Anyways, I'm having a hard time even starting the program. I'm starting off by trying to read and store the words of the text file. My text book has code for reading line by line a text file and then displaying it. So I figured I should start with that. Heres the code-
#include <iostream>
#include <fstream>
#include <cstdlib> // needed for exit()
#include <string>
using namespace std;
int main()
{
string filename = "text.dat"; // put the filename up front
string line;
ifstream inFile;
inFile.open(filename.c_str());
if (inFile.fail()) // check for successful open
{
cout << "\nThe file was not successfully opened"
<< "\n Please check that the file currently exists."
<< endl;
exit(1);
}
// read and display the file's contents
while (getline(inFile,line))
cout << line << endl;
inFile.close();
cin.ignore(); // this line is optional
return 0;
}
Heres my code split into two functions, however it does not compile. I get an error at the while statement.-
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <iomanip>
using namespace std;
void displayFile( char []);
void main ()
{
int const wordLength = 21;
int const Num = 101;
int const fileSize = 255;
char filename[fileSize];
cout << "Please enter the name of the file you wish to open: "<< endl;
cin.getline(filename,fileSize);
displayFile (filename);
cin.ignore();
}
void displayFile (char fileName[] )
{
ifstream inFile;
char line [101];
inFile.open(fileName);
while (getline(inFile, line))
cout << line << endl;
inFile.close();
return 0;
}
My code is nearly identical except for the fact that I use character arrays instead of strings, so I'm not sure why its not compiling.
Also, is this a good way to start off the program? Or should I try something else.