C++ Pig Latin Program CIS107C

Posted on 2014-02-08
Last Modified: 2014-02-10
Hello Everyone. I need help with my code. We are two write a code that will translate a sentence into pig latin. So here is the input and what should be the output.

Original Words: Computer Programming is fun to learn!
New Words: omputercay ogrammingpray isway unfay otay earnlay!

What I get is:
New Words: omputercay ogrammingpray isway unfay otay earn!lay

So I tried to come up with a loop that when the last character is ! that it would return constant + ay+ !.

I appreciate all help, but please explain. I don't want to copy work, I want to understand why I am doing what I am supposed to do to correct.

but it is not working. Here is the code:

//Programming Assignment: LAB5A
//Developer:              Hollis R Burks
//Date Written:           2-7-14
//Purpose:               Pig Latin Program

#include <iomanip>
#include <iostream>
#include <cstring>

//Since those are used in ALL function of program, it wont hurt to set it to global
//Else it is considered EVIL to declare global variables
const int maxLine = 100;
char phraseLine [maxLine] = {'\0'};

void pigLatinString ();

using namespace std;

void main ()
	// Program Heading
	cout << "*** You will be prompted to enter a string of  ***\n";
    cout << "*** words. The string will be converted into   ***\n";
    cout << "*** Pig Latin and the results displayed.       ***\n";
    cout << "*** Enter as many strings as you would like.   ***\n";

	//Enter data or Q
	 cout << "\nEnter a group of words or ENTER to quit: ";
    cin.getline(phraseLine, 100, '\n');
	cout <<endl;

	// This is the main loop. Continue executing until the user hits 'enter' to quit.
	while (phraseLine[0] !='\0')

			// Display the word (s) entered by the user
			cout << "Original words: " << phraseLine << endl;

			// Display the word (s) in Pig Latin
			cout << "New Words: ";
			cout << endl;

			//prompt the user for a group of words or press enter to quit
			cout << "\nEnter a group of words or ENTER to quit: ";
			cin.getline(phraseLine, 100, '\n');
void pigLatinString () 
{ //variable declarations
	char tempConsonant [10];
	tempConsonant [0] = '\0';
	int numberOfConsonants = 0;
	char previousCharacter = ' ';
	char currentCharacter = ' ';
	bool isInWord = 0;
	string punct = "";
	for (int i = 0 ; i < maxLine ; i++)

		if ( phraseLine[i] == '\0')
			if (isInWord)
				if ((previousCharacter != ' ')&&(currentCharacter != '!'))
					cout << tempConsonant << "ay"<< endl;
					cout<< tempConsonant<<"ay"<<"!"<<endl;

			return ;

		if (isInWord)
			if (phraseLine [i] == ' ')
				cout << tempConsonant << "ay";
				isInWord = 0;
				tempConsonant [0] = '\0';
				numberOfConsonants = 0;
			cout << phraseLine [i];
			if (phraseLine[i] != ' ')
				char currentCharacter = phraseLine [i];
				currentCharacter = toupper( currentCharacter);

				if ((currentCharacter != 'A') && (currentCharacter != 'E') && 
					(currentCharacter != 'I') && (currentCharacter != 'O') && (currentCharacter != 'U'))
					tempConsonant [numberOfConsonants] = phraseLine [i];
					tempConsonant [numberOfConsonants + 1] = '\0';

					isInWord = 1;
					cout << phraseLine[i];
				cout << phraseLine [i];

		previousCharacter = phraseLine[i];

	return ;}

Open in new window

Question by:hcaburks
LVL 33

Accepted Solution

sarabande earned 500 total points
ID: 39848129
the issue with the '!' is that it either (A) should be handled like a space character, means as a separator for words, or (B) if the '!' should only be considered differently if it was the last character, you would need to ignore it when vocals and consonants of a word were coming to output.

as it is homework, I only can give you tips but not code.

you tried to handle case (B) in an if-block which was called when you find the final zero character. that is too late cause the '!' already was outputted in the 2nd if-block which begins at line 87. the if condition was 'isInWord' what is true cause the word "learn!" has some vocals. so for any further loop circle until end of phrase the if-block was enetered and the cout statement at end of the block outputs the current character including the '!'.

after detecting the final zero the saved consonant "l" was printed and then the "ay". all that leads to the output "earn!lay".

you can avoid that by correcting the first if-statement and if-block. the if condition additionally needs to check  whether the current character is a '!' and the next character is a binary zero character. you would put that condition within parentheses and connect it with || to the current condition. doing so, the final '!' would enter the if block.

here you have to decide whether isInWord is true and if so output the consonants and the "ay" but not the newline character . note, you don't need to handle the space here because in case of a space character before, the isInWord is false.

after that you would output the '!' if any (again no newline).

finally you would output the newline character.


if you are interested to handle case (A) as well, tell me. it would require some more changes what would improve your code and design which currently is not very good.

so, we would use string class instead of fixed char arrays. we would use find function to search for vocals. we would parse the phrase such that we have all word as tokens which much easier could be handled. and we would pass the input string as argument and avoid global variables which are not necessary.

in the moment a lengthy input longer than 99 characters or a word with more than 9 consonants would crash your code because the char arrays would be written beyond boundaries.


Author Closing Comment

ID: 39849119
thank you very much Sara. That is what I need. I know a lot of folks come on here to get their code done for them. That is not what I am looking for. I need to learn and your explanation makes sense. I will go and recode it and see if I can fix it. I learn best from trial an error. Does me no good to some one to say Here is the code. I need to know why and what.

Thanks again.

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
In Easy String Encryption Using CryptoAPI in C++ ( I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: (…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now