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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
|
/*
Pig Latin Rules: The rules for translating a word to Pig Latin are as follows:
1). If the word begins with a consonant, move the first letter to the end of the word and add “ay”. For instance, chip becomes hipcay.
2). If the word begins with a vowel, add “way” to the end of the word. For instance, else becomes elseway.
Program Specifications:
1). Create a console application that prompts the user for a word to be translated and allows the user to input the word.
2). Once the input "word" is validated, output the user's "word" in pig latin.
3). Allow the program to run continuously until the user enters a value that indicates the program should end or exit.
4). Use a separate function to check the user’s input “word” for valid input. Invalid input includes any character(s) that are not alphabet characters (numbers, spaces, or special characters). If any “illegal” characters exist, prompt the user to correct the error, do not process the input “word”, and provide no translation output.
5). When checking that the user’s input consist of only alphabet characters, there is no need to check that the input “word” is actually a “dictionary word”.
6). The program should evaluate the characters “a, e, i, o, u” as vowels and evaluate the character “y” as a consonant.
*/
#include<iostream>
#include <iomanip>
#include<string>
using namespace std;
bool errorCheck(string);
void firstLetter(string);
void wordTranslate(string);
int main()
{
string wordInput=""; //array
bool inputError = false;
char runSentinel = 'y';
cout << " ----------------------\n" << endl;
cout << " PIG LATIN TRANSLATOR \n" << endl;
cout << " ----------------------\n" << endl;
cin.get();
do
{
cout << "Welcome to the Pig Latin Translator. \n" << endl;
cout << "Would you like to translate a word? (y/n)";
cin >> runSentinel;
if (runSentinel == 'y')
{
cout << "Please enter the word you would like to be translated, \n" << endl;
cout << "and please use only lower case letters. \n" << endl;
cin >> wordInput;
inputError = errorCheck(wordInput);
}
if (inputError == false)
{
firstLetter(wordInput);
//();
}
cin.ignore();
cout << "Would you like to translate a word? (y/n)";
cin >> runSentinel;
} while (runSentinel != 'n');
return 0;
}
bool errorCheck(string wordInput)
{
bool errorVerif = false;
int Length = wordInput.length();
int Counter = 0;
char *specificLetter = &wordInput.at(0);
while (Counter < Length-1 && errorVerif == false)
{
if (!isalpha(*specificLetter) || isspace(*specificLetter))
{
cout << *specificLetter << "is not an acceptable entry. Please re-enter your word.";
errorVerif = true;
}
*specificLetter++;
Counter++;
}
return errorVerif;
}
void firstLetter(string wordInput)
{
char firstLetter;
firstLetter = wordInput.at(0);
if (firstLetter == 'a' || 'e' || 'i' || 'o' || 'u')
{
cout << "In Pig Latin, that would be " << wordInput << "way";
}
else
{
wordTranslate(wordInput);
}
}
void wordTranslate(string wordInput)
{
char firstLetter;
char lastLetter = wordInput.length();
firstLetter = wordInput.at(0);
string otherLetters = wordInput.substr((firstLetter + 1), (lastLetter - 1));
cout << "In Pig Latin, that would be " << otherLetters << firstLetter << "ay." << endl;
}
|