0

Newbie here:

I'm working on a small console game. Few days ago I came up with some code that was doing exactly what I wanted during character creation - I wanted the user to be able to type in anything BUT if the name string contains a number or is empty then force them to try again. IF the name was a normal string I'd take the first letter and swap it with the same letter in upper case so the name would look nice later on.

I came up with something like this:

Player player = new Player();
do
{
    char x; char y;
    Console.Write("Enter your name: ");
    player.playerName = Console.ReadLine().ToLower();
    x = player.playerName[0]; y = Convert.ToChar(player.playerName[0].ToString().ToUpper());
    player.playerName = player.playerName.Replace(x, y).ToString();
}
while (player.playerName == "" || !(player.playerName).All(char.IsLetter));

For a couple of days, it's been working perfectly, just like other things, I had my friend try to "break" the game and he didn't manage to. I moved on with the code, doing other things.

Yesterday I was working on other stuff and I had to ask a question here on the site: How to call a method after each line of code or something like that. I got a great answer and I used it, everything works. However, because of that, I had to put some changes in my Player() class. Basically, it now looks like this:

public class Player
{
    public static bool isDead;
    public string playerName;
    public string playerSex;
    public string playerVocation;
    public string playerRace;
    public int playerLevel;
    public int playerExperience;
    protected int _playerHealth = 0;
    public int playerHealth
    {
        set
        {
            _playerHealth = value;
            if (_playerHealth == 0)
            {
                isDead = true;
                Console.WriteLine("You are dead!");
                Console.WriteLine();
                Console.WriteLine();
                Console.WriteLine();
                Console.WriteLine();
                Console.Write("Press any key to continue...");
                Console.ReadKey();
                Console.Clear();
                Environment.Exit(0);
            }
        }
        get
        {
            return _playerHealth;
        }
    }
    public int playerMana;
    public int playerMagicResistance;
    public int playerPhysicalResistance;
}

Now the line:x = player.playerName[0] in the main "Program" throws an error whenever I press enter while creating a character name. Before the changes I'm talking about when the user input was "empty" it would just ask them to do it again. Now it simply throws IndexOutOfRange error.

I tried making variables static in the Player() class, then removing the static keyword and just declaring a new player object - now nothing seems to work and I haven't found a solution despite trying. It's really killing me that it was working before and now it's not.

#edit I read the suggested answer but it's mostly about arrays and it's not the case.

TinyPaws
  • 3
  • 3
  • So you changed it from requiring a name (by asking again) to allowing an "empty" name. How do you think that would affect `player.playerName[0]`? It has nothing to do with static, objects, etc. – D Stanley Oct 12 '20 at 13:55
  • Hey, Stanley, thanks for answering here, they blocked that topic but I still can't figure it out. Look, this is the code that worked for me originaly and I just changed everything back to exactly how it was but now, as I said, it doesn't work: – TinyPaws Oct 12 '20 at 14:16
  • do { Console.Write("Enter your name: "); player.playerName = Console.ReadLine().ToLower(); char l = player.playerName[0]; char n = Convert.ToChar(player.playerName[0].ToString().ToUpper()); player.playerName = player.playerName.Replace(l, n).ToString(); } while (player.playerName == "" || !(player.playerName).All(char.IsLetter)); – TinyPaws Oct 12 '20 at 14:17
  • My point is if you enter a blank name then `player.playerName[0]` will throw an exception because there is no "first" letter. your `do...while` loop should also fail with an empty entry because it tries to check the first letter _before_ checking if the name is blank. The point of closing the question is to direct you to an answer that helps you understand _why_ you are getting that error and how to debug to find it yourself. – D Stanley Oct 12 '20 at 14:41
  • There's not one right way to fix your particular problem. Certainly you should do the length check before trying to access the first letter, but exactly how you do that is up to you. – D Stanley Oct 12 '20 at 14:43

0 Answers0