Home Ask Login Register

Developers Planet

Your answer is one click away!

Killer066 February 2016

C# program runs but then terminates?

I'm using visual studios 2012 and I'm creating a program that prompts the user to enter the number of wheels their car has, the color of their car, the mileage etc.. and will display 'the starting point of the car, the mileage, and the color of the car once the user inputs their values. But, once I start the program and I enter the number of wheels, the program terminates and doesn't go to the next step.

public class Car
{

        private int NumbofWheels;
        private int Mileage;
        private String Color;
        private double point;
        private double newPoint;
        private String owner;

        static void Main(string[] args)
        {



            Console.WriteLine("****************************************");
            Console.WriteLine("*           WELCOME TO CAR MANAGER     *");
            Console.WriteLine();
            Console.WriteLine("*             BY: MOHAMED SHIRE        *");
            Console.WriteLine("*                                      *");
            Console.WriteLine("*                                      *");
            Console.WriteLine("* **************************************");
            Console.Write("ENTER THE # OF WHEELS OF A CAR: ");
            Console.ReadKey();
        }

        public Car()
        {
            Mileage = 0;
            NumbofWheels = 4;
            point = 1000000;
        }

        public Car(int mile)
        {
            Mileage = mile;
        }

       public Car(int n, String c)
       {
           Mileage = 0;
           NumbofWheels = n;
           Color = c;
           point = 1000000;
       }

    public void setPoint( int p)
    {
        point = p;
    }

     public double getPoint()
    {
        return point;
    }
    public void setMileage(int m)
    {
        Mileage = m;

    }

    public int getMileage()
    {
        return Mileage;
    }

    public void setWheels(int w)
    {
        NumbofWheels = w;

    }

    public int getWheels()
         

Answers


Patrick Motard February 2016

what you're looking for is Console.ReadLine()


Eric J. February 2016

This is all the code your program ever executes

        Console.WriteLine("****************************************");
        Console.WriteLine("*           WELCOME TO CAR MANAGER     *");
        Console.WriteLine();
        Console.WriteLine("*             BY: MOHAMED SHIRE        *");
        Console.WriteLine("*                                      *");
        Console.WriteLine("*                                      *");
        Console.WriteLine("* **************************************");
        Console.Write("ENTER THE # OF WHEELS OF A CAR: ");
        Console.ReadKey();

You define other classes and methods, but you never create an instance of another class and never call any class methods.

You want to figure out which key is actually pressed

var key = Console.ReadKey();

and then actually do something with key.

You want to convert key to an integer because methods of Car need an integer. There are three common ways to do that:

  • int.Parse()
  • Convert.ToInt32()
  • int.TryParse()

Of those, int.TryParse() gives you the best control because you can check whether the input could actually be converted to an integer without catching exceptions.

You will need to repeat your pattern of Console.Write() and Console.ReadKey() to get the additional input you need, then create an instance of Car to pass that input on to.


Zanidd February 2016

Because it's the last setp you programmed. You'll have to call functions after you read the nr... example

console.Readkey();
doSomethingWithNr();

Post Status

Asked in February 2016
Viewed 1,694 times
Voted 5
Answered 3 times

Search




Leave an answer


Quote of the day: live life