I'm having a few issues with my Pig Latin program.
First, The program doesn't loop correctly. I need it to keep prompting the user for input until the user presses CTRL+D, but I can't figure out how. Right now, it never re-writes the line "Enter some text (CTRL-D to end):".
Also, two more issues: the program isn't properly capitalizing the output, and the program always puts an extra " ay" at the end of every word that begins with a consonant.
Here's what the output should look like:
Enter some text (CTRL-D to end):
Pig Latin rules!
Igpay Atinlay ulesray!
Enter some text (CTRL-D to end):
My name is James!
Ymay amenay isway Amesjay!
Enter some text (CTRL-D to end):
<CTRL-D>
Goodbye.
And this is what My output looks like:
Enter some text (CTRL-D to end):
My name is James!
yMay ay amenay ay isway amesJay! ay!
Any help with my issues would be greatly appreciated.
Thanks!
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
string pigLatin(string);
bool isVowel(string);
bool isCapital (char);
bool isConsonant (string);
string flipword (string);
string punctuation (string,string &);
int main()
{
string x;
cout<<"Enter some text (CTRL-D to end): "<<endl;
cin>>x;
while(!cin.eof())
{
pigLatin(x);
cin>>x;
}
cout<<endl;
cout<<"Goodbye"<<endl;
return 0;
}
//****************************************************************
//Function:pigLatin
//Assumption(s): x is a string that contains a single word to be converted to
//pig latin.
//Action(s):Takes the string of x and manipulates it into pigLatin
//****************************************************************
string pigLatin (string x)
{
char a;
string puncmark;
if (isVowel(x) == true)
{
x = punctuation(x,puncmark);
cout << x << "way" << puncmark << " ";
}
else if (isConsonant(x) == true)
{
x = flipword(x);
for (int i = 0; i <= x.length(); i++)
{
a = x[i];
if(isCapital(a) == 1)
{
x[i] = tolower(x[i]);
x[0] = toupper(x[0]);
}
x = punctuation(x,puncmark);
cout << x << "ay" << puncmark << " ";
x = " ";
}
}
}
//****************************************************************
//Function:isVowel
//Assumption(s): x is the word that it checked if the first letter is a vowel
//Action(s):Checks if the first letter of the word is a vowel and returns
//a true or false.
//****************************************************************
bool isVowel(string x)
{
if(x[0]=='a'||x[0]=='e'||x[0]=='i'||x[0]=='o'||x[0]=='u'||x[0]=='y'||
x[0]=='A'||x[0]=='E'||x[0]=='I'||x[0]=='O'||x[0]=='U'||x[0]=='Y')
{
return true;
}
else
{
return false;
}
}
//****************************************************************
//Function:isConsonant
//Assumption(s): x is the word that it checked if the first letter is a consonant
//Action(s):Checks if the first letter of the word is a consonant and returns
//a true or false.
//****************************************************************
bool isConsonant (string x)
{
if (x[0] == 'b' || x[0] == 'B' || x[0] == 'c' || x[0] == 'C' || x[0] == 'd' || x[0] == 'D' || x[0] == 'f' || x[0] == 'F' || x[0] == 'g' || x[0] == 'G' || x[0] == 'h' || x[0] == 'H' || x[0] == 'j' || x[0] == 'J' || x[0] == 'k' || x[0] == 'K' || x[0] == 'l' || x[0] == 'L' || x[0] == 'm' || x[0] == 'M' || x[0] == 'n' || x[0] == 'N' || x[0] == 'p' || x[0] == 'P' || x[0] == 'q' || x[0] == 'Q' || x[0] == 'r' || x[0] == 'R' || x[0] == 's' || x[0] == 'S' || x[0] == 't' || x[0] == 'T' || x[0] == 'v' || x[0] == 'V' || x[0] == 'w' || x[0] == 'W' || x[0] == 'x' || x[0] == 'X' || x[0] == 'z' || x[0] == 'Z')
{
return true;
}
else return false;
}
//****************************************************************
//Function:flipword
//Assumption(s): x is the word that will be manipulated and returned
//Action(s):Takes the part of the word that contains consonates at beginning
//and moves them to the end.
//****************************************************************
string flipword(string x)
{
int consonate_size;
string word;
int a=0;
for (int i=0; i<=x.length();i++)
{
//checks how many consonants before the first vowel
if(x[i]=='a'||x[i]=='e'||x[i]=='i'||x[i]=='o'||x[i]=='u'|| x[i]=='y'||
x[i]=='A'||x[i]=='E'||x[i]=='I'||x[i]=='O'||x[i]=='U'|| x[i]=='Y')
{
consonate_size=i;
break;
}
}
//finds all the letters after
for(int i=consonate_size; i<=x.length();i++)
{
word +=x[i];
}
//finds consonates at begining of word
for(int i=0; i<=consonate_size-1; i++)
{
word +=x[i];
}
// cout<<"word is "<<word<<endl;
return word;
}
//****************************************************************
//Function:isCapital
//Assumption(s): a is a single character from the string x
//Action(s):Checks if the character a is capital
//****************************************************************
bool isCapital(char a)
{
if (isupper(a))
{
return true;
}
else
{
return false;
}
}
//****************************************************************
//Function:punctuation
//Assumption(s):-x is the word that will be converted
// -puncmark is the punctuation mark that will be
// returned by reference
//Action(s):Checks for punctuation marks in the word and moves them to the end
//****************************************************************
string punctuation(string x, string &puncmark)
{
int mark;
string word;
for (int i=0; i<=x.length();i++)
{
//checks for punctuation marks
if(x[i]=='.'|| x[i]==','||x[i]=='?'||x[i]=='!'||x[i]==':'||x[i]==';')
{
mark=i;
puncmark=x[mark];
break;
}
else
{
mark=x.length();
}
}
for(int i=0; i<mark; i++)
{
word +=x[i];
}
for(int i=mark+1; i<x.length(); i++)
{
word +=x[i];
}
return word;
}