Solved

C++ Pig Latin Program CIS107C

Posted on 2014-02-08
2
1,045 Views
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: ";
				pigLatinString();
			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');
	}	
	return;
}
	
	
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;
				else
					cout<< tempConsonant<<"ay"<<"!"<<endl;
			}
			

			return ;
		}

		
		if (isInWord)
		{
			if (phraseLine [i] == ' ')
			{
				
				cout << tempConsonant << "ay";
				isInWord = 0;
				tempConsonant [0] = '\0';
				numberOfConsonants = 0;
			}
			cout << phraseLine [i];
		
		}
		else
		{
			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';
					numberOfConsonants++;
				}

				else
				{
					isInWord = 1;
					cout << phraseLine[i];
				}
			}
			else
			{
				cout << phraseLine [i];
			}
		}

		previousCharacter = phraseLine[i];
	}

	return ;}

Open in new window

0
Comment
Question by:hcaburks
2 Comments
 
LVL 32

Accepted Solution

by:
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.

Sara
0
 

Author Closing Comment

by:hcaburks
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.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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++ (http://www.experts-exchange.com/viewArticle.jsp?aid=1193) I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

746 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

13 Experts available now in Live!

Get 1:1 Help Now