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)
array.int search ( int array[], int number, int key )
{
int pos = 0;
while(pos < number && array[pos] != key)
pos++;
if( pos == number)
pos = 0;
else
pos = 1;
return pos;
}
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.
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 tried to start off by reading the files and storing the words
#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];
ifstream inFile;
struct
{
char word[wordLength];
int count;
} wordCount;
wordCount array[Num];
cout << "Please enter the name of thr file you wish to open: "<< endl;
cin >> filename;
displayFile (filename);
}
void displayFile (char fileName [] )
{
ifstream infile;
char array [101];
char line [101];
int ch;
int i;
infile.open (fileName);
while ((ch = infile.peek()) != EOF)
{
infile.getline (line, 101);
line = array[i];
i++;
}
for (int j = 0; j<101; j++)
cout << array[j] << endl;
infile.close ();
Any tips would be appreciated, and thanks in advance.