1

I'm an absolute beginner when it comes to C# and tried to do a challenge where I make a system that calculates a cars speed in mph. My code works fine until the last part, where I take the time in seconds, divide by 3600 to get it into hours then do the distance of 1 mile divided by this time for mph. But some of the time, I get infinity, others I just get the completely wrong answer. I've tested small numbers like 2, 3, 4 which give infinity and larger ones like 1000000 that give incorrect answers. Other similar questions had suggestions like changing == in if statements to .Equals(), that doesn't help either.

Here's my code:

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

namespace Carspeed
{
    class Program
    {
        static void Main(string[] args)
        {
            Checkspeed();
        }

        static void Checkspeed()
        {
            Console.WriteLine("Has the car passed the first camera? Y/N?");
            string check1 = Console.ReadLine();
            check1 = check1.ToLower();

            if (check1 == "y")
            {
                Checkspeed2();
            }
            else
            {
                Checkspeed();
            }
        }
        
        static void Checkspeed2()
        {
            Console.WriteLine("Has the car passed the second camera? Y/N?");
            string check2 = Console.ReadLine();
            check2 = check2.ToLower();

            if (check2 == "y")
            {
                Speedconv();
            }
            else
            {
                Checkspeed2();
            }
        }
        
        static void Speedconv()
        {
            Console.WriteLine("How long did it take to get there in seconds?");
            int time = Convert.ToInt32(Console.ReadLine());

            if (time <= 0)
            {
                Speedconv();
            }
            else
            {
                double timeadj = time / 3600;
                Console.WriteLine(1 / timeadj);
                Console.ReadLine();
            }
        }
    }
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
ADLG
  • 11
  • 2
  • 1
    As a side note, you're using recursion for what is probably best handled by loops. If you answer "n" too many times, it'll throw `StackOverflowException`. It's a lot of times, but that's beside the point. – madreflection Apr 15 '21 at 22:08
  • 3
    When `time` is less than 3600, `time / 3600` will return 0. See duplicate. Had you stepped through the code in a debugger, you would have seen the erroneous result which, at the very least, would have allowed you to post a clearer question, and at best would have made it easier for you to search the site yourself for the answer. – Peter Duniho Apr 15 '21 at 22:10
  • Very likely, your integer division for `timeAdj` is evaluating to zero. You can fix this by simply dividing by `3600.0` instead of `3600`. This forces the division into a floating point calculation. – Robert Harvey Apr 15 '21 at 22:11

0 Answers0