-3
cout << "Are you driving a vehicle onto the ferry? (y/n) ";
    cin >> vehicle;
    if (vehicle == 'y' || vehicle == 'Y') {
        cout << "What is the length of the vehicle in feet? ";
        cin >> vehicleLength;
        if (vehicleLength > 20) {
            extraLengthCharge = (vehicleLength - 20) * extraLengthPrice; }
        cout << "Is the vehicle over 7 feet high? (y/n) ";
        cin >> vehicleHeight;
        if (vehicleHeight == 'y' || 'Y') {
            vehiclePrice = 69.00, fuelSurcharge = 10.40;
        } else if (vehicleHeight == 'n' || 'N') {
            vehiclePrice = 43.00, fuelSurcharge = 4.15;
        }
    else if (vehicle == 'n' || vehicle == 'N') {
        vehiclePrice = 0, fuelSurcharge = 0; }
    }

My code will give me the correct 'fare' if the response is yes, but if I type in no it will still assign the 'yes' value. I assume it has something to do with my else if statement format for vehicleHeight. Thanks and sorry for the novice question.

ooclay
  • 1
  • 1
  • 1
    `vehicleHeight == 'y' || 'Y'` Look back at the *first* `if` you wrote, then change this one to work the same way. – dxiv Sep 27 '20 at 00:38

1 Answers1

0

The conditions vehicleHeight == 'y' || 'Y' and vehicleHeight == 'n' || 'N' will be always true because 'Y' and 'N' will be always true and logical OR is used with them.

You should use conditions vehicleHeight == 'y' || vehicleHeight == 'Y' and vehicleHeight == 'n' || vehicleHeight == 'N' like what you used in other if statements instead.

MikeCAT
  • 73,922
  • 11
  • 45
  • 70