error

  • (2 Pages)
  • +
  • 1
  • 2

21 Replies - 1185 Views - Last Post: 01 October 2012 - 10:28 AM Rate Topic: -----

#1 magedo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 23-September 12

error

Posted 23 September 2012 - 08:38 AM

I am working on this code and I have the error that says invalid expression term 'else'
and another one that says only assignment, call, increment, decrement, await and new object expression can be used as statement

Can someone provide me with directions or instructions on how to work on with these errors please?

Thanks

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace pigLatin
{

    class PigLatin
    {

        static void Main(string[] args)
        {
            string english = Convert.ToString("txtEnglsih.Text");
            string Vowels = "AEIOUaeiou";
            int WordLenght;
            string firstLetter;
            string restOftheWord;
            string piglatin;
            

            Console.WriteLine("");
            foreach (char word in english)
            {
               
                english = firstLetter.Remove(word);
                restOftheWord = firstLetter + "ay";
                //ifconsent 
                
                {
                    {
                        piglatin = restOftheWord + firstLetter + "AY";
                    }
                        else 
                    {
                        piglatin = word + "AY";


                        Console.ReadLine();


                    }
                }
        }
    }
    }
    }
}
    


Is This A Good Question/Topic? 0
  • +

Replies To: error

#2 ThrowsException  Icon User is offline

  • D.I.C Head

Reputation: 33
  • View blog
  • Posts: 83
  • Joined: 21-February 12

Re: error

Posted 23 September 2012 - 08:55 AM

How much c# experience, or programming experience, do you have? Have you coded in other languages?
Was This Post Helpful? 0
  • +
  • -

#3 tlhIn`toq  Icon User is offline

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

Reputation: 5436
  • View blog
  • Posts: 11,656
  • Joined: 02-June 10

Re: error

Posted 23 September 2012 - 10:01 AM

Notice line 29... See that badly formed "if"... That you commented out

in order to have an 'else' you must have an 'if'


if (this condition
{
    // Do this block
}
else
{
    // DO this other block
}


ThrowsException Is probably very right in asking how much (or little in this case) experience you have. I suspect you aren't familiar with any of these C# commands. You should either re-read the course textbook chapters you skipped over - or buy a "Learn C# in 30 days" type book and just work it cover-to-cover before you try to write a program from scratch.

If you can't speak the langague you can't write a book. You have to learn "See Spot chase Dick and Jane" before you can write a mystery novel.

This post has been edited by tlhIn`toq: 23 September 2012 - 10:03 AM

Was This Post Helpful? 0
  • +
  • -

#4 magedo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 23-September 12

Re: error

Posted 24 September 2012 - 08:19 AM

I dont have that much experience in C# and yes I did coded in other languages.

Thanks for help :)
Was This Post Helpful? 0
  • +
  • -

#5 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3467
  • View blog
  • Posts: 10,687
  • Joined: 05-May 12

Re: error

Posted 24 September 2012 - 08:58 AM

As far as I know, most other programming languages that have an "else" key word need to have it in the context of an if statement. Why were you expecting C# to not require the "if" ?
Was This Post Helpful? 0
  • +
  • -

#6 magedo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 23-September 12

Re: error

Posted 24 September 2012 - 09:06 AM

I have been coding for sometime then I went for grade school to different major and somehow forgot most of the concept in programming languages and now trying to remember by programming again....I totally agree with you, I mean it looks very silly not to notice such a basic mistake....
Was This Post Helpful? 0
  • +
  • -

#7 magedo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 23-September 12

Re: error

Posted 24 September 2012 - 09:12 AM

Ok, I have fixed all the possible errors I could, there is one more that is bugging me, the one in the line 39 that says " The name word does not exist in the current context...any help or direction would be really appreciate it

Thanks
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace pigLatin
{

    class PigLatin
    {

        static void Main(string[] args)
        {
            string english = Convert.ToString("txtEnglsih.Text");
            string Vowels = "AEIOUaeiou";
            string firstLetter;
            string restOftheWord;
            string piglatin;
            
            

            Console.WriteLine("");
            foreach (char word in english)
            
               
                english = firstLetter.Remove(word);
                restOftheWord = firstLetter + "ay";
                //ifconsent 
            
                
                    if (firstLetter != Vowels)
                    {
                     piglatin = restOftheWord + firstLetter + "AY";
                   
                    }
                    else
                    {
                    piglatin = word + "AY";
                
                    }
                  Console.ReadLine();

                        }

        
    }

        }

Was This Post Helpful? 0
  • +
  • -

#8 Gleave  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 46
  • Joined: 06-December 10

Re: error

Posted 24 September 2012 - 09:35 AM

First of all you don't ever assign a value to FirstLetter. So when you try to assign english with firstletter.remove(word) it does nothing. I am not even getting the error with word that you describe because its hung up on the assignment call.
Was This Post Helpful? 0
  • +
  • -

#9 magedo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 23-September 12

Re: error

Posted 24 September 2012 - 09:42 AM

In the line 39 the error says " The name "word" does not exist in the current context "
Thank you very much for reply
Was This Post Helpful? 0
  • +
  • -

#10 Gleave  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 46
  • Joined: 06-December 10

Re: error

Posted 24 September 2012 - 09:50 AM

Well, if your not getting the first error, then what I would try is to create a variable AFTER you start the foreach loop.
SO if you have Foreach(char word in english) inside that loop I would make another char value and assign the value of word to it. Then use that variable to remove the char.
foreach(char word in english)
{
char c = word;
english = firstletter.remove(c);
}



This post has been edited by Gleave: 24 September 2012 - 09:51 AM

Was This Post Helpful? 0
  • +
  • -

#11 magedo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 23-September 12

Re: error

Posted 24 September 2012 - 10:06 AM

Tried that one and yet I am still getting the same error
I am really grateful of your time and the effort
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace pigLatin
{

    class PigLatin
    {

        static void Main(string[] args)
        {
            string english = Convert.ToString("txtEnglsih.Text");
            string Vowels = "AEIOUaeiou";
            string firstLetter;
            string restOftheWord;
            string piglatin;
            
            
            

            Console.WriteLine("");
            foreach (char word in english)
            {
                char c = word;
                english = firstLetter.Remove(c);
            }
                restOftheWord = firstLetter + "ay";
                //ifconsent 

            
                    if (firstLetter != Vowels)
                    {
                     piglatin = restOftheWord + firstLetter + "AY";
                   
                    }
                    else
                    {
                    piglatin = word + "AY";
                
                    }
                  Console.ReadLine();

                        }

        
    }

        }
    
    


Was This Post Helpful? 0
  • +
  • -

#12 ThrowsException  Icon User is offline

  • D.I.C Head

Reputation: 33
  • View blog
  • Posts: 83
  • Joined: 21-February 12

Re: error

Posted 24 September 2012 - 12:01 PM

Back to what tlhintoq was saying. I think your best bet is to grab a beginners book for C# or some kind of tutorial or reference material because you are making a lot of mistakes that you are going to continue to make.

- You are misusing brackets and putting variables out of scope.
- You are changing iterators in the middle of loops

This isn't a knock on your skills. You'll just find yourself getting this stuff a lot easier I think if you run through an exercise book and understand the basic constructs of the language that you will really need to have a firm grip on before you can start asking meaningful questions and understand the feedback you are given here on the forums.

Do what tlhintoq said. You need to understand the language before you write the novel.

This post has been edited by ThrowsException: 24 September 2012 - 12:02 PM

Was This Post Helpful? 0
  • +
  • -

#13 magius96  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 71
  • View blog
  • Posts: 764
  • Joined: 15-April 09

Re: error

Posted 24 September 2012 - 01:48 PM

string english = Convert.ToString("txtEnglsih.Text");

I assume that your attempting to access the value of a textbox with that line, if that's the case then you don't need the quotations around the name of the control. If on the other hand, that is a reference to a text file, then you'll need to actually open the file and read it into a variable. The fact that you're making calls to the console object leads me to believe that you are not creating a form project and should instead be loading a text file for parsing. You'll want to look up a tutorial on reading from a text file.

I also noticed that your parsing through the input one character at a time, that's fine if your only getting one word input, but won't give you the desired results if your receiving sentences and such. Instead you should split the input on the space, and work with each separate word. Then, use substring to get the parts of the word that you need to work with instead of parsing the words one character at a time.

When you are checking if the first character is a vowel, what your really doing is seeing if the full value of firstLetter is equal to the full value of Vowel, that will not give you the desired results. What you should be doing is seeing if the string Vowel contains the value of firstLetter.
Was This Post Helpful? 0
  • +
  • -

#14 magedo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 23-September 12

Re: error

Posted 24 September 2012 - 07:16 PM

Thanks guys for the suggestion, I found this online link for learning though http://www.homeandle...arp/csharp.html I think it's going to help me with my basic...do you recommend the link? and can you point out my weakness where I should be improving ?


At thanks for reply, I tried to use Subtraction but it shows an error
Was This Post Helpful? 0
  • +
  • -

#15 ThrowsException  Icon User is offline

  • D.I.C Head

Reputation: 33
  • View blog
  • Posts: 83
  • Joined: 21-February 12

Re: error

Posted 25 September 2012 - 05:47 AM

Check out these from direct from the Microsoft Visual C# Resources. In particular I'd look at the C# Fundamentals: Development for Absolute Beginners (2010) section. Theres even two videos in there that are basically everything you are trying to do. Video 11 is reading data from a text file and Video 12 working with strings but I'd suggest you watch the other videos as well about branching statements, variable scopes and function scopes (Videos 5, 6 and 7).

After watching these videos you'll have a much better grasp of whats going on in the code you are trying to write.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2