0

Basically, I have 3 functions The first and second functions is to check whether ignition is true or false. The third function basically is to check if ignition is on, the speed cannot be greater than 65, if the speed is greater than 65, it will "Fix" that speed at 65. Whereas, if the ignition is turn off, the speed will be 0.

However, in my code, I did a if else statement. When I print the part where ignition is turn off, the value I got is 65. It suppose to be 0.

May I know what's wrong with my code?

car.h

#ifndef car_inc_h
#define car_inc_h
#include <iostream>
#include <string>

using namespace std;

class Car {
    bool isIgnitionOn;
    int speed;
public:
    void turnIgnitionOn();
    void turnIgnitionOff();
    void setSpeed(int);
    void showCar();
};
#endif

car.cpp

#include <iostream>
#include <string>
#include "Car.h"

using namespace std;

void Car::turnIgnitionOn() {
    this->isIgnitionOn = true;
}

void Car::turnIgnitionOff() {
    this->isIgnitionOn = false;
};


void Car::setSpeed(int speed) {

    if (isIgnitionOn == true) {
        if (speed >= 65) {
            this->speed = 65;
        }
        else {
            this->speed = speed;
        }
    }
    else if (isIgnitionOn == false){
        this->speed = 0;
    }

};


void Car::showCar() {
    if (isIgnitionOn == true) {
        cout << "Ignition is on." << endl;
        cout << "Speed is " << speed << endl;
    }
    else if (isIgnitionOn == false) {
        cout << "Ignition is off" << endl;
        cout << "Speed is " << speed << endl;
    }


};

main.cpp

#include <iostream>
#include <string>
#include "Car.h"

using namespace std;

int main() {
    Car myCar;
myCar.turnIgnitionOn();
    myCar.setSpeed(35);
    myCar.showCar();

    myCar.setSpeed(70);
    myCar.showCar();

    myCar.turnIgnitionOff();
    myCar.showCar();
    return 0; 
}
stack
  • 414
  • 1
  • 7
  • 21
  • 1
    Well, have you stepped through the code in the debugger? – OldProgrammer Mar 18 '16 at 11:10
  • 7
    `speed` is never reset to 0. You can add `this->speed=0` in `turnIgnitionOff`. – Jean-Baptiste Yunès Mar 18 '16 at 11:10
  • 1
    `if (isIgnitionOn == false){ this->speed = speed; }` looks wrong, I thought you said it's meant to be 0 if the ignition is off? – Jonathan Wakely Mar 18 '16 at 11:13
  • 3
    Why do you check if the bool is `false` after he was checked for `true`? `isIgnitionOn` is either `true` or `false`. What else do you expect? –  Mar 18 '16 at 11:13
  • Also, your class should have a constructor that sets sensible initial values (probably setting speed to zero and the ignotion off). And [don't put `using namespace xxx` in headers](http://stackoverflow.com/questions/5849457/using-namespace-in-c-headers). – Jonathan Wakely Mar 18 '16 at 11:15
  • 1
    @Jean-BaptisteYunès it deserves to be an answer, cause it is. – YSC Mar 18 '16 at 11:21
  • 1
    @stack, what is set to zero? where? If you aren't showing us the actual code that isn't working then how are we supposed to answer it? – Jonathan Wakely Mar 18 '16 at 11:22
  • Also, why are you including `` in the header, where it isn't needed? And why are you including `` at all, when it's not needed anywhere? – Jonathan Wakely Mar 18 '16 at 11:24

1 Answers1

3

speed is never reset to 0. You can add this->speed=0 in turnIgnitionOff which is more logical afterall.

YSC
  • 38,212
  • 9
  • 96
  • 149
Jean-Baptiste Yunès
  • 34,548
  • 4
  • 48
  • 69