Oct 6, 2017 at 4:19am Oct 6, 2017 at 4:19am UTC
So I need to read words from a file and sort them. I managed to do that but when I print them to see if there sorted it only show 8689 words out of 16,000
words. The last line (8689) is also blank for some reason. any ideas on how why this happened and how to fix it?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
void selectionSort(string [], int );
void showDictionaryWords(string [], int );
const int KEYWORD_SIZE = 84;
const int DICTIONARY_SIZE =16000;
int main() {
string dictionaryWords[DICTIONARY_SIZE], keywords[KEYWORD_SIZE];
ofstream outputFile;
ifstream infile,infileKeywords;
infile.open("Unsorted_Dictionary.txt" ); //opens dictionary file
infileKeywords.open("keywords.txt" ); //opens keywords file
if (!infile || !infileKeywords) // checks if file opened
{
cout << "One of the files could not be opened\n" ;
return 1;
}
else
{
for (int i =0; i < DICTIONARY_SIZE; i++) // loop for dictionary words
{
infile >> dictionaryWords[i]; //gets words from file
}
selectionSort(dictionaryWords,DICTIONARY_SIZE); //sorts file
showDictionaryWords(dictionaryWords,DICTIONARY_SIZE); //prints file
}
return 0;
}
void selectionSort(string array[],int size)
{
int startScan,minIndex;
string minValue;
for (startScan=0;startScan < (size-1); startScan++)
{
minIndex=startScan;
minValue = array[startScan];
for (int index= startScan +1; index <size; index++)
{
if (array[index] <minValue)
{
minValue= array[index];
minIndex=index;
}
}
array[minIndex]= array[startScan];
array[startScan] = minValue;
}
}
void showDictionaryWords(string dictionaryWords[], int DICTIONARY_SIZE)
{
for (int i=0;i<DICTIONARY_SIZE;i++)
{
cout << dictionaryWords[i] <<endl;
}
}
file I used has 16,000 words and character limit is 9,000 so I cant really post it,hopefully you guys wont need it .
Last edited on Oct 6, 2017 at 4:21am Oct 6, 2017 at 4:21am UTC
Oct 6, 2017 at 5:37am Oct 6, 2017 at 5:37am UTC
Put the file on pastebin.
Oct 6, 2017 at 7:27pm Oct 6, 2017 at 7:27pm UTC
Thanks for the tips chervil