Why does my code say "Yes" when it should say "No"?

Viewed 170

When putting freeSeats to 0, my code still says that a person has avalibale seats in his/hers car.

I have created two classes. One for Car and one for Person. The Car class has a function to see if there are free seats in the car. A person-object can have a car. When checking if the person has avalibale seats, my code responds "Yes" even though I give input "0". Why?

#pragma once
#include <iostream>

//Here is class Car declaration
class Car {
private:
    unsigned int freeSeats; 
public:
    bool hasFreeSeats() const; 
    void reserveFreeSeat();
    Car( unsigned int freeSeats);

};


//Here is function definition
#include "Car.h"

bool Car::hasFreeSeats() const {
    if (freeSeats > 0)
        return true; 
    return false;
}

void Car::reserveFreeSeat() { 
    --freeSeats; 
}

Car::Car(unsigned int freeSeas) : 
    freeSeats{ freeSeats }        
{
}


//Here is class Person declaration

class Person {
private:
    std::string name;
    std::string email; 
    Car *car; //pointer to a car
public:
    Person(std::string name, std::string email, Car *car = nullptr);
    std::string getName() const; 
    std::string getEmail() const; 
    void setEmail(); 
    bool hasAvalibaleSeats() const; 
    friend std::ostream& operator << (std::ostream& os, const Person& p);
};

//Here is function definition 


Person::Person(std::string name, std::string email, Car *car) : 
    name{ name }, email{ email }, car{ car }
{
}

std::string Person::getName() const {
    return name;
}

std::string Person::getEmail() const {
    return email;
}

void Person::setEmail() {
    std::string newEmail;
    std::cout << "What is the e-mail adress?";
    std::cin >> newEmail;
    email = newEmail;
    std::cout << "E-mail has been set." << std::endl;
}


bool Person::hasAvalibaleSeats() const {
    if (car != nullptr) { //check if there is a car
        return car->hasFreeSeats(); 
    }
    return false; 
}



std::ostream& operator << (std::ostream& os, const Person& p) {
    std::string seats = "No";
    if (p.hasAvalibaleSeats())
        seats = "Yes";
    return os << "Name: " << p.name << "\nE-mail: " << p.email << "\nHas free seats: " << seats << std::endl;
}

//From main im calling
#include "Car.h"
#include "Person.h"

int main() {
    Car ferrari{ 2 };
    Car bugatti{ 3 };
    Car jeep{0};


    Person one{ "Aleksander","aleks@aleks.com", &ferrari };
    Person two{ "Sara","sara@sara.com", &bugatti };
    Person three{ "Daniel", "daniel@daniel.com", &jeep };
    Person four{ "Chris", "chris@chris.com" };

    std::cout << one << std::endl;
    std::cout << two << std::endl;
    std::cout << three << std::endl;
    std::cout << four << std::endl;
    system("pause");
    return 0;
}

I get

Name: Aleksander E-mail: aleks@aleks.com Has free seats: Yes

Name: Sara E-mail: sara@sara.com Has free seats: Yes

Name: Daniel E-mail: daniel@daniel.com Has free seats: Yes

Name: Chris E-mail: chris@chris.com Has free seats: No

But I want Daniel has free seats to be "No"

2 Answers

There's a typo here:

Car::Car(unsigned int freeSeas) :
    freeSeats{ freeSeats }
    {}

You wrote freeSeas instead of freeSeats. Due to that, the freeSeas parameter is unused and freeSeats{ freeSeats } does nothing as freeSeats is refering to the member variable, not the parameter.

Debugging is way easier when you enable compiler warnings. Compiler is your friend, and will help you immensely if you are willing to hear it.

For example, gcc gave me the following warnings when compiling your code:

prog.cc: In constructor 'Car::Car(unsigned int)':
prog.cc:37:23: warning: unused parameter 'freeSeas' [-Wunused-parameter]
 Car::Car(unsigned int freeSeas) :
          ~~~~~~~~~~~~~^~~~~~~~
prog.cc: In constructor 'Car::Car(unsigned int)':
prog.cc:38:16: warning: '*<unknown>.Car::freeSeats' is used uninitialized in this function [-Wuninitialized]
     freeSeats{ freeSeats }
                ^~~~~~~~~

I don't have to understand everything, but it tells me 2 things:

  1. There is unused argument (why? it is used to initialize...)
  2. Variable is initialized with uninitialized value (why?)

It made me look closer at this constructor and then you can see the typo.

Related