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;
}

Recommended Answers

All 9 Replies

See if this works.

Also since you are using string and cctype header, you should capitalize
on their functions.

I changed your functions a little bit :

#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;
  
	do{	 
		pigLatin(x);      	  
		cin>>x;
	}while(!cin.eof() && !cin.fail());
  
	cin.clear();
	while(cin.get() != '\n')
		continue;

   cout<<endl<<"GoodBye!\n"<<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 (unsigned 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 = " ";
   }
   }
  return 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)   {
	string vowels = "aeiou"; //our vowels
	char v = tolower(x[0]);  //the lowered case char we are checking if its a vowels
	return (vowels.find(v) != string::npos); //check if the lowered case char matches one of our vowels
}

//****************************************************************
//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){
	char ch = tolower(x[0]); //lower case it if its a letter
	return ( ch >= 'b' && ch <= 'z' );  
}

//****************************************************************
//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 = 0;
   string word;
   int a=0;
   string vowels = "aeiou";

   for (unsigned int i=0; i<=x.length();i++)
   {
	   char ch = tolower(x[i]);
      //checks how many consonants before the first vowel
	   if( vowels.find(ch) != string::npos ) {
         consonate_size=i;
         break;
      }
   }
   
   //finds all the letters after
   for(unsigned 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 (unsigned int i=0; i<=x.length();i++)
   {	   
      //checks for punctuation marks
      //if(x[i]=='.'|| x[i]==','||x[i]=='?'||x[i]=='!'||x[i]==':'||x[i]==';')
	  if(ispunct(x[i]))
      {
         mark=i;
         puncmark=x[mark];
         break;
      }
      else
      {
         mark=x.length();
      }
   }
   for(int i=0; i<mark; i++)
   {
      word +=x[i];
   }
   for(unsigned int i=mark+1; i<x.length(); i++)
   {
      word +=x[i];
   }

   return word;
}

Thanks for the reply.
The code you mentioned won't remedy my loop problem. The line "Enter some text (CTRL-D to end):" needs to be incorporated into the loop so that the output looks exactly like this:

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!

I've tried every loop I can think of in every different combination, and I can't figure out how to make the loop output the text exactly as shown.

How the heck can I get this loop to work so the output looks correct?

Is it CNTRL-D or CNTRL-C ? What system are you running?

[edit]

You can do this :

string x("");

	char END[2] = { 0x04,'\0' };
   
   cout<<"Enter some text (CTRL-D to end): "<<endl;
   cin>>x;
  
	do{	 
		pigLatin(x);   
		cin >> x;
	}while(x != END);
  
	cin.clear();
	while(cin.get() != '\n')
		continue;

   cout<<endl<<"GoodBye!\n"<<endl;

From this

It's CTRL-D, I am working on a Unix system.

I'll let you know if I have any problems with the loop you just mentioned, but I probably won't have time to implement it until morning.

Is there any way to make the loop work without using the ASCII stuff? I'm fairly sure we aren't allowed to use that in our program.

Okay, so...
I've implemented the code you've mentioned, but the output hasn't changed at all. This is what the output looks like:

Enter some text (CTRL-D to end):
My name is James!
Myay ay amenay ay isway amesJay! ay!

As you can see, the capitalization still doesn't work right, the program still adds " ay" to the end of words that begin with a consonant. It's exactly the same as it was before I changed the code.

Also, now, when I try to press CTRL-D, it sends the program into an infinite loop rather than ending the program.

So, every one of my original questions still stands. Any help would be greatly appreciated, this project is due first thing tomorrow morning.

Alright! I totally fixed the capitalization problem, and the "ay" problem!

The only thing I have left to do is to repeat "Enter some text (CTRL-D to end): " after my input. I still can't figure out how to do that loop, though.

Any help fixing my loop in int main() would be great.

Try putting this after the eof bit is set :

cin.clear();
  cin.sync();

Try putting this after the eof bit is set :

cin.clear();
  cin.sync();

I just tried entering that code both inside the eof loop, and outside of the eof loop. It didn't do anything.

Any other ideas?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.