-3

Here is the homework problem:

Write a program that computes speed: Takes distance (in meters) and time (as three numbers: hours, minutes, seconds), computes speed, in meters per second, kilometres per hour and miles per hour (hint: 1 mile = 1609 meters). Prints results to Console.

Here is my code:

int distanceInMeters, hours, minutes, seconds;
Console.WriteLine("Please type distance in meters: ");
distanceInMeters = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Please type time in hours: ");
hours = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Please type time in minutes: ");
minutes = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Please type time in seconds: ");
seconds = Convert.ToInt32(Console.ReadLine());

int metersSecond, kmH, milesH;

metersSecond = distanceInMeters / ((hours * 3600) + (minutes * 60) + seconds);
kmH = (distanceInMeters / 1000) / (hours + (minutes / 60) + (seconds / 3600));
milesH = (distanceInMeters / 1609) / (hours + (minutes / 60) + (seconds / 3600));

Console.WriteLine("Your speed in meters/seconds is: " + metersSecond);
Console.WriteLine("Please speed in km/h is: " + kmH);
Console.WriteLine("Please speed in miles/h is: " + milesH);
halfer
  • 19,824
  • 17
  • 99
  • 186
Emil Ko
  • 3
  • 1
  • 2
    All of your variables are _integers_, which means you are doing _integer division_. Is that what you really want to do? – gunr2171 Oct 02 '22 at 21:31
  • Does this answer your question? [Division returns zero](https://stackoverflow.com/questions/9288904/division-returns-zero) – Charlieface Oct 02 '22 at 22:09
  • Consider refactoring your program. First, instead of integers, use `double`s for all your quantities. Second, write a method that takes a prompt as a parameter, prompts the user, reads from the console, uses `double.TryParse` to convert. If the conversion fails, try prompting to user again; if it succeeds, return the value. Third, Use `double` literals (instead of integers) for all your constants. Fourth, consider using `const` to define all your constants: e.g. `const double secondsPerHour = 3600.0;` – Flydog57 Oct 02 '22 at 23:57
  • Actually, since you are using Hours, Minutes and Seconds to figure how much time has passed, look at the `TimeSpan` type. You can create a `TimeSpan` from those three quantities (as integers) and then use the `TotalHours` property (a double) in you calculations – Flydog57 Oct 03 '22 at 00:04

2 Answers2

0

Just cast any of the variables, say distanceInMeters to decimal and use decimal datatype for the output variables.

distanceInMeters = Decimal.Parse(Console.ReadLine());

Your output will automatically be in decimal format. You can round the digits if you want a certain level of precision.

WisdomSeeker
  • 854
  • 6
  • 8
0

All of your variables in the following computation:

metersSecond = distanceInMeters / ((hours * 3600) + (minutes * 60) + seconds);

are of type int (whole number). Therefore the decimal places will be cut of. You can fix this by doing:

metersSecond = 1.0 * distanceInMeters / ((hours * 3600.0) + (minutes * 60.0) + seconds)

also metersSecond should be declared type double, float or decimal, those types support the decimal places you want.

Bruno Pfohl
  • 451
  • 1
  • 8
  • As aditional info you could mention you could declare a type without the need of `.0`, you can explicit declare a number as decimal, floatn double... Check [this related answer](https://stackoverflow.com/a/3271831/2265446) – Cleptus Oct 02 '22 at 22:00
  • Thanks a lot Bruno! Your hint resolved my problem ! – Emil Ko Oct 03 '22 at 11:10