3 Replies - 5212 Views - Last Post: 09 July 2010 - 12:51 AM Rate Topic: -----

#1 jrayborn66  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 08-July 10

English to Piglatin Program?

Posted 08 July 2010 - 08:36 PM

I am trying to do this:
You are to write a program that will input English text translate it into Pig Latin, then output the result to a file

Pig Latin is an invented language formed by transforming each word according to the following rules:
1.If the word starts with a vowel ('a', 'e', 'i', 'o', 'u', or 'A, 'E', 'I', 'O', 'U') or does not contain a vowel, its Pig Latin equivalent is formed by just adding the suffix “way”.
2.If the word starts with a consonant (any letter that is not a vowel) and contains a vowel its Pig Latin equivalent is formed by moving the initial consonant string (that is all letters up to the first vowel) from the beginning of the word to the end and then adding the suffix “ay”.
3.If a word is transformed by rule 2) and starts with an upper case character (consonant), its Pig Latin equivalent also starts with an upper case character (vowel) and the consonant is changed to lower case.

A word will be taken to mean any consecutive sequence of letters, i.e. you don't have to check that the sequence is actually an English word. A word is terminated by any non-letter (e.g white space, punctuation mark, etc.).

Made this outline:
int main( )
{
declare el, pl as strings to store an English line, Pig Latin line;

prompt for and get English line (use gets);
while (line is not equal to “!!!”)
{
translate(el, pl);
output Pig Latin line to the screen and to a file;
prompt for and get English line (use getline);
}

system("pause");
return 0;
}

void translate(const char el[ ], char pl[ ])
{
declare ew, pw as c-strings to store an English, Pig Latin word;
declare ep, pp as integer variables that tell the current positions in el, pl;
/*alternatively you can declare ep, pp as pointers of type char *, but this is tricky*/

initialize current positions ep, pp to be the beginning of el, pl, i.e. initialize them to 0;
while (not at end of el)
if (character at current position in el is a letter)
{
extract word from el that starts from current position and store in ew;
translate word ew to Pig Latin and store in pw;
copy pw at current end of pl;
make sure ep, pp are now set to positions just past the word;
}
else /* character at current position in el is not a letter */
{
copy this character unchanged to current position in pl;
increment ep, pp;
}

Null terminate the string pl;
}

This is what I have tried to do but I really suck so....
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
int main( )
{
     char englishline[], piglatinline[]
     cout << "Please enter the text that is going to be translated to piglatin.";
     cin.get(englishline[]);
     while (englishline[] != "!!!")
     {
          translate(englishline[],     piglatinline[]);
cout << piglatinline[] << endl;
fout << piglatinline[] << endl;
cin.getline(englishline[]);
     }
system("pause");
return 0;
}
void translate(const char englishline[], char piglatinline[])
{
char englishword[], piglatinword[];
int englishpostion=englishline[0]
int piglatinpostion=piglatinline[0];
while (!englishline[\0])
{
if (englishline[] == char)
{
 englishword[]=englishline[]-englishline[]
translate(englishword[] piglatinword[]);
strcpy.piglatinword;
englishpostion=englishline[0]-englishposition;
piglatinpostion=piglatinline[0]-piglatinposition;
}
else
{
strcpy.englishword;
englishword=piglatinpostion;
copy character unchanged to current position in pl;
increment ep, pp;
}
Null terminate the string pl;
}


Can someone help walk me through this?

This post has been edited by jrayborn66: 08 July 2010 - 08:48 PM


Is This A Good Question/Topic? 0
  • +

Replies To: English to Piglatin Program?

#2 eker676  Icon User is offline

  • Software Engineer
  • member icon

Reputation: 378
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: English to Piglatin Program?

Posted 08 July 2010 - 08:40 PM

There are many syntax errors within your code. You don't access a character array with the ending brackets []. You just use the name and no brackets.

Fix your array access
http://cplusplus.com...utorial/arrays/

And could you please repost your code without the line numbers.
Was This Post Helpful? 0
  • +
  • -

#3 xtreampb  Icon User is offline

  • D.I.C Regular

Reputation: 14
  • View blog
  • Posts: 337
  • Joined: 20-June 07

Re: English to Piglatin Program?

Posted 09 July 2010 - 12:43 AM

yes your syntax is horrible that that isn't a big deal you can prob fix that on you own (like makes sure each command is terminated properly). When you initialize your character arrays, you don't have a size so your compile doesn't know how much memory to put aside for your array. so when you declare/initialize your array, make sure you give it a size. If you don't know the size give it a decent size that you know will prob not be used like if your just going to input words to the array use like 15-20 for the size and that could be over doing it to some people. in your while loop, i am assuming you are looking for the line terminating character. Well to access what is stored in the array you have to select the address. an array is a series of memory address side by side. array[0] is the first one and it keeps on going from there all the way up to array[x] (where x is the size you designated minus one). that tells the compiler that hey at address array[0] put 'C'. I hope this is helping. in the brackets when accessing elements in an array you always use numbers. you want the position x of the array (array[x]) so instead of "while(!array[/0])" do something more along the lines of:
//you could also have an int datat type to store where the null terminator was located so you can use that position for future reference
int nLocation;
for(int i=0;array[i] != '/0';i++)
{
if(iarray[i]=='/0')
nLocation=i;
//the code you want to do while the element you are accessing is not the null terminator
}



another good way to move elements in an array is to have a seperate place holder something like this

//this is used to hold the first character of the c string
char First=array[0];
//because we got the location of the null terminator earlier, we can use it now
for(int i=0;i<nLocation;i++)
array[i]=array[i+1];

//now we can put the first letter in the back
array[nLocation-1]=First;



there is some logic that i think will help you get through this

~xtreampb~
Was This Post Helpful? 0
  • +
  • -

#4 taylorc8  Icon User is offline

  • B&

Reputation: 149
  • View blog
  • Posts: 1,572
  • Joined: 21-July 09

Re: English to Piglatin Program?

Posted 09 July 2010 - 12:51 AM

The name of your array is implicitly a pointer to it's base, meaning:

it's a pointer to the first element of your array, so the name of it is a pointer, which is probably what cin.getline() will need from you.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1