Mar 12, 2014 at 8:33pm UTC
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.
Mar 12, 2014 at 8:37pm UTC
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...
Mar 12, 2014 at 9:22pm UTC
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 Mar 12, 2014 at 9:26pm UTC
Mar 12, 2014 at 9:46pm UTC
i <= numWithWordLength
It should be <, not <=. You are reading past the end of array.
Mar 12, 2014 at 10:10pm UTC
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.
Mar 12, 2014 at 10:12pm UTC
begin and end are member functions. It should be : std::count(words2[i].begin() , words2[i].end() , e)
Mar 12, 2014 at 10:21pm UTC
Wow, again a silly mistake on my behalf! Thanks so much for your help. It's working perfect now.
Mar 12, 2014 at 11:08pm UTC
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];)
Mar 12, 2014 at 11:08pm UTC
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 Mar 12, 2014 at 11:44pm UTC