### Comparing the first letter in a string array

So I have a text file (allWords.txt) that has a list of all the words in the dictionary, line by line. I want the user to think of a random word and the program guess it. So far I'm asking the user two questions to narrow down what word the user chose. Question 1.) What is the length of your word? Question 2.) What is the first letter of your word?

So far the program works perfectly up until the if statement...

`if(words[i].at(i) == firstLetter)`

It complies but when it runs it says...

`terminate called after throwing an instance of 'std:: out_of_range' what(): basic_string::at`

 ``12345678910111213141516171819202122232425262728293031323334353637383940`` ``````#include #include #include using namespace std; int main() { ifstream allWords("allWords.txt"); unsigned int wordLength; string getWord; string words[100000]; int numWithWordLength = 0; char firstLetter; cout << "Enter the length of your word..." << endl; cin >> wordLength; cout << "Enter the letter your word starts with..." << endl; cin >> firstLetter; while(allWords.good()) { getline(allWords, getWord); if(getWord.length() == wordLength) { words[numWithWordLength] = getWord; numWithWordLength++; } } for(int i = 0;i <= numWithWordLength;i++) { if(words[i].at(i) == firstLetter) { cout << words[i] << endl; } } return 0; }``````
If, say, you said that there is 100 words with 3 letters length, your loop will eventually try to access 100th letter of 100th 3-letter word. As there is clearly will not be 100th letter in 3-letter word, it throws an exceprtion.
Basically, what I'm trying to do is build a program that will be able to guess the users word with asking only 5 questions...

1.) Length of word?
3.) How many e's in your word?
4.) How many t's?
5.) How many a's?

I'm not even sure if it will work/is possible. I'm just trying it out as a theory, and will probably have to make adjustments to the questions.

I'm just having a little trouble with the code working...
So why are you testing i-th letter instead of first (index 0)?

 3.) How many e's in your word? 4.) How many t's? 5.) How many a's?
http://en.cppreference.com/w/cpp/algorithm/count
Wow, silly mistake on my behalf. Thanks for that.

So I changed it to...

`if(words[i].at(0) == firstLetter)`

Its printing out the words, but then still has a runtime error after printing out the words.

Last edited on
`i <= numWithWordLength`It should be <, not <=. You are reading past the end of array.
Thank you very much for your help.

I'm not sure what I'm doing wrong with this count statement tho...

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263`` ``````#include #include #include #include using namespace std; int main() { ifstream allWords("allWords.txt"); unsigned int wordLength; string getWord; string words1[100000]; string words2[1000]; string words3[100]; int numWithWordLength1 = 0; int numWithWordLength2 = 0; char firstLetter; int howManyEs; int numOfEs; char e = 'e'; cout << "Enter the length of your word..." << endl; cin >> wordLength; cout << "Enter the letter your word starts with..." << endl; cin >> firstLetter; cout << "Enter the the number of \"e's\" in your word..." << endl; cin >> howManyEs; while(allWords.good()) { getline(allWords, getWord); if(getWord.length() == wordLength) { words1[numWithWordLength1] = getWord; numWithWordLength1++; } } allWords.close(); for(int i = 0;i < numWithWordLength1;i++) { if(words1[i].at(0) == firstLetter) { words2[i] = words1[i]; numWithWordLength2++; } } for(int i = 0;i < numWithWordLength2;i++) { numOfEs = std::count(words2[i].begin, words2[i].end, e); if(numOfEs == howManyEs) { words3[i] = words2[i]; cout << words3 << endl; } } return 0; } ``````

The error I keep getting is...

error: no matching function for call to 'count(<unresolved overloaded function type>, <unresolved overloaded function type>, char)'|
Everything works perfect up until the count statement. If I can get that working I can write the rest of the program without a problem.
begin and end are member functions. It should be : `std::count(words2[i].begin(), words2[i].end(), e)`
Wow, again a silly mistake on my behalf! Thanks so much for your help. It's working perfect now.
Sorry, but I'm having yet another problem I cant figure out.

It compiles right and everything, but it will only work for words that start with 'a' or 'b'

I'm thinking it has something to do with array sizes or how I'm passing the arrays to each other (words2[i] = words1[i];)
 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980`` ``````#include #include #include #include using namespace std; int main() { ifstream allWords("allWords.txt"); unsigned int wordLength; string getWord; string words1[100000]; string words2[100000]; string words3[100000]; string words4[100000]; int numWithWordLength1 = 0; int numWithWordLength2 = 0; int numWithWordLength3 = 0; char firstLetter; int howManyEs; int numOfEs; int howManyTs; int numOfTs; char e = 'e'; char t = 't'; cout << "Enter the length of your word..." << endl; cin >> wordLength; cout << "Enter the letter your word starts with..." << endl; cin >> firstLetter; cout << "Enter the the number of \"e's\" in your word..." << endl; cin >> howManyEs; cout << "Enter the the number of \"t's\" in your word..." << endl; cin >> howManyTs; while(allWords.good()) { getline(allWords, getWord); if(getWord.length() == wordLength) { words1[numWithWordLength1] = getWord; numWithWordLength1++; } } for(int i = 0;i < numWithWordLength1;i++) { if(words1[i].at(0) == firstLetter) { words2[i] = words1[i]; numWithWordLength2++; } } for(int i = 0;i < numWithWordLength2;i++) { numOfEs = std::count(words2[i].begin(), words2[i].end(), e); if(numOfEs == howManyEs) { words3[i] = words2[i]; cout << words3[i] << endl; numWithWordLength3++; } } for(int i = 0;i < numWithWordLength3;i++) { numOfTs = std::count(words3[i].begin(), words3[i].end(), t); if(numOfTs == howManyTs) { words4[i] = words3[i]; cout << words4[i] << endl; } } return 0; } ``````

Last edited on
 ``12345`` ``````{ words2[i] = words1[i]; words2[numWithWordLength2] = words1[i]; numWithWordLength2++; }``````
Topic archived. No new replies allowed.