Hello,
I'm working on a simple Game for class but can't seem to correct my runtime error. It is supposed to calculate to Pig Latin via the use of a Vector, Iterator and Subtr. The problem is as the reaches the if statement, it skips the condition (which seems to be true) and goes to the else bracket. Here's the syntax:
for(iter = vowel.begin(); iter != vowel.end(); iter++)
word_choice = *iter;
cout << "THE PIG LATIN GAME" << endl;
cout << "\nThe Rules:\n" ;
cout << "In words that begin with: " ;
cout << "\n - vowel sounds " ;
cout << "\n - silent consonants at the first position";
cout << "\n - and the letter 'y' ";
cout << "\nThe syllable 'way' is added to the end of the word." << endl;
cout << "\n\nLets Begin!\n\nEnter your word for translation: ";
cin >> word;
The errors I see in the program are variable type mismatches.
for example, the variable found is the incorrect type and I figure I am wasting a lot of effort to do the this task. My rewrite should help you out which I supply below.
1 2 3 4 5 6 7 8 9 10 11 12 13
if(word_choice.find(word) != string::npos)
{
found = word_choice.find(word);
word1 = word.substr(found, word.size());
word2 = word.substr(0, found);
pigword = word1 + "-" + word2 + "way";
cout <<"\nThe Pig Latin word for "<<word<<" is "<<pigword<<endl;
}
else
{
cout <<"\nWord entered does not contain vowels.\n"<<endl;
}
my rewrite:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// finding the string, we only need to preform once.
// note the type difference, I am wondering if you had a type mismatch causing the exception.
// this is what you should have for your code.
size_t found = word_choice.find(word);
if(found != string::npos}
{
word1 = word.substr(found, word.size());
word2 = word.substr(0, found);
cout <<"\nThe Pig Latin word for "<<word<<" is "<<pigword<<endl;
}
else
{
cout <<"\nWord entered does not contain vowels.\n"<<endl;
}
And for the given problem, should you be scanning the input "word" for "word_choice", and not scanning "word_choice" for "word", like you have it? Don't I want to find the vowel in the inputted word?
Edit: I keep looking at the code as the wrong logic: You need to scan each letter of word in word_choice list, or I scan word with each member of the word choice list. I don't see this logic in your code.
Ok, I see. Thank you. I found another method of doing it which I'm going to try and implement now but still, thanks for the reply. (I'm a noob at this)