Sign in to follow this  
Kenny77

[C#] Problem with loops and switch statement

Recommended Posts

I'm trying to practice the use of classes by programming a simple game. When I run the code below and select option 2 (train), I go to that method, but when I return, the variable 'choice' doesn't update anymore. Main Menu code
while (user.getHealth() > 0)
            {
                choice = DisplayMainMenu(choice);
                
                switch (choice)
                {
                    case 1 : BattleArena(user.getHealth(), user.getStrength()); break;
                    case 2 : Train(user.getStrength(), strengthUp); break;
                    case 3 : Store(user.getMoney(), user.getWeapon()); break;
                    case 4 : Casino(user.getMoney()); break;
                    case 5 : ViewStats(user.getName(), user.getHealth(), user.getMoney(), user.getStrength(), user.getWeapon()); break;
                    default : DisplayMainMenu(choice); break;
                }
            }

Train method
static int Train(int strength, int value)
        {
            if (strength == 100)
            {
                Console.WriteLine("\nYour strength is maxed.");
                return strength;
            }

            char choice;
            int counter = 0;

            Console.WriteLine("\nTo increase your strength, you need to practice your moves.");
            Console.WriteLine("Enter 's' to swing your weapon, the more you do this, the stronger you get.");
            Console.WriteLine("To go back to the main menu, enter 'q'");

            choice = (char)Console.Read();

            while (choice != 'q')
            {
                Console.WriteLine("You train your strength");

                counter++;

                if (counter == 100)
                {
                    Console.WriteLine("\nYou gained a level!");

                    value += 1;
                }

                choice = (char)Console.Read();
            }
            return value;
        }

The value variable stores the change in the user's strength.

Share this post


Link to post
Share on other sites
What do you mean doesn't update anymore? Can you also post the declaration of choice in the main menu code as well as DisplayMainMenu?

Share this post


Link to post
Share on other sites
I mean it doesn't change from 2, it just keeps running the Train method. Here is the Program source file, the other file is the Hero class, with the simple get/set methods.


using System;
using System.Collections.Generic;
using System.Text;

namespace Battle_Arena
{
class Program
{
static void Main(string[] args)
{
Hero user = new Hero();

int choice = 0;
int strengthUp = 0;

string name;
Console.Write("Enter your name: ");
name = Console.ReadLine();

user.setName(name);

while (user.getHealth() > 0)
{
choice = DisplayMainMenu(choice);

switch (choice)
{
case 1 : BattleArena(user.getHealth(), user.getStrength()); break;
case 2 : Train(user.getStrength(), strengthUp); break;
case 3 : Store(user.getMoney(), user.getWeapon()); break;
case 4 : Casino(user.getMoney()); break;
case 5 : ViewStats(user.getName(), user.getHealth(), user.getMoney(), user.getStrength(), user.getWeapon()); break;
default : DisplayMainMenu(choice); break;
}
}

Console.WriteLine("You have died.");
}

static int DisplayMainMenu(int choice)
{
Console.WriteLine("\nWhat would you like to do?");
Console.WriteLine("1) Battle in the arena");
Console.WriteLine("2) Train");
Console.WriteLine("3) Visit the store");
Console.WriteLine("4) Gamble");
Console.WriteLine("5) View Stats");
Console.Write("Choice: ");
try
{
choice = int.Parse(Console.ReadLine());
}
catch (FormatException)
{
Console.WriteLine("Error entering value, try again.");
DisplayMainMenu(choice);
}

return choice;
}

static int Train(int strength, int value)
{
if (strength == 100)
{
Console.WriteLine("\nYour strength is maxed.");
return strength;
}

char choice;
int counter = 0;

Console.WriteLine("\nTo increase your strength, you need to practice your moves.");
Console.WriteLine("Enter 's' to swing your weapon, the more you do this, the stronger you get.");
Console.WriteLine("To go back to the main menu, enter 'q'");

choice = (char)Console.Read();

while (choice != 'q')
{
Console.WriteLine("You train your strength");

counter++;

if (counter == 100)
{
Console.WriteLine("\nYou gained a level!");

value += 1;
}

choice = (char)Console.Read();
}
return value;
}

static void Store(int money, string weapon)
{
Console.WriteLine("Welcome to the store.");
Console.WriteLine("You currently have a " + weapon);
}

static int BattleArena(int health, int strength)
{
Console.WriteLine("Welcome to the battle arena.");
return health;
}

static void Casino(int money)
{
Console.WriteLine("Welcome to the casino.");
}

static void ViewStats(string name, int health, int money, int strength, string weapon)
{
Console.WriteLine("\nName: " + name);
Console.WriteLine("Health: " + health);
Console.WriteLine("Money: " + money);
Console.WriteLine("Strength: " + strength);
Console.WriteLine("Weapon: " + weapon);
}
}
}

Share this post


Link to post
Share on other sites
Why are you passing choice into DisplayMainMenu? That might be causing some of the confusion, but it looks fairly correct. Where exactly is the variable not changing where you expect it to?

Share this post


Link to post
Share on other sites
Quote:
Original post by Kenny77
Right before the switch(choice) statement. When it returns from the Train method, shouldn't it run that code?


You're being completely ambiguous.

Assuming on the first loop you enter two, and don't die, then yes it should display the main menu again for you to enter a new choice. Though the way you're trying to use the parameter in DisplayMainMenu is peculiar and leaves room for ambiguous expectations from the code. You should make that take 0 params and use a local variable to read input.

Share this post


Link to post
Share on other sites
... Because it's in another loop waiting for q, so the menu bit in main doesn't ever get control back until thats done?

Set up a break point at the end of that training loop, and take a look at the call stack.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this