Why is my file getting sorted weird

closed account (EAk1vCM9)
Hello guys , so I im working on this program were I have read two files (one file is a dictionary that has 16,000 words) and the other one has keywords (only has 84 keywords) and then I have to store them In arrays,and after that I have to sort them. They need to be sorted because after were going to have to use bionary search to see if the keywords are in the dictionary(there should be between 20-30 keywords missing). Right now Im trying to check if their sorted correctly by printing them on a file, however when I open the file to see what was printed the words go from A-Z but then it starts again from A-Z but with different words(all the words printed are still words from the file)
Now for this program I dont have to print out the sorted words to another file.(on the file I just need to print all the keywords not found on the dictionary ex: keyword not found: alignas) But like I said earlier Im doing this to check if they are getting sorted correctly.
So why are they getting sorted A-Z and A-Z ? and how can I fix it? ps. as of right now ive only touched the dictionary file,im not allowed to use vectors,and only allowed to use bubble or selection sort. here the link to the file im using https://pastebin.com/frRJQTpd

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 PrintDictionaryWordsToFile(string [], int );

const int KEYWORD_SIZE = 84;
const int DICTIONARY_SIZE =16000;


int main() {
	string dictionaryWords[DICTIONARY_SIZE], keywords[KEYWORD_SIZE];
	ifstream infile,infileKeywords;

	infile.open("Unsorted_Dictionary.txt"); //opens dictionary file

	if (!infile) // 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
		}
		infile.close();
		selectionSort(dictionaryWords,DICTIONARY_SIZE); //sorts file
		PrintDictionaryWordsToFile(dictionaryWords,DICTIONARY_SIZE); //prints fill
	}
	cout <<"Done writing to 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 PrintDictionaryWordsToFile(string dictionaryWords[], int DICTIONARY_SIZE)
{
	ofstream outputFile;
	outputFile.open("TestLab2");
	for(int i=0;i<DICTIONARY_SIZE;i++)
	{
		outputFile << dictionaryWords[i]  << endl;
	}
	outputFile.close();
}

here the visual of the problem:
Williamson
Willis
Willoughby
Wills
Wilmington
Wilshire
Wilson
Winifred
Winnetka
Winnie
Winnipeg
Winnipesaukee
Winslow
Winston
Winters
Wolcott
Wolfe
Woodard
Woodbury
Woodrow
Woodward
Wooster
Worcester
Wordsworth
Worthington
Wotan
Wrigley
Wronskian
Wu
Wuhan
Wyandotte
Wyeth
Wyner
Wyoming
Xerox
Xerxes
YMCA
YWCA
Yakima
Yale
Yalta
Yamaha
Yankee
Yankton
Yaqui
Yates
Yeats
Yellowknife
Yemen
Yerkes
Yiddish
Yoder
Yokohama
Yokuts
Yonkers
Yorktown
Yosemite
Youngstown
Ypsilanti
Yucatan
Yugoslavia
Yuki
Yukon
Yves
Zambia
Zanzibar
Zeiss
Zellerbach
Zen
Zeus
Ziegler
Zion
Zionism
Zomba
Zurich
a
aback
abalone
abandon
abash
abate
abater
abbas
abbe
abbey
abbot
abbreviate
abdicate
abdomen
abdominal
abed
aberrant
aberrate
abet
abetted
abeyance
abeyant
abject
ablaze
able
abnormal
aboard
abode
abolish
abolition
abominable
abominate
aboriginal
aborigine
aborning
abort
about
above
aboveground
abovementioned
abrade
abrasion
abreact
abreast
abridge
abridgment
abrupt
abscess
abscissa
absence
absentee
absenteeism
absentminded
absinthe
absolute
absolution
absolve
absorb
absorbent
absorption
abstain
abstinent
abstracter
abstruse
absurd
abuilding
abundant
abusable
abuse
abusive
abutted
abutting
abysmal
abyss
academe
academia
academic
academy
acanthus
accelerate
accentual
accentuate
accept
acceptant
acceptor
access
accession
accessory
accidental
accipiter
acclaim
acclimate
acclimatize
accolade
accompaniment
accompany
accomplice
accomplish
accord
accordion
accost
account
accountant
accreditation
accrue
acculturate
accumulate
accuracy
accusation
accuse
acerbic
acetate
acetic
acetylene
achromatic
acidic
acidulous
acknowledge
acknowledgeable
acolyte
acorn
acoustic
acquaint
acquiesce
acquiescent
acquisition
acquit
acquittal
acrid
acrimonious
acrimony
acrobat
acrobatic
acropolis
acrylate
acrylic
actinide
actinium
actinometer
activate
activation
actress
acuity
acyclic
ad
adage
adagio
adamant
adapt
adaptive
add
added
addend
addendum
addition
adept
adequacy
adherent
adhesion
adjacent
adjective
adjoint
adjudge
adjust
adjutant
administer
administrate
administration
admiral
admire
admissible


Last edited on
What you are seeing is the difference between upper and lower case.
Upper case A-Z are represented by 65-90.
Lower case a-z are represented by 97-122.
Therefore a word starting with upper case is going to sort before lower case.
http://www.asciitable.com/

In your sorting function you need to be case-sensitive.
Example:
1
2
3
4
5
6
  string s1("anna"), s2("Lisa");

  if (s1 > s2)
    cout << s1 << " is greather than " << s2;
  else
    cout << s1 << " is smaller than " << s2;

Output:
anna is greather than Lisa
Topic archived. No new replies allowed.