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;
}

As an aside, please use int main() instead of void main() . While some compilers accept void , it is not actually correct according to the C++ standard.

On a related topic, under the current standards (both C++98 and C++11), the older C-style library headers all start with 'c' and do not have the '.h' extension. For example, <string.h> and <stdlib.h> are now <cstring> and <cstdlib> . Just so you are aware.

Edited 4 Years Ago by Schol-R-LEA: n/a

I'm not sure that the earlier parts of the program are working correctly, either. This is a transcript of a run I made after compiling the program under GCC 4.4.1:

Type in a file name (ex: inputFile.txt) and press Enter: input.txt

Storing Input File Name...

Opening File...
Reading file...
Finished copying file 1
programming is for progr
Closing input file...DONE!
Converting file list to lowercase letters: 1
toLower: p
toLower: r
toLower: o
toLower: g
toLower: r
toLower: a
toLower: m
toLower: m
toLower: i
toLower: n
toLower: g
toLower:
toLower: i
toLower: s
toLower:
toLower: f
toLower: o
toLower: r
toLower:
toLower: p
toLower: r
toLower: o
toLower: g
toLower: r
toLower: ■
toLower:  
toLower:  
toLower:  
toLower: z
toLower: ◄
toLower: >
toLower: w
toLower: Ç
toLower: ☼
toLower: >
toLower: w
toLower: (
toLower: ⌠
toLower: "
....DONE!
1Sorting list...............DONE!
programming is for progr, 0
Creating Unique List.....DONE!
Opening Output File...DONE!
Writing to output file....
DONE!
Closing output file..........DONE!

As you can see, it fails to read the file correctly. I would address this problem first, or, if it is working for you but not me, determine why it is behaving differently under different compilers.

This article has been dead for over six months. Start a new discussion instead.