Coding Help

Hey Guy’s I am new to coding in cCsharp and i have run into a slight problem with if statements. any help would be greatly appreciated.
Thanks
p.s I know that the coding is bad but i have just started today.
p.p.s there is no hashtag button on the mac so i just say sharp :slight_smile:

The hashtag is Option/Alt-3 on OSX.

It’ll probably be much clearer if you copied the code into the forums using the Code tags, too.

using System;

namespace IFStatement
{
class MainClass
{
public static void Main (string[ ] args)
{
begin:

Console.ForegroundColor = ConsoleColor.Yellow;

Console.WriteLine(“PickANumberBetween1and5”);

int UserNumber = int.Parse(Console.ReadLine ());

if (UserNumber == 1)
{
Console.WriteLine (“WhatsYourName?”);
Console.ReadLine ();
Console.WriteLine (“\nThatsANiceName”);
}

else if (UserNumber == 2)
{
Console.WriteLine (“HowOldAreYou?”);
}

int age = Convert.ToInt32(Console.ReadLine ());

if (age < 20)
{
Console.WriteLine (“WhatIsYourDreamJob?”);
Console.ReadLine ();
Console.WriteLine (“\nOhCoolIHaveAllwaysWantedToDoThat!”);
}

else
{
Console.WriteLine (“\nDoYouAlreadyHaveAJob,1=YesOr0=No.”);
}
int answer = int.Parse(Console.ReadLine ());
if (answer == 1)
{
Console.WriteLine(“SoWhatDoYouDo?”);
Console.ReadLine();
Console.WriteLine(“ThatSoundsFun!”);
}
else
{
Console.WriteLine(“YouShouldProbablyGetaJob:/”);
}

else if (UserNumber == 3) // the word in red is the unexpected symbol
{
Console.WriteLine (“DoYouLikeCoding?”);
Console.ReadLine();
Console.WriteLine (“Awesome!”);
}

else
{
Console.WriteLine(“WhatsYourFavouriteColour?”);
Console.ReadLine();
Console.WriteLine (“\nThatsMyFavouriteColourToo!”);
}

Console.WriteLine (“\nDoYouWishToContinue?1=YesOr0=No.”);

if (answer == 1)
{
goto begin;}
else
{
;
}

}
}
}

You have an

if
else
else if

going on there.

It always has to go in this order:

if
else if (any number of else ifs)
else last.

So Your saying i can’t have an IF statement inside an IF statement?

No we are saying you can’t have a ELSE IF after a ELSE

since else would run first and never give the else if a chance to be tested and run.

It looks like you’re trying to use nested conditionals. I think the closing brace after you ask for the age shouldn’t be there, as most of what follows depends on the answer to that. In this case you can have else if after a else, if the else is inside the previous else if. Be a bit more careful with indenting your code blocks and it will be easier to spot the problem. I’d quote line numbers but they’re a bit hard to work out from your code. (About line 28). That closing brace should be on the line before where the error occurs.

using System;

namespace IFStatement
{
    class MainClass
    {
        public static void Main (string[] args)
        {
            begin:
           
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine("PickANumberBetween1and5");

            int UserNumber = int.Parse(Console.ReadLine ());

            if (UserNumber == 1)
            {
                Console.WriteLine ("WhatsYourName?");
                Console.ReadLine ();
                Console.WriteLine ("\nThatsANiceName");
            }

            else if (UserNumber == 2)
            {
                Console.WriteLine ("HowOldAreYou?");
                int age = Convert.ToInt32(Console.ReadLine ());

                if (age < 20)
                {
                    Console.WriteLine ("WhatIsYourDreamJob?");
                    Console.ReadLine ();
                    Console.WriteLine ("\nOhCoolIHaveAllwaysWantedToDoThat!");
                }
                else
                {
                    Console.WriteLine ("\nDoYouAlreadyHaveAJob,1=YesOr0=No.");
   
                    int answer = int.Parse(Console.ReadLine ());
                    if (answer == 1)
                    {
                        Console.WriteLine("SoWhatDoYouDo?");
                        Console.ReadLine();
                        Console.WriteLine("ThatSoundsFun!");
                    }
                    else
                    {
                        Console.WriteLine("YouShouldProbablyGetaJob:/");
                    }
                }
            }

            else if (UserNumber == 3) // the word in red is the unexpected symbol
            {
                Console.WriteLine ("DoYouLikeCoding?");
                Console.ReadLine();
                Console.WriteLine ("Awesome!");
            }

            else
            {
                Console.WriteLine("WhatsYourFavouriteColour?");
                Console.ReadLine();
                Console.WriteLine ("\nThatsMyFavouriteColourToo!");
            }

            Console.WriteLine ("\nDoYouWishToContinue?1=YesOr0=No.");

            if (answer == 1)
            {
                goto begin;}
            else
            {
            ;
            }
        }
    }
}

Thanks