5 Replies - 645 Views - Last Post: 23 March 2013 - 12:48 PM Rate Topic: -----

#1 Ironhead42  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 23-March 13

Problem with foreach loop

Posted 23 March 2013 - 10:35 AM

I am writing a piglatin translator program. It is supposed to check for several things in order to do the translation. The basic translation code works, however, when I add an additional if statement, only one part of the code or the other works instead of both. For example, it will either translate based on the letterPosition or hasNumber based on which one is listed first. I would appreciate it if someone could tell me what I need to do in order to get both to function. Thanks.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Pig_Latin_Translator
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnTranslate_Click(object sender, EventArgs e)
        {
            string englishText = txtEnglishText.Text;
            string[] words = englishText.Split(' ');
            string vowels = "AEIOUaeiou";
            string firstLetter = "";
            string restOfWord = "";
            string pigLatinOut = "";
            string wholeWord = "";
            int letterPosition;
            string numbers = "1234567890";
            int hasNumber;

            foreach (string word in words)
            {
                firstLetter = word.Substring(0, 1);
                restOfWord = word.Substring(1, word.Length - 1);
                wholeWord = word.ToString();

                letterPosition = vowels.IndexOf(firstLetter);
                hasNumber = numbers.IndexOf(word);

                if (hasNumber == -1)
                {
                    pigLatinOut = wholeWord + " ";
                }

                else if (letterPosition == -1)
                {
                    // it's a consonant
                    pigLatinOut = restOfWord + firstLetter + "ay" + " ";
                }
                else
                {
                    // it's a vowel
                    pigLatinOut = word + "way" + " ";
                } // end if

                
                txtTranslation.Text += pigLatinOut;
            } // end foreach

        }


        private void btnClear_Click(object sender, EventArgs e)
        {
            txtEnglishText.Text = "";
            txtTranslation.Text = "";
            txtEnglishText.Focus();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Problem with foreach loop

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5535
  • View blog
  • Posts: 11,857
  • Joined: 02-June 10

Re: Problem with foreach loop

Posted 23 March 2013 - 11:13 AM

Your description is hard to follow... One part or the other... Huh?

Stop and read it and think it through..

Line 42.. If that is true then the block of 43-45 will run.
elseif line 47 will only run 48-51 if that is true
else 52 will only run 53-56 if the check on line 47 is false

Now picture this
Line 42 is true
Therefore line 47 is never run
Therefore line 52 is never seen because the check at line 47 is never run


Given this is course homework we can't give you working code to fix it. All we can do is steer you towards how to find the problem so you can understand how to fix it yourself.



tlhIn`toq's FAQ list

Learning to debug one's own code is an essential skill. Sadly, one that apparently few college courses teach. Silly if you ask me.

Placing breakpoints and walking through the code line by line allows you to actually WATCH it execute.

Visualizing what your code does will let you see why it behaves the way it does.

It would be well worth your time to do the tutorials on FAQ 2. A couple hours learning this skill will save you hundreds of hours of confusion in one project alone.


TOP most asked:
What does this error message mean?
FAQ 2: How do I debug
FAQ 3: How do I make Class1/Form1 talk to Class2/Form2


FAQ (Frequently Asked Questions - Updated Feb 2013
Spoiler



This post has been edited by tlhIn`toq: 23 March 2013 - 11:15 AM

Was This Post Helpful? 1
  • +
  • -

#3 Ironhead42  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 23-March 13

Re: Problem with foreach loop

Posted 23 March 2013 - 12:10 PM

"Line 42.. If that is true then the block of 43-45 will run.
elseif line 47 will only run 48-51 if that is true
else 52 will only run 53-56 if the check on line 47 is false"

This is exactly how I want it to work. Unfortunately, my code isn't doing exactly this. If line 42 is true then it doesn't run the else if or else statement exactly like I want. What isn't working is when line 42 is false. If line 42 is false, then the else if statement on line 47 should run, but it does not. The program is executing line 42 regardless of it being true or false. I have not been able to deduce why it does not move on and execute line 47 if line 42 is false. I do not want the code, I just want to understand why it is doing this so that I can correct the code. Thank you for your help.
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6066
  • View blog
  • Posts: 23,526
  • Joined: 23-August 08

Re: Problem with foreach loop

Posted 23 March 2013 - 12:41 PM

See the spoiler in our Klingon friend's signature? Click on it and read all about debugging. It will help you immensely not only in this program, but going forward.
Was This Post Helpful? 0
  • +
  • -

#5 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5535
  • View blog
  • Posts: 11,857
  • Joined: 02-June 10

Re: Problem with foreach loop

Posted 23 March 2013 - 12:42 PM

Quote

The program is executing line 42 regardless of it being true or false.

Line 42 is always going to exectute. The question is does it result in true or false mean line 44 is then executed or not.


Remember what I said about this being basic debugging and I gave you links about how to debug?
Let me ask you this... When is hasNumber ever NOT -1? What is an example of text that would make 42 if (hasNumber == -1) be false? Because in my testing it is always -1.
Was This Post Helpful? 1
  • +
  • -

#6 Ironhead42  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 23-March 13

Re: Problem with foreach loop

Posted 23 March 2013 - 12:48 PM

My apologies, I missed the spoiler link. I was actually looking for the FAQ 2. Now I will go read through that. Also, thanks for the question about hasNumber not being -1, for whatever reason my brain wasn't even processing that issue.I truly appreciate the help.And you are right, I agree that we should be taught debugging techniques much more thoroughly.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1