Start with the data. You're prompting the user for a sentence but most of the work is done with individual words. So You'll want to store the sentence as a vector of words.
Next, read the problem slowly and start sketching out in your editor the parts that you'll need:
Write a program that prompts a user for a sentence. (Each word in the sentence should be separated by a single space. The sentence should not contain punctuation.) |
That sounds like a function:
vector<string> getSentence();
Your program will then convert each word to Pig Latin |
We can do this word-by-word:
1 2 3 4
|
for each word in the sentence {
newWord = translate(word);
store newWord in the translated sentence
}
|
...and print out the translated sentence |
1 2 3
|
for each word in the translated sentence {
print the word and a space
}
|
After the sentence is successfully translated, the program should ask if the user would like another sentence translated. If the user does, the program should loop. If not the program should end. |
Oh, so we need all that stuff above in a loop and at the bottom it needs to prompt:
1 2 3 4 5 6
|
do {
all the stuff above
cout << "Do you want to do another sentence? ";
cin >> answer;
} while (answer == "yes");
|
Here come a whole bunch of rules. Fortunately, we decided to use a function to translate a single word, so all of the rules apply to our translate() function.
Now here's a trick: write the program, but start with a stub for the translate function. The stub doesn't translate anything, it just returns the original string:
1 2 3 4
|
string translate(const string &word)
{
return word;
}
|
Get the whole program working with this stub. Once all the other stuff is working, you can concentrate on writing the real translate() function.
Write code to implement each of the rules. If you're clever, you'll see that the first 3 rules can be expressed as a single rule instead.
Oh, it talks about consonants and vowels. Better write a little helper routine to tell if a character is a vowel:
bool isVowel(char ch)
Now go for it. If you get stuck for more than an hour, post whatever code you have and explain the problem that you're having.
Good luck!
Dave