Stepper motor on and off with switch

I am currently able to make the motor go on and off with a small cheat.
I used the Steps Per Revolution sketch and modified it.
Sometimes the motor makes a few steps after the switch shut off.
Is there a better way to just make the motor run a specific speed until told to turn off?
Here is the potion of my code.

#include <Stepper.h>

const int steps = 4;  // change this to fit the number of steps per revolution
// for your motor
const int pinInput1 = 2;
const int pinInput2 = 4;
const int pinOutput = 13;
#define analogPin A0
int sensorPin = A0;
int sensorValue;  // variable to store the value coming from the sensor
float alarmValue;   // a variable to know when to light LED - alarm value


// initialize the stepper library on pins 8 through 11:
Stepper myStepper(steps, 8, 9, 10, 11);

void setup() {
  // set the speed at 60 rpm:
  myStepper.setSpeed(45);
  // initialize the serial port:
  Serial.begin(9600);
  // set input and output pin mode
  pinMode (pinInput1, INPUT);
  pinMode (pinInput2, INPUT);
  pinMode (pinOutput, OUTPUT);
  digitalWrite (pinOutput, HIGH);
  alarmValue = 200; // initialize alarm value in Celsius
}

void loop() {
  // check if input 2 is on
  if (digitalRead (pinInput2) == HIGH)
  {
    //if input 2 is on turn on output
    //digitalWrite (pinOutput, HIGH);
    //if input 2 on step in one direction:
  Serial.println("counterclockwise");
  myStepper.step(steps);
  }

Four steps per revolution implies giant steps. 200 steps per revolution is more common. Post a link to the datasheet for your stepper motor and also tell us what stepper driver you are using.

Also I think you are using steps incorrectly in the line

myStepper.step(steps)

The value there should be the number of steps you want the motor to move - it does not have to be the same as the number of steps per revolution. I suspect, for what you want, it should be myStepper.step(1) as then there will be no steps in the queue when you switch off. However that may screw up the speed - I am not familar with the Stepper library.

If you use the AccelStepper library you could set the number of steps-to-go to zero when you switch off.

...R
Stepper Motor Basics