Hi, I'm trying to find a solution to my code and I have general idea what the problem is but I don't know how to solve it please help me.

What the code does:
What my code does is it should read a dictionary file called dict.txt and store each of the words into an array. Then I run my functions to search for the words in the array.

Problem:
The problem I'm facing is that I supposedly read all the files into the array but when I run a search for the words, not all of them are there. But for some reason it won't read words like zucchini.

I know that it has something to do with the number of words in the dict.txt file and the size of my array. If I set the array size to be the same as the exact amount of words then my program will run without fail. However, I am forced to keep my array at 250,000 as it is a requirement for my task. The dict.txt file has about 233606 words therefore my array is roughly 17000 cells too large.

I have tried multiple input methods even a dict.!eof() but I'm not getting any results.

I have attached my dict.txt file along with the post incase someone wants to run some tests.

Here is my code:

#include <cstdlib>
#include <iostream>
#include <fstream>

using namespace std;

//Variables
string line, word, wordlist[250000];
int linenumber = 0, c = 0;
string temp;

//Function prototypes
void printMenu();
int binSearch (string a[], int n, string target);
bool pattern(string s1, string s2);
bool alpha (string s);
bool alpha2 (string s);

//Main code
int main()
{
	ifstream dict;
    dict.open("dict.txt");
    if (dict.fail())
    {
        cout << "No dictionary file" << endl;
        system("pause");
        return 0;
    }

    //Read dictionary into array to make multiple searches faster:

    while(!dict.fail())	//Makes sure empty lines aren't inserted into the array.
    {
    	while (dict >> wordlist[c])
    		c++;
    }

    //Loop to display menu, get user choice and perform required action:

	while (temp != "3")
	{
		printMenu();
		cin >> temp;
		int cmd = atoi(temp.c_str());

		if(cmd == 1)	//Word search using binSearch method.
		{
			cout << "Enter word " << flush;
			cin >> word;
			if(alpha(word))	//Checks if word is a valid input for option1.
			{
				for (unsigned int i = 0; i < word.length(); i++)	//Loop that converts word to lower-case.
				{
					if (isalpha(word.at(i)) == false)	//Checks if there are any letters in word that are not in the alphabet.
					{
						word.erase(i, 1);
						i--;
					}
					else
						word.at(i) = tolower(word.at(i));	//Converts word to lower-case before search.
				}
				if (binSearch(wordlist, 250000, word)== -1)
				{
					cout << word << " is not in dictionary"  << endl;	//binSearch = -1 (hence not found)
				}
				else
				{
					cout << word << " is in dictionary" << endl;	//binSearch = mid (hence found)
				}
			}
			else
				cout << "Word must be alphabetic" << endl;
		}

		else if (cmd == 2)
		{
			cout << "Enter word pattern with ? for missing letters " << flush;
			cin >> word;
			if(alpha2(word))	//Checks if word is a valid input for option2.
			{
				for (unsigned int i = 0; i < word.length(); i++)	//Loop that converts word to lower-case.
				{
					if ((isalpha(word.at(i)) == false) && (word.at(i) != '?'))	//Checks if there are any letters in word that are not in the alphabet.
					{
						word.erase(i, 1);
						i--;
					}
					else
						word.at(i) = tolower(word.at(i));	//Converts word to lower-case before search.
				}

				for (int k = 0; k < 250000; k++)	//Checks array for words.
				{
					if (pattern(word, wordlist[k]))	//Check if words match.
						cout << wordlist[k] << endl;	//Print words that match
				}
			}
			else
			{
				cout << "Word must be alphabetic" << endl;
			}
		}

		else if (cmd == 3)
		{
			goto Pause;	//Goes to pauce if cmd is equal to 3.
		}

		else
		{
			cout << "Invalid Choice" << endl;
		}
	}
	Pause:
    system("pause");
    return 0;
}

void printMenu()
{
   cout << "\tCrossword Helper " << endl << endl;
   cout << " 1 : Check if word is in dictionary " << endl;
   cout << " 2 : Find words in dictionary which match pattern " << endl;
   cout << " 3 : Quit " << endl;
}

int binSearch(string a[], int n, string target)	//Binary search function.
{
	int first = 0;
	int last = n-1;
	while (first <= last)
	{
		int mid = (first + last) / 2;
		if (a[mid] == target)
			return mid;	//Search returns true, word exists in dictionary.
		else if (a[mid] > target)
			last = mid - 1;
		else
			first = mid + 1;
	}
	return -1;	//Search returns false, word doesn't exist in dictionary.
}

bool pattern(string s1, string s2)	//Checks if pattern in s1 matches s2.
{
	if (s2.length() != s1.length()) return false;	//Checks if length matches, return false if it doesn't.
	for (unsigned int i=0; i < s1.length() ; i++)
	{
		if (s1.at(i) != '?')	//Loop checks if word has any ? in word, if not proceeds to next check.
		{
			if (s1.at(i) != s2.at(i))	//Checks if letters in word match if not return false.  Else check next letter.
			{
				return false;
		    }
		}
	}
	return true;
}

bool alpha (string s)	//Checks if string is a valid input for option1.
{
	for (unsigned int i = 0; i < s.length(); i++)
	{
		if(isalpha(s.at(i)))	//Makes sure all letters in word are alphabetic.
			return true;
	}
	return false;
}

bool alpha2 (string s)	//Checks if string is a valid input for option2.
{
	if((s.at(0)== '?') || isalpha(s.at(0)))  //If letter first letter is alpha or ?.
		return true;	//If true then word is valid input.
	else
		return false;
}
Attachments
a
aa
aal
aalii
aam
aani
aardvark
aardwolf
aaronic
aaronical
aaronite
aaronitic
aaru
ab
ababdeh
ababua
abac
abaca
abacate
abacay
abacinate
abacination
abaciscus
abacist
aback
abactinal
abactinally
abaction
abactor
abaculus
abacus
abadite
abaff
abaft
abaisance
abaiser
abaissed
abalienate
abalienation
abalone
abama
abampere
abandon
abandonable
abandoned
abandonedly
abandonee
abandoner
abandonment
abanic
abantes
abaptiston
abarambo
abaris
abarthrosis
abarticular
abarticulation
abas
abase
abased
abasedly
abasedness
abasement
abaser
abasgi
abash
abashed
abashedly
abashedness
abashless
abashlessly
abashment
abasia
abasic
abask
abassin
abastardize
abatable
abate
abatement
abater
abatis
abatised
abaton
abator
abattoir
abatua
abature
abave
abaxial
abaxile
abaze
abb
abba
abbacomes
abbacy
abbadide
abbas
abbasi
abbassi
abbasside
abbatial
abbatical
abbe
abbess
abbey
abbeystede
abbie
abbot
abbotcy
abbotnullius
abbotship
abbreviate
abbreviately
abbreviation
abbreviator
abbreviatory
abbreviature
abby
abc
abcoulomb
abdal
abdat
abderian
abderite
abdest
abdicable
abdicant
abdicate
abdication
abdicative
abdicator
abdiel
abditive
abditory
abdomen
abdominal
abdominales
abdominalian
abdominally
abdominoanterior
abdominocardiac
abdominocentesis
abdominocystic
abdominogenital
abdominohysterectomy
abdominohysterotomy
abdominoposterior
abdominoscope
abdominoscopy
abdominothoracic
abdominous
abdominovaginal
abdominovesical
abduce
abducens
abducent
abduct
abduction
abductor
abeam
abear
abearance
abecedarian
abecedarium
abecedary
abed
abeigh
abele
abelia
abelicea
abelite
abelmoschus
abelmosk
abelonian
abeltree
abencerrages
abenteric
abepithymia
aberdevine
aberdonian
aberia
aberrance
aberrancy
aberrant
aberrate
aberration
aberrational
aberrator
aberrometer
aberroscope
aberuncator
abet
abetment
abettal
abetted
abetting
abettor
abevacuation
abey
abeyance
abeyancy
abeyant
abfarad
abhenry
abhiseka
abhominable
abhor
abhorred
abhorrence
abhorrency
abhorrent
abhorrently
abhorrer
abhorrible
abhorring
abhorson
abidal
abidance
abide
abider
abidi
abiding
abidingly
abidingness
abie
abies
abietate
abietene
abietic
abietin
abietineae
abietineous
abietinic
abiezer
abigailship
abigeat
abigeus
abilao
ability
abilla
abilo
abintestate
abiogenesis
abiogenesist
abiogenetic
abiogenetical
abiogenetically
abiogenist
abiogenous
abiogeny
abiological
abiologically
abiology
abiosis
abiotic
abiotrophic
abiotrophy
abipon
abir
abirritant
abirritate
abirritation
abirritative
abiston
abitibi
abiuret
abject
abjectedness
abjection
abjective
abjectly
abjectness
abjoint
abjudge
abjudicate
abjudication
abjunction
abjunctive
abjuration
abjuratory
abjure
abjurement
abjurer
abkar
abkari
abkhas
abkhasian
ablach
ablactate
ablactation
ablare
ablastemic
ablastous
ablate
ablation
ablatitious
ablatival
ablative
ablator
ablaut
ablaze
able
ableeze
ablegate
ableness
ablepharia
ablepharon
ablepharous
ablepharus
ablepsia
ableptical
ableptically
abler
ablest
ablewhackets
ablins
abloom
ablow
ablude
abluent
ablush
ablution
ablutionary
abluvion
ably
abmho
abnaki
abnegate
abnegation
abnegative
abnegator
abnerval
abnet
abneural
abnormal
abnormalism
abnormalist
abnormality
abnormalize
abnormally
abnormalness
abnormity
abnormous
abnumerable
aboard
abobra
abode
abodement
abody
abohm
aboil
abolish
abolisher
abolishment
abolition
abolitionary
abolitionism
abolitionist
abolitionize
abolla
aboma
abomasum
abomasus
abominable
abominableness
abominably
abominate
abomination
abominator
abomine
abongo
aboon
aborad
aboral
aborally
abord
aboriginal
aboriginality
aboriginally
aboriginary
aborigine
aborning
abort
aborted
aborticide
abortient
abortifacient
abortin
abortion
abortional
abortionist
abortive
abortively
abortiveness
abortus
abouchement
abound
abounder
abounding
aboundingly
about
abouts
above
aboveboard
abovedeck
aboveground
abovementioned
aboveproof
abovestairs
abox
abracadabra
abrachia
abradant
abrade
abrader
abrahamic
abrahamidae
abrahamite
abrahamitic
abraid
abramis
abranchial
abranchialism
abranchian
abranchiata
abranchiate
abranchious
abrasax
abrase
abrash
abrasiometer
abrasion
abrasive
abrastol
abraum
abraxas
abreact
abreaction
abreast
abrenounce
abret
abrico
abridge
abridgeable
abridged
abridgedly
abridger
abridgment
abrim
abrin
abristle
abroach
abroad
abrocoma
abrocome
abrogable
abrogate
abrogation
abrogative
abrogator
abroma
abronia
abrook
abrotanum
abrotine
abrupt
abruptedly
abruption
abruptly
abruptness
abrus
absalom
absampere
absaroka
absarokite
abscess
abscessed
abscession
abscessroot
abscind
abscise
abscision
absciss
abscissa
abscissae
abscisse
abscission
absconce
abscond
absconded
abscondedly
abscondence
absconder
absconsa
abscoulomb
absence
absent
absentation
absentee
absenteeism
absenteeship
absenter
absentia
absently
absentment
absentminded
absentmindedly
absentness
absfarad
abshenry
absi
absinthe
absinthial
absinthian
absinthiate
absinthic
absinthin
absinthine
absinthism
absinthismic
absinthium
absinthol
absit
absmho
absohm
absolute
absolutely
absoluteness
absolution
absolutism
absolutist
absolutistic
absolutistically
absolutive
absolutization
absolutize
absolutory
absolvable
absolvatory
absolve
absolvent
absolver
absolvitor
absolvitory
absonant
absonous
absorb
absorbability
absorbable
absorbed
absorbedly
absorbedness
absorbefacient
absorbency
absorbent
absorber
absorbing
absorbingly
absorbition
absorpt
absorptance
absorptiometer
absorptiometric
absorption
absorptive
absorptively
absorptiveness
absorptivity
absquatulate
abstain
abstainer
abstainment
abstemious
abstemiously
abstemiousness
abstention
abstentionist
abstentious
absterge
abstergent
abstersion
abstersive
abstersiveness
abstinence
abstinency
abstinent
abstinential
abstinently
abstract
abstracted
abstractedly
abstractedness
abstracter
abstraction
abstractional
abstractionism
abstractionist
abstractitious
abstractive
abstractively
abstractiveness
abstractly
abstractness
abstractor
abstrahent
abstricted
abstriction
abstruse
abstrusely
abstruseness
abstrusion
abstrusity
absume
absumption
absurd
absurdity
absurdly
absurdness
absvolt
absyrtus
abterminal
abthain
abthainrie
abthainry
abthanage
abu
abucco
abuilding
abulia
abulic
abulomania
abuna
abundance
abundancy
abundant
abundantia
abundantly
abura
aburabozu
aburban
aburst
aburton
abusable
abuse
abusedly
abusee
abuseful
abusefully
abusefulness
abuser
abusion
abusious
abusive
abusively
abusiveness
abut
abuta
abutilon
abutment
abuttal
abuttals
abutted
abutter
abutting
abuzz
abvolt
abwab
aby
abysm
abysmal
abysmally
abyss
abyssal
abyssinian
abyssobenthonic
abyssolith
abyssopelagic
acacatechin
acacatechol
acacetin
acacia
acacian
acaciin
acacin
academe
academia
academial
academian
academic
academical
academically
academicals
academician
academicism
academism
academist
academite
academization
academize
academus
academy
acadialite
acadian
acadie
acaena
acajou
acaleph
acalepha
acalephae
acalephan
acalephoid
acalycal
acalycine
acalycinous
acalyculate
acalypha
acalypterae
acalyptrata
acalyptratae
acalyptrate
acamar
acampsia
acana
acanaceous
acanonical
acanth
acantha
acanthaceae
acanthaceous
acanthad
acantharia
acanthia
acanthial
acanthin
acanthine
acanthion
acanthite
acanthocarpous
acanthocephala
acanthocephalan
acanthocephali
acanthocephalous
acanthocereus
acanthocladous
acanthodea
acanthodean
acanthodei
acanthodes
acanthodian
acanthodidae
acanthodii
acanthodini
acanthoid
acantholimon
acanthological
acanthology
acantholysis
acanthoma
acanthomeridae
acanthon
acanthopanax
acanthophis
acanthophorous
acanthopod
acanthopodous
acanthopomatous
acanthopore
acanthopteran
acanthopteri
acanthopterous
acanthopterygian
acanthopterygii
acanthosis
acanthous
acanthuridae
acanthurus
acanthus
acapnia
acapnial
acapsular
acapu
acara
acarapis
acardia
acardiac
acari
acarian
acariasis
acaricidal
acaricide
acarid
acarida
acaridea
acaridean
acaridomatium
acariform
acarina
acarine
acarinosis
acarocecidium
acarodermatitis
acaroid
acarol
acarologist
acarology
acarophilous
acarophobia
acarotoxic
acarpelous
acarpous
acarus
acastus
acatalectic
acatalepsia
acatalepsy
acataleptic
acatallactic
acatamathesia
acataphasia
acataposis
acatastasia
acatastatic
acate
acategorical
acatery
acatharsia
acatharsy
acatholic
acaudal
acaudate
acaulescent
acauline
acaulose
acaulous
acca
accede
accedence
acceder
accelerable
accelerando
accelerant
accelerate
accelerated
acceleratedly
acceleration
accelerative
accelerator
acceleratory
accelerograph
accelerometer
accend
accendibility
accendible
accension
accensor
accent
accentless
accentor
accentuable
accentual
accentuality
accentually
accentuate
accentuation
accentuator
accentus
accept
acceptability
acceptable
acceptableness
acceptably
acceptance
acceptancy
acceptant
acceptation
accepted
acceptedly
accepter
acceptilate
acceptilation
acception
acceptive
acceptor
acceptress
accerse
accersition
accersitor
access
accessarily
accessariness
accessary
accessaryship
accessibility
accessible
accessibly
accession
accessional
accessioner
accessive
accessively
accessless
accessorial
accessorily
accessoriness
accessorius
accessory
accidence
accidency
accident
accidental
accidentalism
accidentalist
accidentality
accidentally
accidentalness
accidented
accidential
accidentiality
accidently
accidia
accidie
accinge
accipient
accipiter
accipitral
accipitrary
accipitres
accipitrine
accismus
accite
acclaim
acclaimable
acclaimer
acclamation
acclamator
acclamatory
acclimatable
acclimatation
acclimate
acclimatement
acclimation
acclimatizable
acclimatization
acclimatize
acclimatizer
acclimature
acclinal
acclinate
acclivitous
acclivity
acclivous
accloy
accoast
accoil
accolade
accoladed
accolated
accolent
accolle
accombination
accommodable
accommodableness
accommodate
accommodately
accommodateness
accommodating
accommodatingly
accommodation
accommodational
accommodative
accommodativeness
accommodator
accompanier
accompaniment
accompanimental
accompanist
accompany
accompanyist
accompletive
accomplice
accompliceship
accomplicity
accomplish
accomplishable
accomplished
accomplisher
accomplishment
accomplisht
accompt
accord
accordable
accordance
accordancy
accordant
accordantly
accorder
according
accordingly
accordion
accordionist
accorporate
accorporation
accost
accostable
accosted
accouche
accouchement
accoucheur
accoucheuse
account
accountability
accountab

I know that it has something to do with the number of words in the dict.txt file and the size of my array. If I set the array size to be the same as the exact amount of words then my program will run without fail. However, I am forced to keep my array at 250,000 as it is a requirement for my task. The dict.txt file has about 233606 words therefore my array is roughly 17000 cells too large.

What exactly are your forced to do? If you are forced to have an array CAPACITY of 250,000, that's fine. Technically that's the "size" of the array, but define the size as 233,606 and just ignore all the elements after that. Don't use them and definitely don't pass them uninitialized to a function that expects all of the elements to be ordered and to represent real data.

I have tried multiple input methods even a dict.!eof() but I'm not getting any results.

It has nothing to do with the input method. It has to do with the search method.

int binSearch(string a[], int n, string target)	//Binary search function.
{
	int first = 0;
	int last = n-1;
	while (first <= last)
	{
		int mid = (first + last) / 2;
		if (a[mid] == target)
			return mid;	//Search returns true, word exists in dictionary.
		else if (a[mid] > target)
			last = mid - 1;
		else
			first = mid + 1;
	}
	return -1;	//Search returns false, word doesn't exist in dictionary.
}

n here should be the number of data elements that actually have meaning (i.e. contain actual words), not the array capacity. You have 233,606 words, so that's what you should pass the function. You're making >, <, and == comparisons on elements. Do these have any meaning for element 240,000?

Or I suppose you could initialize elements 233,606 and beyond to "zzzzzzzzz", a "word" that doesn't exist and would be at the end of the alphabet, so any real word would be "less than" that and you thus get the correct range even if you pass the function 250,000.

Yeah, your right I just got too caught up with the whole input method because my lecturer kept saying I was using !eof incorrectly and wasn't reading the inputs correctly...

I found my solution and here it is:

int binSearch(string a[], int n, string target)
{
	int first = 0;
	int last = n-1;
	while (first <= last)
	{
		int mid = (first + last) / 2;
		if (a[mid] == target)
			return mid;
		else if (target.compare(a[mid]) < 0)  <-- .compare fixed it
			last = mid - 1;
		else
			first = mid + 1;
	}
	return -1;
}

I missed the > versus "compare" problem too. Glad you solved it.

[EDIT # 2]
Actually, strike that. What's wrong with using > here? It's just like using compare, right?
[/EDIT]

I missed the > versus "compare" problem too. Glad you solved it.

[EDIT # 2]
Actually, strike that. What's wrong with using > here? It's just like using compare, right?
[/EDIT]

Apparenty .compare compares everything from string length as well as the alphabet of each letter..

The way it was explained to me is that if you don't use compare then A and AB are equal. When in reality AB should be smaller because it comes after in the alphabet if this makes any sense. So it only checks the first letter of the string to see if its larger or smaller.

Apparenty .compare compares everything from string length as well as the alphabet of each letter..

The way it was explained to me is that if you don't use compare then A and AB are equal. When in reality AB should be smaller because it comes after in the alphabet if this makes any sense. So it only checks the first letter of the string to see if its larger or smaller.

Lines 12 and 15 both display for me in the program below.

#include <string>
#include <iostream>
using namespace std;


int main()
{
   string string1 = "AB";
   string string2 = "A";
   
   if (string1.compare (string2) > 0)
       cout << "compare : A is before AB\n";
       
   if (string1 > string2)
       cout << "> : A is before AB\n";
          
   cin.get ();
   return 0;
}

http://www.cppreference.com/wiki/string/string_operators

From the above link (string operators page):

Two strings are equal if:

1. Their size is the same, and
2. Each member in location i in one string is equal to the the member in location i in the other string.

http://www.cppreference.com/wiki/string/compare

From the above link (compare page)

The compare() function either compares str to the current string in a variety of ways, returning

Return Value Case

less than zero 	      this < str
zero 	              this == str
greater than zero     this > str

It looks like using "compare" and using > are equivalent here.

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