English to Pig Latin translator
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: English to Pig Latin translator

Hybrid View

  1. #1
    Join Date
    Feb 2013
    Posts
    23

    English to Pig Latin translator

    Working on an English to Pig Latin translator. When I click Translate I am getting:

    System.Windows.Forms.TextBox.Text: System.Windows.Forms.TextBox.Text:Syst..using System;

    Here's the code I have so far, any help narrowing this down would be greatly appreciated.

    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace CSA_164_Midterm
    {
        public partial class frmTranslator : Form
        {
            public frmTranslator()
            {
                InitializeComponent();
            }
    
            private void btnTranslation_Click(object sender, EventArgs e)
            {
                string english = Convert.ToString(txtInput.Text);
                string pigLatin = "";
                string firstLetter;
                string restOfWord;
                string vowels = "AEIOUaeiou";
                int letterPos;
    
                foreach (string word in english.Split())
                {
                    firstLetter = word.Substring(0,1);
                    restOfWord = word.Substring(1,word.Length - 1);
                    letterPos = vowels.IndexOf(firstLetter);
                    if (letterPos == -1)
                    {
                        pigLatin = restOfWord + firstLetter + "ay";
                    }
                    else
                    {
                        pigLatin = word + "way";
                    }
                    txtTranslation.Text = txtTranslation.ToString();
                    txtInput.Focus();
                }
                
            }
    
            private void btnClear_Click(object sender, EventArgs e)
            {
                txtInput.Text = "";
                txtTranslation.Text = "";
            }
    
            private void btnExit_Click(object sender, EventArgs e)
            {
                this.Close();
            }
        }
    }

  2. #2
    Join Date
    Feb 2013
    Posts
    23

    Re: English to Pig Latin translator

    Ok, so I have gotten further in this, but now it is just translating the last word I enter in the English box. Any general ideas of why this isn't working? I'm guessing I am not loading the translated words correctly. Thanks for looking.

    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace CSA_164_Midterm
    {
        public partial class frmTranslator : Form
        {
            public frmTranslator()
            {
                InitializeComponent();
            }
    
            private void btnTranslation_Click(object sender, EventArgs e)
            {
                string english = Convert.ToString(txtInput.Text);
                string pigLatin = "";
                string firstLetter;
                string restOfWord;
                string vowels = "AEIOUaeiou";
                int letterPos;
    
                foreach (string word in english.Split())
                {
                    firstLetter = word.Substring(0,1);
                    restOfWord = word.Substring(1,word.Length - 1);
                    letterPos = vowels.IndexOf(firstLetter);
                    if (letterPos == -1)
                    {
                        pigLatin = restOfWord + firstLetter + "ay";
                    }
                    else
                    {
                        pigLatin = word + "way";
                    }
                    txtTranslation.Text = pigLatin;
                    txtInput.Focus();
                }
            }
            private void btnClear_Click(object sender, EventArgs e)
            {
                txtInput.Text = "";
                txtTranslation.Text = "";
            }
    
            private void btnExit_Click(object sender, EventArgs e)
            {
                this.Close();
            }
        }
    }

  3. #3
    Join Date
    May 2002
    Location
    Boston
    Posts
    66

    Cool Re: English to Pig Latin translator

    try this

    Code:
    if (letterPos == -1)
                    {
                        pigLatin = pigLatin + " " + restOfWord + firstLetter + "ay";
                    }
                    else
                    {
                        pigLatin = pigLatin + " " + word + "way";
                    }

    Curt

  4. #4
    Join Date
    Jun 2011
    Location
    .NET4.0 / VS 2010
    Posts
    71

    Re: English to Pig Latin translator

    If I am looking at the code correctly then the issue is within your foreach loop.
    Code:
                // The loop extracts each word from the text box to translate it into pig latin
                foreach (string word in english.Split())
                {
                    firstLetter = word.Substring(0,1);
                    restOfWord = word.Substring(1,word.Length - 1);
                    letterPos = vowels.IndexOf(firstLetter);
                    // Check for condition and set pigLatin variable to correct phrase
                    if (letterPos == -1)
                    {
                        pigLatin = restOfWord + firstLetter + "ay";
                    }
                    else
                    {
                        pigLatin = word + "way";
                    }
                    // Replaces text in txtTranslation with word stored in variable pigLatin
                    txtTranslation.Text = pigLatin;
                    txtInput.Focus();
                }
    If you look at
    Code:
    txtTranslation.Text = pigLatin;
    You can see that it sets the translated word into the text box. The issue is not that it is only translating the last word but it replaces the previous word. If you want the whole sentence you have to append the new text into the text box.

    I may be wrong since I just took a brief overlook at the code. If it still causing an issue I'll look it over when I have a bit more time on my hands.
    Last edited by Ubiquitous; March 26th, 2013 at 12:35 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center