Hey all,
I'm a beginner programmer and need help here. I can't figure out how to translate a word that begins with a consonant. I have all the code to translate a word that begins with a vowel, and those words translate correctly, but I cannot get the words with consonants to translate correctly. I have seen lots of examples on here of Pig Latin programs, but they don't seem to follow the type of parameters that we have to use for this class. Any help would really be appreciated, as I have been working like crazy trying to get this thing right, and I simply cannot.
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
string getMenuOption();
string translateLine(string);
string translateWord(string);
string getNextWord(string, int);
bool isVowel(char);
bool isPuncutation(char);
void splitWord(string, int, string&, string&);
//void splitWord(string, int, string&, string&);
// Main
int main()
string sentence, pigSentence;
string menuChoice;
menuChoice = getMenuOption();
{
string sentence, pigSentence;
string menuChoice;
menuChoice = getMenuOption();
if (menuChoice == "1")
{
ifstream inputFile;
inputFile.open("Program5Input.txt");
inputFile >> sentence;
}
else
{
cout << "Your sentence: ";
cout << endl;
getline(cin, sentence);
// call a function to translate a sentence
pigSentence = translateLine(sentence);
cout << pigSentence << endl;
}
system("pause");
return 0;
}
// Menu
string getMenuOption()
{
string menuChoice;
cout << "[1] to process a file";
cout << endl;
cout << "[2] to process a single sentence";
cout << endl;
cout << "Your choice: ";
getline(cin, menuChoice);
return menuChoice;
}
// Translate Line Function
string translateLine(string sentence)
{
int i = 0, length;
length = sentence.length();
string word, pigWord, pigSentence="";
while (i < length)
{
word = getNextWord(sentence, i);
i = i + word.length() + 1;
pigWord = translateWord(word);
pigSentence = pigSentence + pigWord + " ";
}
return pigSentence;
}
// Get Next Word Function
string getNextWord(string sentence, int i)
{
string word;
char letter;
letter = sentence[i];
while (letter != ' ' && i < sentence.length())
{
word = word + letter;
i = i + 1;
if (i < sentence.length())
letter = sentence[i];
}
return word;
}
// Translate Word Function
string translateWord(string word)
{
int i = 0;
string pigWord;
string first = "";
string second = "";
if (isVowel(word[0]) == true)
{
pigWord = word + "way";
}
else
{
// THIS IS WHAT I CAN'T FIGURE OUT
while (isVowel(word[i]) == false)
{
first = first + word[i];
i++;
}
splitWord(word, i, first, second);
pigWord = first + "ay";
}
return pigWord;
}
// Determine Whether Character is a Vowel
bool isVowel(char letter)
{
bool vowel;
letter = tolower(letter);
if (letter == 'a' || letter == 'e' ||
letter == 'i' || letter == 'o' ||
letter == 'u' )
{
vowel = true;
}
else
{
vowel = false;
}
return vowel;
}
void splitWord(string word, int i, string& first, string& second)
{
int l;
l = 0;
while (l < i)
{
first = first + word[i];
l++;
}
while (i < word.length())
{
second = second + word[i];