Why am I only getting around half the content of my file printed and not all of it?
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 UTC
Oct 6, 2017 at 5:37am UTC
Put the file on pastebin.
Oct 6, 2017 at 5:55am UTC
Also, check that the input is working. (don't just loop and hope).
Here I count how many words are read, and also confirm that end of file is reached.
Notice the size number passed to the other functions is the number of items which were actually read, not the array size.
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
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
void selectionSort(string [], int );
void showDictionaryWords(string [], int );
int main()
{
const int DICTIONARY_SIZE =16000;
string dictionaryWords[DICTIONARY_SIZE];
ifstream infile("Unsorted_Dictionary.txt" ); // open dictionary file
// check if files opened
if (!infile)
{
cout << "Dictionary file could not be opened\n" ;
return 1;
}
// loop for dictionary words
int count = 0;
while ( count < DICTIONARY_SIZE && infile >> dictionaryWords[count] )
{
++count;
}
cout << "Number of words read: " << count << '\n' ;
cout << "End of file reached: " << boolalpha << infile.eof() << '\n' ;
selectionSort(dictionaryWords, count); // sort file
showDictionaryWords(dictionaryWords, count); // print file
}
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 size)
{
for (int i=0; i<size; i++)
{
cout << dictionaryWords[i] << endl;
}
}
Oct 6, 2017 at 7:27pm UTC
Thanks for the tips chervil
Topic archived. No new replies allowed.