1

I'm trying to write a program to keep track of how many times I die in a game. The program runs, accepts user input, and keeps track of the information. When I run the program, it works, but you can only enter one key. For example, if you start it up and then press "D," it will add one to the "deaths" variable, but then you won't be able to type in anything else. Here's the code:

Console.WriteLine(
      "To add one to Deaths stat, press D. To add one to Charms Stat, press C. " +
      "To see all stats, press S.");

int deaths = 0;
int charms = 23;

ConsoleKeyInfo datKey;
datKey = Console.ReadKey();


if (datKey.Key == ConsoleKey.D)
{
    deaths = deaths + 1;
    Console.WriteLine();
    Console.WriteLine("Death Added");
}
if (datKey.Key == ConsoleKey.C)
{
    charms = charms + 1;
    Console.WriteLine();
    Console.WriteLine("Charm Added");
}
if (datKey.Key == ConsoleKey.S)
{
    Console.WriteLine();
    Console.WriteLine($"You have {charms} charms \nYou have died {deaths} times");
3
  • Hello and welcome to stack overflow. I edited your question a bit to add material information (and remove immaterial information). I'll also give a quick answer in a bit! Commented Jun 2, 2020 at 19:22
  • you need to keep asking for user input, until some kind of condition to stop is met. you can do that using a while loop or a for loop, depending on the rules of your game Commented Jun 2, 2020 at 19:24
  • Loops. You are looking for loops. Check out while, for, do while. Oh and don't even start using GOTO, that only leads to confusing errors. Commented Jun 2, 2020 at 19:37

1 Answer 1

3

Try this:

using System;

namespace TestApp
{
   class Program
   {
      static void Main()
      {

         Console.WriteLine("To add one to Deaths stat, press D. To add one to Charms Stat, press C (like you'll have to use that one). To see all stats, press S.");

         int deaths = 0;
         int charms = 23;

         ConsoleKeyInfo datKey;

         do
         {
            datKey = Console.ReadKey();
            switch(datKey.Key)
            {
               case ConsoleKey.D:
                  deaths++;
                  Console.WriteLine();
                  Console.WriteLine("Death Added");
                  break;
               case ConsoleKey.C:
                  charms++;
                  Console.WriteLine();
                  Console.WriteLine("Charm Added");
                  break;
               case ConsoleKey.S:
                  Console.WriteLine();
                  Console.WriteLine($"You have {charms} charms \nYou have died {deaths} times sence starting this program");
                  break;
               default:
                  Console.WriteLine();
                  Console.WriteLine("A useless key pressed");
                  break;
            }
         } while (datKey.Key != ConsoleKey.S);

         Console.ReadKey();

      }
   }
}

As you can see, I have encapsulated your request for a user input (datKey = Console.ReadKey();) in a loop, so the program will continue to ask the user to type a key. Only is the user has type the S, the loop is breaked. Furthermore I have changed your many if-statements to switch-struture, which is better to use in this situation

Sign up to request clarification or add additional context in comments.

3 Comments

This of course is the correct answer, but it would be greatly improved by telling the user exactly what you added (especially considering his experience level he may not notice)
Yes, Sidney - You are right. I have added some explanation to my answer :)
Thanks! It works perfectly now, and you introduced me to the switch command.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.