#Help with C sharp code

1 messages · Page 1 of 1 (latest)

humble seal
#

Hi everyone, I'm fairly new to coding and I'm just beginning my CS degree. I'm trying to figure out why my 'else' statement is not compiling. I'm getting an error CS1525: Unexpected symbol 'else'. It's my first coding class so all this is very new to me lol. It's also online and our professor does not respond to any emails or messages left for him, so I've kinda been learning on my own. Any help is appreciated, thanks. Also I'm not sure how much code I should put, but this is the part that's giving me an error. I can post the code previous to that, it's not much.


if (month > 11 && month < 2)
  extInput = winter;
  extTotal = winter;
  WriteLine("{0} interior murals are scheduled for a total of {2}", 
  intInput, intTotal.ToString("C", CultureInfo.GetCultureInfo("en-US")));
  WriteLine("{0} exterior murals are scheduled for a total of {2}", 
  extInput, extTotal.ToString("C", CultureInfo.GetCultureInfo("en-US")));
  //revenue
  revenue = intTotal + extTotal;
  WriteLine("Total revenue expected is {0}", 
  revenue.ToString("C", CultureInfo.GetCultureInfo("en-US")), 
  extTotal.ToString("C", CultureInfo.GetCultureInfo("en-US")));
 else if (month == 4 || month == 5 || month == 9 || month == 10)
#

Also on the last 'else if' line, I put the 'or' operators in between each statement, it's just not showing up for some reason

willow night
#

In java I would have only put else if (month ==4 || month ==5) then else if(month == 9 || month ==10)

#

the or operator was a discord command but its basically just separating the two

#

or you can try else if((month == 4 II month ==5) II (month == 9 II month ==10))

humble seal
rocky orbit
#

;compile

using System;
                    
public class Program
{
    public static void Main()
    {
        if (false)
          Console.WriteLine("First");
          Console.WriteLine("Second"); // always executed

        if (false) 
        {
          Console.WriteLine("Third");
          Console.WriteLine("Fourth");
        }
    }
}
warm craterBOT
#
Compiler Output
prog.cs(13,11): warning CS0162: Unreachable code detected
Compilation succeeded - 2 warning(s)

Program Output
Second

rocky orbit
#

You can omit the brackets only if the body is composed of one statements

#

Hence why in the first if the Console.WriteLine("First"); isn't executed but the Console.WriteLine("Second"); is

If you do something like this, it will executed but it will yield unexpected results

#
if (month > 11 && month < 2)
  extInput = winter; // no executed if month is greater than 11 and month is lower than 2
  extTotal = winter; // Always executed 
  WriteLine("{0} interior murals are scheduled for a total of {2}", intInput, intTotal.ToString("C", CultureInfo.GetCultureInfo("en-US"))); // always executed

Your code correctly formatted and fixed (at least syntactically speaking)

using System;
                    
public class Program
{
    public static void Main()
    {
        // the beginning of your code
        
        if (month > 11 && month < 2) 
        {
          extInput = winter;
          extTotal = winter;
            
          Console.WriteLine("{0} interior murals are scheduled for a total of {2}", intInput, intTotal.ToString("C", CultureInfo.GetCultureInfo("en-US")));
          Console.WriteLine("{0} exterior murals are scheduled for a total of {2}", extInput, extTotal.ToString("C", CultureInfo.GetCultureInfo("en-US")));
          
            //revenue
          revenue = intTotal + extTotal;
          Console.WriteLine("Total revenue expected is {0}", 
                    revenue.ToString("C", CultureInfo.GetCultureInfo("en-US")), 
                    extTotal.ToString("C", CultureInfo.GetCultureInfo("en-US")));
        } 
        else if (month == 4 || month == 5 || month == 9 || month == 10) 
        {
            // this is empty in the code you sent
        }
        
        // rest of your code (if needed)
}

PS. In the future, put as much code as possible. As long as it fits in a discord message, then you might want to switch to one of the solution enumerated in the guidelines. Also, format you code like shown bellow.

```csharp
your code here
```

humble seal