18166
18166

Reputation: 111

C# Xamarin Wrong If clause being used?

In my C# code (I am currently trying to learn it), I am trying to make a text - console based adventure game. The first 'Section' works, and I copy pasted it over to and changed the name of the variables etc. (Its a repetitive code), but it just wont work.

Here is the code:

using System;

namespace LearningC
{
    class MainClass
    {
        public static void Main (string[] args)
        {
            lvl_1_start:                                                                                                                            //LEVEL ONE
            Console.WriteLine ("Welcome to AlexAdventureLand.\nPlease press the enter key to start.");
            Console.ReadKey ();
            Console.WriteLine ("You are a rabbit in a burrow. There is a badger outside. \nPress 1 to leave the burrow. \nPress 2 to wait.");
            int lvl_1 = Convert.ToInt32 (Console.ReadLine ());
            if (lvl_1 == 1) {
                Console.WriteLine ("The badger sees you and eats you. \nYou fail. \n\nPress enter to try again.");
                Console.ReadKey ();
                goto lvl_1_start;
            } else if (lvl_1 == 2) {
                Console.WriteLine ("The badger scurries away!");
                goto lvl_2_start;
            } else {
                Console.WriteLine ("You pressed the wrong button. Start again.");
                goto lvl_1_start;
            }
            lvl_2_start:                                                                                                                            //LEVEL TWO
            Console.WriteLine ("Now what do you want to do? \nPress 1 to leave burrow. \nPress 2 to wait.");
            Console.ReadKey ();
            int lvl_2 = Convert.ToInt32 (Console.Read ());
            if (lvl_2 == 1) {
                Console.WriteLine ("You leave the burrow and look around.");
                Console.ReadKey ();
                goto lvl_3_prt_a_start;
            } else if (lvl_2 == 2) {
                Console.WriteLine ("You wait a little longer. \nYou can hear footsteps around your burrow. You wonder whether it's your friend - troy - or the badger still.");
                goto lvl_3_prt_b_start;
            } else {
                Console.WriteLine ("You pressed the wrong button. Start again.");
                goto lvl_2_start;
            }
            lvl_3_prt_a_start:                                                                                                                      //LEVEL THREE PART A
            Console.WriteLine ("PlaceHolder");
            Console.ReadKey ();
            lvl_3_prt_b_start:
            Console.WriteLine ("PlaceHolder");
            Console.ReadKey ();

        }
    }
}

The problem occurs when running it. The first section works fine, and pressing '1', '2' or any other number works - but on section two, it always comes up with the else section, none of the if or if else clauses. Any quick help would be great. Thanks!

Upvotes: 2

Views: 1077

Answers (1)

Tim S.
Tim S.

Reputation: 56556

The problem is that you're calling both ReadKey and Read. These each read a single character, so ReadKey gets your selection, and Read just gets your \r (first character of newline). ReadKey returns info about your keypress (which is ignored), and Read returns an int corresponding to the char value you entered (note that int and char are numeric types, and converting one of those ToInt32 is different from parsing a string!).

You should use code like your working code to read input:

int lvl_2 = Convert.ToInt32(Console.ReadLine()); // or, does the same thing:
int lvl_2 = int.Parse(Console.ReadLine());

(the Convert class supports conversions in a wide range of types; if all you need to do is parse a string to an int, int.Parse is the more common option and gives you more choices)

Also, you should avoid gotos in C#. They lend themselves to writing spaghetti code, and overall are a bad idea (except for very rare exceptions). I can see that they're already leading to your method being a God Method. Here's an example that should put you more on the right track:

static void Main(string[] args)
{
    Level1();
}
static void Level1()
{
    Console.WriteLine("Welcome to AlexAdventureLand.\nPlease press the enter key to start.");
    Console.ReadKey();
    Console.WriteLine("You are a rabbit in a burrow. There is a badger outside. \nPress 1 to leave the burrow. \nPress 2 to wait.");
    int selection = int.Parse(Console.ReadLine());
    if (selection == 1)
    {
        Console.WriteLine("The badger sees you and eats you. \nYou fail. \n\nPress enter to try again.");
        Console.ReadKey();
        Level1();
    }
    else if (selection == 2)
    {
        Console.WriteLine("The badger scurries away!");
        Level2();
    }
    else
    {
        Console.WriteLine("You pressed the wrong button. Start again.");
        Level1();
    }
}
static void Level2()
{
    Console.WriteLine("Now what do you want to do? \nPress 1 to leave burrow. \nPress 2 to wait.");
    int selection = int.Parse(Console.ReadLine());
    if (selection == 1)
    {
        Console.WriteLine("You leave the burrow and look around.");
        Console.ReadKey();
        Level3A();
    }
    else if (selection == 2)
    {
        Console.WriteLine("You wait a little longer. \nYou can hear footsteps around your burrow. You wonder whether it's your friend - troy - or the badger still.");
        Level3B();
    }
    else
    {
        Console.WriteLine("You pressed the wrong button. Start again.");
        Level2();
    }
}
static void Level3A()
{
    Console.WriteLine("PlaceHolder");
    Console.ReadKey();
}
static void Level3B()
{
    Console.WriteLine("PlaceHolder");
    Console.ReadKey();
}

Upvotes: 2

Related Questions