-1

Soooo this program seems logical, and I've been able to make programs with the same idea in c++ and java whenever i run it everything just closes all of a sudden... perhaps i just got something downloaded wrong

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            /* 
            int[] numbahs= new int[5];
            numbahs[0] = 4;
            numbahs[1] = 3;
            Console.WriteLine(numbahs[1].ToString());

            string[] cow = new string[] { "pee", "poop" };
            foreach (string pee in cow)
            {

                Console.WriteLine(pee);
            }
            */

            int x;
            int y;
            Console.WriteLine("enter x:");
            x = Console.Read();
            Console.WriteLine("enter y:");
            y = Console.Read();
            int z = (x > y)? 8 : 2;
            Console.WriteLine("x:{0} y:{1} z:{2}",x ,y, z);
            if (z > x)
            {
                Console.WriteLine("yoyo");
                Console.Read();
            }
            else { Console.WriteLine("hihi"); }

            Console.Read();

        }
    }
}
Mohamad Shiralizadeh
  • 8,329
  • 6
  • 58
  • 93
Mike Fellner
  • 385
  • 1
  • 3
  • 7

2 Answers2

5

Use Console.ReadLine

    int x;
    int y;

    Console.WriteLine("enter x:");
    x = int.Parse(Console.ReadLine());
    Console.WriteLine("enter y:");
    y = int.Parse(Console.ReadLine());

    int z = (x > y)? 8 : 2;

    Console.WriteLine("x:{0} y:{1} z:{2}",x ,y, z);
    if (z > x)
    {
        Console.WriteLine("yoyo");
    }
    else { Console.WriteLine("hihi"); }

    Console.ReadLine();
Dimitris Fousteris
  • 1,091
  • 8
  • 12
  • @B.K., actually this is the correct solution, yours is not. – walther Oct 18 '14 at 20:40
  • @B.K.: This will work. The mistake in the original code was to assume that `Console.Read()` would read and return an int when it actually returns the next character entered. Entering a few digits and a newline would result in all the `Console.Read()` calls returning and the program terminating. – Martin Liversage Oct 18 '14 at 20:41
  • @B.K both will work. Console.Readline() will block until you type a string and hit enter, while console.ReadKey() until you press any key. – Dimitris Fousteris Oct 18 '14 at 20:43
  • wow thanks I'm sorry for not researching enough before I posted, I am going through these microsoft virtual academy tutorials and never knew about the int.Parse thing – Mike Fellner Oct 18 '14 at 20:51
  • @DimitrisFousteris - This answer could be much better if you explain what is going on and why this is the answer. – Emond Oct 18 '14 at 20:59
  • @walther You're right, I misunderstood your initial response. – B.K. Oct 19 '14 at 02:28
-1

Add a Console.ReadLine() instead of Console.Read.

The correct explanation is here : Why is the console window closing immediately without displaying my output?

Community
  • 1
  • 1
e4rthdog
  • 5,103
  • 4
  • 40
  • 89