Question else if block not getting hit ... not sure why

rbalbat

New member
Joined
Aug 25, 2018
Messages
3
Programming Experience
Beginner
I wrote the following code which prompts the user to enter a (road) speed limit and speed of car. The program works except for when the speed of car is less than the speed limit. I highlighted the else if block that should catch this. Can't figure out why. Any ideas?


C#:
using System;
namespace SpeedingCamera
{
    class Program
    {
        /*3- 4- Your job is to write a program for a speed camera. For simplicity, ignore the details such as camera, sensors, 
        etc and focus purely on the logic. Write a program that asks the user to enter the speed limit. Once set, the program
        asks for the speed of a car. If the user enters a value less than the speed limit, program should display Ok on the 
        console. If the value is above the speed limit, the program should calculate the number of demerit points. For every
        5km/hr above the speed limit, 1 demerit points should be incurred and displayed on the console. If the number of 
        demerit points is above 12, the program should display License Suspended.
        */
        static void Main(string[] args)
        {
            var demeritPoints = 0;
            Console.WriteLine("Please enter the speed limit");
            string num1 = Console.ReadLine();
            //Convert string to integer
            int speedLimit = Convert.ToInt32(num1);
            Console.WriteLine("Please enter the speed of the car");
            string num2 = Console.ReadLine();
            //Convert string to integer
            int speedOfCar = Convert.ToInt32(num2);
            Console.WriteLine("speedOfCar: " + speedOfCar);
            Console.WriteLine("speedLimit: " + speedLimit);
            if ( speedOfCar < speedLimit)
            {
                Console.WriteLine("Ok");
            }
            else if ( speedOfCar > speedLimit)
            {
                demeritPoints = speedOfCar - speedLimit;
                Console.WriteLine("You were going " + demeritPoints + " over the speed limit");
            }
            [B]else if (speedOfCar < speedLimit)
            {
                demeritPoints = speedLimit - speedOfCar;
                Console.WriteLine("You were going " + demeritPoints + " under the speed limit");
            }[/B]
            else
            {
                Console.WriteLine("You were going the speed limit");
            }
            // Added these 2 lines to keep cmd window from closing before I can see response.
            Console.WriteLine("Press enter to close...");
            Console.ReadLine();
        }
    }
}
 
Your second 'else if' block is using the same condition as the 'if' block, so it can't ever possibly be hit. If that condition is true then the 'if' block will be executed and none of the other blocks will be examined. Basically, you need to move the contents of that second 'else if' block into the 'if' block.
 
Your second 'else if' block is using the same condition as the 'if' block, so it can't ever possibly be hit. If that condition is true then the 'if' block will be executed and none of the other blocks will be examined. Basically, you need to move the contents of that second 'else if' block into the 'if' block.

Thanks so much! I don't know why I missed that.
 
Back
Top Bottom