-2

When it gets to the part where the user gets asked a question, it skips over and closes, doesn't even print the stats of Donald at the end. I can't seem to figure out why, even with read key it does not work.

using System;


namespace ConsoleApp2
{

    class Donald
    {
        public static int Health = 100;

        public static int Damage = 50;
        public static int Speed = 15;
        public static string Food;
        public static string Potion;
    }
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("What will Donald eat?: ");

            Donald.Food = Convert.ToString(Console.Read());
            Console.ReadKey();
            Console.Write("Will Donald drink a slow potion?: ");
            Donald.Potion = Convert.ToString(Console.Read());
            Console.ReadKey();
            TakeDamage();
            SlowDown();
            PrintStats();

        }

        static void TakeDamage()
        {
            if (Donald.Food == "Turkey")
            {
                Donald.Health -= 15;
            }
            if (Donald.Food == "Steak")
            {
                Donald.Health += 15;
            }
        }

        static void SlowDown()
        {
            if (Donald.Potion == "Yes")
            {
                Donald.Speed -= 50;
            }
        }

        static void PrintStats()
        {
            Console.WriteLine(Donald.Speed);
            Console.WriteLine(Donald.Potion);
            Console.WriteLine(Donald.Health);
            Console.WriteLine(Donald.Food);
            Console.WriteLine(Donald.Damage);
        }
    }
}
atline
  • 28,355
  • 16
  • 77
  • 113
  • Try setting public access on your class and property and also switch from console.read to readline – Canica Jan 24 '19 at 02:31
  • 1
    you know that Console.Read() returns int? you know that program prints something but doesnt wait after printing and exits? replace Read with ReadLine and remove ReadKey then put one ReadKey after PrintStats – Selvin Jan 24 '19 at 02:31
  • Thanks to all that helped. I used ReadLine instead of Read. – dominickator Jan 24 '19 at 03:08

2 Answers2

0

Please study the below code to see why it's different.

To wait for user input use ReadKey to read user input user ReadLine, also note that if there is nothing to wait for at the end of the program, it will quickly write you values and close, as such it will happen to fast for you to see, so you need to stop it somehow, once again ReadKey will help.

Console.Write("What will Donald eat?: ");   
Donald.Food = Console.ReadLine();

Console.Write("Will Donald drink a slow potion?: ");
Donald.Potion = Console.ReadLine();

TakeDamage();
SlowDown();
PrintStats();

Console.ReadKey();
halfer
  • 19,824
  • 17
  • 99
  • 186
TheGeneral
  • 79,002
  • 9
  • 103
  • 141
0

Change your code like below:

static void Main(string[] args)
{
    Console.Write("What will Donald eat?: ");

    Donald.Food = Console.ReadLine();

    Console.Write("Will Donald drink a slow potion?: ");
    Donald.Potion = Console.ReadLine();

    TakeDamage();
    SlowDown();
    PrintStats();

    // wait for user to exit
    Console.ReadLine();
    }

You can also put Console.ReadKey() or ReadLine() here:

 static void PrintStats()
 {
        Console.WriteLine(Donald.Speed);
        Console.WriteLine(Donald.Potion);
        Console.WriteLine(Donald.Health);
        Console.WriteLine(Donald.Food);
        Console.WriteLine(Donald.Damage);
        Console.ReadLine(); // ReadKey as this will be discarded.
 }

Difference between Read and ReadLine is here:

Difference between Console.Read() and Console.ReadLine()?

Gauravsa
  • 6,330
  • 2
  • 21
  • 30