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

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
#include <iostream>
#include<fstream>
#include<string>

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?
2.) What does your word start with?
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...
2.) What does your word start with?
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.

Also, thanks for the links.
Last edited on
i <= numWithWordLengthIt 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...

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
#include <iostream>
#include<fstream>
#include<string>
#include <algorithm>

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];)
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
72
73
74
75
76
77
78
79
80
#include <iostream>
#include<fstream>
#include<string>
#include <algorithm>

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
1
2
3
4
5
{
    words2[i] = words1[i];
    words2[numWithWordLength2] = words1[i];
    numWithWordLength2++;
}
Topic archived. No new replies allowed.