Search Unity

Coding Help

Discussion in 'Scripting' started by Harryr20, Dec 2, 2014.

  1. Harryr20

    Harryr20

    Joined:
    Dec 2, 2014
    Posts:
    5
    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 :)
    Screen Shot 2014-12-02 at 21.43.59.png
     
  2. GetUpKidAK

    GetUpKidAK

    Joined:
    Apr 9, 2013
    Posts:
    84
    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.
     
  3. Harryr20

    Harryr20

    Joined:
    Dec 2, 2014
    Posts:
    5
    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
    {
    ;
    }

    }
    }
    }
     
  4. JasonBricco

    JasonBricco

    Joined:
    Jul 15, 2013
    Posts:
    936
    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.
     
  5. Harryr20

    Harryr20

    Joined:
    Dec 2, 2014
    Posts:
    5
    So Your saying i can't have an IF statement inside an IF statement?
     
  6. cmcpasserby

    cmcpasserby

    Joined:
    Jul 18, 2014
    Posts:
    315
    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.
     
  7. christinanorwood

    christinanorwood

    Joined:
    Aug 9, 2013
    Posts:
    402
    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.
     
    Last edited: Dec 3, 2014
  8. christinanorwood

    christinanorwood

    Joined:
    Aug 9, 2013
    Posts:
    402
    Code (csharp):
    1.  
    2. using System;
    3.  
    4. namespace IFStatement
    5. {
    6.     class MainClass
    7.     {
    8.         public static void Main (string[] args)
    9.         {
    10.             begin:
    11.            
    12.             Console.ForegroundColor = ConsoleColor.Yellow;
    13.             Console.WriteLine("PickANumberBetween1and5");
    14.  
    15.             int UserNumber = int.Parse(Console.ReadLine ());
    16.  
    17.             if (UserNumber == 1)
    18.             {
    19.                 Console.WriteLine ("WhatsYourName?");
    20.                 Console.ReadLine ();
    21.                 Console.WriteLine ("\nThatsANiceName");
    22.             }
    23.  
    24.             else if (UserNumber == 2)
    25.             {
    26.                 Console.WriteLine ("HowOldAreYou?");
    27.                 int age = Convert.ToInt32(Console.ReadLine ());
    28.  
    29.                 if (age < 20)
    30.                 {
    31.                     Console.WriteLine ("WhatIsYourDreamJob?");
    32.                     Console.ReadLine ();
    33.                     Console.WriteLine ("\nOhCoolIHaveAllwaysWantedToDoThat!");
    34.                 }
    35.                 else
    36.                 {
    37.                     Console.WriteLine ("\nDoYouAlreadyHaveAJob,1=YesOr0=No.");
    38.    
    39.                     int answer = int.Parse(Console.ReadLine ());
    40.                     if (answer == 1)
    41.                     {
    42.                         Console.WriteLine("SoWhatDoYouDo?");
    43.                         Console.ReadLine();
    44.                         Console.WriteLine("ThatSoundsFun!");
    45.                     }
    46.                     else
    47.                     {
    48.                         Console.WriteLine("YouShouldProbablyGetaJob:/");
    49.                     }
    50.                 }
    51.             }
    52.  
    53.             else if (UserNumber == 3) // the word in red is the unexpected symbol
    54.             {
    55.                 Console.WriteLine ("DoYouLikeCoding?");
    56.                 Console.ReadLine();
    57.                 Console.WriteLine ("Awesome!");
    58.             }
    59.  
    60.             else
    61.             {
    62.                 Console.WriteLine("WhatsYourFavouriteColour?");
    63.                 Console.ReadLine();
    64.                 Console.WriteLine ("\nThatsMyFavouriteColourToo!");
    65.             }
    66.  
    67.             Console.WriteLine ("\nDoYouWishToContinue?1=YesOr0=No.");
    68.  
    69.             if (answer == 1)
    70.             {
    71.                 goto begin;}
    72.             else
    73.             {
    74.             ;
    75.             }
    76.         }
    77.     }
    78. }
    79.  
    80.  
    81.  
     
  9. Harryr20

    Harryr20

    Joined:
    Dec 2, 2014
    Posts:
    5
    Thanks
     
unityunity