8 Replies - 1943 Views - Last Post: 25 September 2016 - 11:23 AM Rate Topic: -----

#1 Swfan42   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 25-September 16

Pig Latin Translator

Posted 25 September 2016 - 10:05 AM

Hello! So I'm new to C# Programming and for a school assignment I need to create an English to Pig Latin Translator. The statement that needs to be translated is the following:

Hello I am a computer

I need to do this as a Console Application through Visual Studio and I have my program working however, it only seems to translate one word at a time (a screenshot is provided). Note that every time I enter the statement, "Hello I am a computer" it translates the next word in the sentence. I would like to know how I can have the entire sentence translated on a single line without entering the sentence multiple times. I will post my code below. Thanks in advance for your help :)/>/>/>/>Attached Image


namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Please enter what you want translated:");
            string english = Console.ReadLine();
            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";
                } 
                Console.WriteLine(pigLatin);
                Console.ReadLine();
            }
        }
    }
}
:code:

This post has been edited by modi123_1: 25 September 2016 - 10:19 AM
Reason for edit:: Please use the '[code]' tag button in the editor.


Is This A Good Question/Topic? 0
  • +

Replies To: Pig Latin Translator

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15307
  • View blog
  • Posts: 61,387
  • Joined: 12-June 08

Re: Pig Latin Translator

Posted 25 September 2016 - 10:20 AM

I am confused - why do you have a 'readline' in line 28?
Was This Post Helpful? 0
  • +
  • -

#3 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Pig Latin Translator

Posted 25 September 2016 - 10:22 AM

You did notice that lines 27 and 28 are *INSIDE* your loop that translates the sentences... Right?

Basic debugging and walking through the code line by line would have shown you this. I'm going to assume your class teacher does what most do and expect you to code - but (s)he hasn't bothered to first show you how to debug your programs in Visual Studio. I don't really know how they get their credentials with this thinking. Please see the debugging tutorials listed below: They will put you head and shoulders above your competition in class.

See FAQ # 2 "How do I debug my program?"

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


FAQ (Frequently Asked Questions - Updated July 2016
Spoiler

Was This Post Helpful? 0
  • +
  • -

#4 Swfan42   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 25-September 16

Re: Pig Latin Translator

Posted 25 September 2016 - 10:27 AM

View Postmodi123_1, on 25 September 2016 - 10:20 AM, said:

I am confused - why do you have a 'readline' in line 28?


I threw that in there because my program would instantly crash without it. If I take it out and add a bunch of spaces after entering the sentence, all the words appear but they are on separate lines (the program then crashes after about 10 seconds). Like I said, I am brand new to C#. I've only been doing programming for about a year but my previous teacher had no idea what she was doing so bare with me if I have troubling understanding stuff. xD

elloHay
Iway
amway
away
omputercay
Was This Post Helpful? 0
  • +
  • -

#5 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15307
  • View blog
  • Posts: 61,387
  • Joined: 12-June 08

Re: Pig Latin Translator

Posted 25 September 2016 - 10:28 AM

Have you looked at the alternative to ' Console.WriteLine'? Maybe ' Console.Write'
https://msdn.microso...(v=vs.110).aspx
Was This Post Helpful? 2
  • +
  • -

#6 Swfan42   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 25-September 16

Re: Pig Latin Translator

Posted 25 September 2016 - 10:38 AM

View Postmodi123_1, on 25 September 2016 - 10:28 AM, said:

Have you looked at the alternative to ' Console.WriteLine'? Maybe ' Console.Write'
https://msdn.microso...(v=vs.110).aspx


Ok so I used Console.Write instead of Console.WriteLine and all of the words now appear on one line but there is no spacing between them.

Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#7 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15307
  • View blog
  • Posts: 61,387
  • Joined: 12-June 08

Re: Pig Latin Translator

Posted 25 September 2016 - 10:48 AM

.. so append a space to the value in variable 'pigLatin'.
Was This Post Helpful? 0
  • +
  • -

#8 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Pig Latin Translator

Posted 25 September 2016 - 10:59 AM

> I threw that in there because my program would instantly crash without it.
No. It wasn't crashing. It was just completing, then closing. That's the nature of a console applications; It does its job, then closes. Did your text book or teach not cover that?
If you move the ReadLine to OUTSIDE the loop then the loop will complete THEN it will wait for you to hit the [Enter] key.

> Like I said, I am brand new to C#. I've only been doing programming for about a year but my previous teacher had no idea what she was doing
I suspect your current teacher isn't doing you any favors either.
If you've been at this for a full year... My gawd... There are so many books out there along the lines of "Learn C# in 30 days". If you pick one up and WORK it COVER TO COVER, don't just glance at it like a dictionary only when you get stuck... You could be learning a whole lot more in a lot less time.
Was This Post Helpful? 1
  • +
  • -

#9 Swfan42   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 25-September 16

Re: Pig Latin Translator

Posted 25 September 2016 - 11:23 AM

View Postmodi123_1, on 25 September 2016 - 10:48 AM, said:

.. so append a space to the value in variable 'pigLatin'.


I just realised I needed to use "ay " and "way " instead of "ay" and "way" to add the spacing. *facepalm* Thank you for the assistance :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1