Hi,
I am learning C++ and am writing a program to create a Unique List of words. I am having problems getting my struct count field to increment in my createUnique function. After returning from the Search function, the count field within the struct needs to increment if the key was found in the array. For example, I have a text file with the words "programming is for programming" in it and the word programming would have a count of 2 while the rest of the words have a count of 1.
I also need to use the Linear and Search Sort algorithms in my code. Any suggestions are much appreciated.
Here is my code:
#include <iostream>
#include <fstream>
#include <string.h>
using namespace std;
const int MAX_WORDS = 100;
const int MAX_LENGTH = 21;
const int MAX_FILENAME = 81;
struct list
{
char word[21];
int count;
};
//Function Prototypes
void getFile(char inputFile[]);
void displayFile(char inputFile[],list tempList[], int&);
void readFile (ifstream&, char inputFile[],list tempList[],int&);
void fileClose (ifstream&);
void toLower (list tempList[], int);
void sortList(list tempList[], int);
void createUnique(list tempList[], list finalList[], int);
int search(list finalList[], int, char[]);
void totalWords(list finalList[], int);
void makeOutputName(char inputFile[], list finalList[]);
void openOutput(char outputFile[],list finalList[]);
void writeOutput(ofstream&, list finalList[]);
void closeFile(ofstream&);
//Main Body
void main()
{
list tempList[MAX_WORDS];
list finalList[MAX_WORDS];
char inputFile[MAX_LENGTH];
int arraySize = 0;
//Functions
getFile(inputFile);
displayFile(inputFile, tempList, arraySize);
createUnique(tempList, finalList, arraySize);
totalWords(finalList, arraySize);
makeOutputName(inputFile, finalList);
//Main Cleanup
cout << endl;
}
void getFile(char inputFile[])
{
//Main
cout << "\nType in a file name (ex: inputFile.txt) and press Enter: ";
cin.getline(inputFile,MAX_FILENAME,'\n');
cout << "\nStoring Input File Name...\n" << endl;
}
void displayFile (char inputFile[], list tempList[], int& arraySize)
{
cout << "Opening File..." << endl;
ifstream infile;
infile.open(inputFile);
if(infile.fail())
{
cout << "The file could not be opened\n";
exit(1);
}
readFile(infile, inputFile, tempList, arraySize);
}
void readFile (ifstream& file, char inputFile[],list tempList[], int& arraySize)
{
int ch;
cout << "Reading file..." << endl;
while((ch = file.peek()) != EOF)
{
file.getline(tempList[arraySize].word,MAX_FILENAME);
tempList[arraySize].count = 0;
arraySize++;
}
cout << "Finished copying file " << arraySize << endl;
cout << tempList[0].word << endl;
fileClose(file);
toLower(tempList, arraySize);
sortList(tempList, arraySize);
}
void fileClose(ifstream& file)
{
cout << "Closing input file";
file.close();
cout << "...DONE!" << endl;
}
void toLower (list tempList[],int arraySize)
{
cout << "Converting file list to lowercase letters: " << arraySize << endl;
while(arraySize >= 0)
{
int pos = 0;
do
{
tempList[arraySize - 1].word[pos] = tolower(tempList[arraySize - 1].word[pos]);
cout << "toLower: " << tempList[arraySize - 1].word[pos] << endl;
pos++;
}while(pos < strlen(tempList[arraySize - 1].word));
arraySize--;
}
cout << "....DONE!" << endl;
}
void sortList(list tempList[],int arraySize)
{
int i, j, minidx, moves = 0;
char min[21];
char temp[21];
cout << arraySize << "Sorting list...........";
for(i = 0; i < (arraySize - 1); i++)
{
strcpy(min, tempList[i].word);
minidx = i;
for(j = i + 1; j < arraySize; j++)
{
strcpy(temp, tempList[j].word);
if(strcmp(temp,min) <0)
{
strcpy(min, tempList[j].word);
minidx = j;
}
}
if (strcmp(min, tempList[i].word) < 0)
{
strcpy(temp, tempList[i].word);
strcpy(tempList[i].word, min);
strcpy(tempList[minidx].word, temp);
moves++;
}
}
cout << "....DONE! " << endl;
for(int x = 0; x < arraySize; x++)
cout << tempList[x].word << ", " << tempList[x].count << endl;
}
void createUnique(list tempList[], list finalList[], int arraySize)
{
cout << "Creating Unique List.....";
int checkArray = 0;
int x = 0;
int tmpCounter = 0;
int finalCounter = 0;
char key[21];
for(tmpCounter = 0; tmpCounter < arraySize; tmpCounter++)
{
checkArray = search(finalList, finalCounter, tempList[tmpCounter].word);
if (checkArray != -1)
{
strcpy(finalList[finalCounter].word, tempList[tmpCounter].word);
finalList[finalCounter].count = 1;
}
else
{
finalList[finalCounter].count = finalList[finalCounter].count + 1;
}
finalCounter++;
}
cout << "DONE!"<< endl;
/*while(strlen(finalList[x].word) > 0)
{
cout << finalList[x].word << ", " << finalList[x].count << endl;
x++;
}*/
}
int search(list finalList[], int finalCounter, char key[])
{
int position = 0;
int compareResult;
compareResult = strcmp(finalList[position].word,key);
while (position < finalCounter && strcmp(finalList[position].word, key) != 0)
{
position++;
}
if (position == finalCounter)
{
position = -1;
}
return position;
}
void totalWords(list finalList[],int arraySize)
{
int y = 0;
int countWords = 0;
do
{
countWords += finalList[y].count;
y++;
}while(y <= arraySize);
}
void makeOutputName(char inputFile[], list finalList[])
{
int length = 0;
length = strlen(inputFile);
char outputFile[MAX_LENGTH];
strcpy(outputFile, inputFile);
outputFile[length - 3] = 'o';
outputFile[length - 2] = 'u';
outputFile[length - 1] = 't';
openOutput(outputFile, finalList);
}
void openOutput(char outputFile[],list finalList[])
{
cout << "Opening Output File...";
ofstream outfile;
outfile.open(outputFile);
if(outfile.fail())
{
cout << "The file could not be opened\n";
exit(1);
}
cout << "DONE!" << endl;
writeOutput(outfile, finalList);
closeFile(outfile);
}
void writeOutput(ofstream& outfile,list finalList[])
{
int x = 0;
cout << "Writing to output file....\n";
outfile << "\t words in\t\tnumber of" << endl;
outfile << "\tsorted order\t\toccurences" << endl;
outfile << "--------------------------------------------------------" << endl;
do
{
outfile << finalList[x].word;
outfile << '\t\t' << finalList[x].count << endl;
x++;
}while(strlen(finalList[x].word) > 0);
cout << "DONE!" << endl;
}
void closeFile(ofstream& outfile)
{
cout << "Closing output file..........";
outfile.close();
cout << "DONE!" << endl;
}