0

i have been having trouble with this code for a while now and i tried searching for an answer but could not find any. when i ask the user to input the color of the car it does not wait for the input and immediately skips to the other print statement how do i fix this?

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

namespace Homework1
{
    class Car
    {
        private string color;
        private int numOfWheels;
        private int startingPoint;
        private int mileage;
        private int currentSpeed;

        public Car()
        {
            color = "";
            NumOfWheels = 0;
            StartingPoint = 100000;
            CurrentSpeed = 0;
            Mileage = 0;
        }

        public Car(string color, int numOfWheels, int startingPoint, int currentSpeed, int mileage)
        {
            Color = color;
            NumOfWheels = numOfWheels;
            StartingPoint = startingPoint;
            CurrentSpeed = currentSpeed;
            Mileage = mileage;
        }

        public virtual string Color
        {
            get
            {
                return color;
            }
            set
            {
                color = value;
            }
        }

        public virtual int NumOfWheels
        {
            get
            {
                return numOfWheels;
            }
            set
            {
                numOfWheels = value;
            }
        }

        public virtual int StartingPoint
        {
            get
            {
                return startingPoint;
            }
            set
            {
                startingPoint = value;
            }
        }

        public virtual int CurrentSpeed
        {
            get
            {
                return currentSpeed;
            }
            set
            {
                currentSpeed = value;
            }
        }

        public virtual int Mileage
        {
            get
            {
                return mileage;
            }
            set
            {
                mileage = value;
            }
        }


        public override string ToString()
        {
            return (" color " + color + " numOfWheels" + numOfWheels + "startingPoint " + startingPoint + "mileage" + mileage + "current speed" + currentSpeed);
        }
    }
}


********************************************************************************

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

namespace Homework1
{
    class CarTest
    {
        static void Main(string[] args)
        {


            Car myCar = new Car();


            Console.WriteLine("*****************************");
            Console.WriteLine("*                           *");
            Console.WriteLine("*  WELCOME TO CAR MANAGER   *");
            Console.WriteLine("*    By <<my Name>>         *");
            Console.WriteLine("*                           *");
            Console.WriteLine("*****************************");



            Console.WriteLine("\nEnter the number of wheels of a car");
            myCar.NumOfWheels = Console.Read();


            Console.WriteLine("Enter the color of the car");
            myCar.Color = Console.ReadLine();
            ///this line does not for wait input. it prints out but          immediately continues to the other print statements


            Console.WriteLine("Current mileage will be set to zero");

            Console.WriteLine("The current starting point will be set to 100000");

            Console.Write("The current status of your car \n{0:D} Wheels {1} \n{2:D} Miles and \nCAR POINT = {3:D}", myCar.NumOfWheels,
            myCar.Color, myCar.Mileage, myCar.StartingPoint);

            Console.WriteLine("\nEnter the owner's name");
            String name = Console.ReadLine();

            Console.WriteLine("Enter the miles the car ran in this week");
            myCar.Mileage = Console.Read();


            Console.WriteLine("This car is owned by {0}", name);


            Console.WriteLine("===>The current status of your car:");
            Console.WriteLine("Wheels: " + myCar.NumOfWheels);
            Console.WriteLine("Color: " + myCar.Color);
            Console.WriteLine("Current Mileage: " + myCar.Mileage);
            Console.WriteLine("Starting Point: " + myCar.StartingPoint);
            Console.WriteLine("************ Thank you for using CAR MANAGER *************");
            Console.WriteLine("----------------------------------------------------------");
            Console.WriteLine("----------------------------------------------------------");
            Console.WriteLine("Press ENTER to close console…….");
            Console.ReadKey(); 

        }
    }
}
Student214
  • 29
  • 1
  • 4
  • 10
  • If you want to read a number - use `Console.ReadLine()` and then parse it to the desired type (int, long, double, etc.). You can use `int.Parse()`, `long.Parse()` etc. – Teodor Kurtev Feb 07 '16 at 18:10
  • 1
    After the (now 4) questions on this same program, with the only improvement each time being the direct answer to your problem right then, I wonder how much of this you're doing? I'm nearly certain it's not your professor's intent to have each hurdle you encounter be solved by someone on the internet. – jdphenix Feb 07 '16 at 18:23

2 Answers2

2

This happen because you have a Read before that ReadLine. Console.Read reads just the first character from the keyboard buffer and returns, but you have typed the number and pressed enter, so when the code reaches the ReadLine there is the newline ready in the keyboard buffer that is just what ReadLine waits to return

You should use a ReadLine also for the NumOfWheels

Console.WriteLine("\nEnter the number of wheels of a car");
string userInput = Console.ReadLine();
int numOfWheels;
if(Int32.TryParse(userInput, out numOfWheels))
    myCar.NumOfWheels = numOfWheels;
else
{
   Console.WriteLine("You haven't typed a valid number for wheels!")
   return;
}


Console.WriteLine("Enter the color of the car");
myCar.Color = Console.ReadLine();

But this pose a problem. The NumOfWheels property is an integer and thus you cannot directly assign the string returned to the property. Here you need to use the Int32.TryParse to check if the input is a valid number and abort the program in case of error. (Or introduce a loop to continue asking a valid input)

Steve
  • 213,761
  • 22
  • 232
  • 286
  • this worked thanks. so Console.ReadLine() was skipping because pressing enter is practically a user input? – Student214 Feb 07 '16 at 18:18
  • Well, technically is a series of numeric values inserted in the keyboard buffer. For simplicity assume a single byte for character, you type 4 and press enter. In the keyboard buffer you will find 52 (ASCII code for 4) followed by 13 and then 10 (windows newline). The first value is returned by read while 13 and 10 remains in the buffer for the following ReadLine that expects those byte as Line terminators so it returns immediately. The funny thing is that Read returns an Integer so your Car will have 52 wheels. – Steve Feb 07 '16 at 18:21
  • ok i get it i was having that problem earlier it printed out i had 52 wheels, but now when i enter for example 50 mileage it says i end up with 53 – Student214 Feb 07 '16 at 18:25
  • The same problem. The ASCII code for "5" is 53 while the "0" is lost because your program terminates without any other Read/ReadLine. You need the pattern (ReadLine, Int32.TryParse) also here – Steve Feb 07 '16 at 18:25
  • yeah i got it thanks i understand a lot more now – Student214 Feb 07 '16 at 18:28
0

Sometimes you use Console.Read and other times you use Console.ReadLine.

If you use Console.Read you only read a character, therefore when the user enters 4 and enter, we have two characters '4' and '\n'. That '\n' is saved in the buffer and the next Console.read or Console.readline take it.

You should only use Console.ReadLine in the program.

ganchito55
  • 3,559
  • 4
  • 25
  • 46