Hey I have to write a program that reads a text file that contains a list of words, 1 word per line. I have to store the unique words and count the occurrences of each unique word. When the file is completely read, I have to print the words and the number of occurrences to a text file. The output should be the words in alphabetical order along with the number of times they occur. Then print to the file some statistics:
I have to use character arrays instead of strings.
I must use the linear search (something that looks like this)
to determine if a word is in the array. The array is an array of structures and that the key is a char array so the string comparison must be used. The search task should be a separate function.Code:int search (int list [], int size, int key) { int pos = 0; while (pos < size && list[pos] != key) pos++; if (pos == size) pos = -1; return pos; }
The search must be a separate function that returns an integer values. I cant use a for loop and the function must have only one return statement.
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-
Heres my code split into two functions, however it does not compile. I get an error at the while statement.-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; }
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.Code:#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; }#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; }
Also, is this a good way to start off the program? Or should I try something else.




Reply With Quote