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

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"

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:
There is unused argument (why? it is used to initialize...)
Variable is initialized with uninitialized value (why?)
It made me look closer at this constructor and then you can see the typo.

Related

I am having problem with the << operator in the owner file

I have made a programme that has two types of classes that implements owner as well as dogs , I am having a problem with the iterator part in the << operator in the Owner.h file, I know that we can't use an iterator for an STL container having its type as a custom class , but then what is the alternative to do so ?
I am attaching the files for you reference with the main file
I know this is a lot to ask from you guys , but just recommend me a way and i'll figure it out
Thanks
Main File
#include <iostream>
#include "Owner.h"
#include "Dog.h"
using namespace std;
void Purchase(Owner& owner, Dog& dog) {
owner.AddDog(dog);
dog.SetOwner(owner);
}
int main() {
Owner owner1("Michael Hagley", "14 Brentwood Terrace");
Owner owner2("Oliver Walter", "299 Mill Road");
Dog dog1("Cheeto", 5, 2000.00);
Dog dog2("Mavrick", 8, 1800.00);
Dog dog3("Biglet", 4, 2100.00);
Dog dog4("Snoopy", 11, 600.00);
Dog dog5("Leggo", 2, 500.00);
Dog dog6("Bugsy", 4, 1100.00);
Purchase(owner1, dog1);
Purchase(owner1, dog5);
Purchase(owner2, dog2);
Purchase(owner2, dog3);
Purchase(owner2, dog6);
cout<<owner1.GetName()<<" Has the following dogs"<<std::endl;
cout<<std::endl;
cout<<owner2.GetName()<<"has the following dogs"<<std::endl;
// cout << owner1;
//
// cout << owner2;
Owner* owner = dog1.GetOwner();
if (owner) cout << dog1.GetName() << " belongs to " << owner->GetName() << endl;
owner = dog4.GetOwner();
if (owner){
cout << dog4.GetName() << " belongs to " << owner->GetName() << endl;
}
else{
cout<<dog4.GetName()<<" Has no owner"<<endl;
}
return 0;
}
Owner File
//
// Created by Kannav Sethi on 21/04/22.
//
#ifndef FINALS_OWNER_H
#define FINALS_OWNER_H
#include <iostream>
#include <vector>
//Wwe'll be using the concept of association and aggregation here
class Dog;
class Owner{
// provided the variables for the Owner here
// the only viable STL container here is vectors of type dogs
//but there is a problem with it , I wont be able to iterate over the dogs vector due to it being of a custom type
//Lets try that out here
std::string name;
std::string address;
std::vector<Dog> dogs;
int size=0;
public:
// constructor
Owner(){
name="";
address="";
};
//constructor
Owner(std::string name,std::string address){
this->name=name;
this->address=address;
};
// tried out making a copy assignment but it was futile
// Owner& operator=(Owner& oTemp){
// this->name=oTemp.name;
// this->address=oTemp.address;
// for(auto i = oTemp.dogs.begin();i!=oTemp.dogs.end();i++){
// this->dogs.push_back(i);
// }
// return *this;
// }
//add dog
void AddDog(Dog& dog){
dogs.push_back(dog);
size++;
}
//get name function
std::string GetName(){
return this->name;
}
friend std::ostream& operator<<(std::ostream& os,Owner& owner);
};
std::ostream& operator<<(std::ostream& os,Owner& owner){
std::vector<Dog>::iterator it;
os<<owner.name<<"of "<<owner.address<<" has the following dogs"<<std::endl;
double sum=0;
for(auto it=owner.dogs.begin();it!=owner.dogs.end();it){
it.operator++();
sum+=it.cost;
}
std::cout<<"the total costs of all the dogs are $"<<sum<<std::endl;
#endif //FINALS_OWNER_H
Dog file
//
// Created by Kannav Sethi on 21/04/22.
//
#ifndef FINALS_DOG_H
#define FINALS_DOG_H
#include <iostream>
#include "Owner.h"
class Owner;
class Dog{
// the variables
std::string name;
int age;
double cost;
Owner* owner;
public:
// constructors
Dog(std::string name,int age,double cost){
this->name=name;
this->age=age;
this->cost=cost;
}
// set owner
void SetOwner(Owner& _owner){
this->owner=(&_owner);
}
// get owner
Owner* GetOwner(){
return this->owner;
}
// getName
std::string GetName(){
return this->name;
}
// this streaming operator works but not the one I used in Owner
friend std::ostream& operator<<(std::ostream& os, Dog dog);
};
std::ostream& operator<<(std::ostream& os, Dog dog){
os<<dog.name<<" is "<<dog.age<<" years old and costs "<<dog.cost<<std::endl;
}
#endif //FINALS_DOG_H
You need
std::ostream& operator<<(std::ostream& os, Owner& owner) {
os << owner.name << "of " << owner.address << " has the following dogs" << std::endl;
double sum = 0;
for (auto it = owner.dogs.begin(); it != owner.dogs.end(); it++) {
sum += it->cost;
std::cout << it->name << std::endl;
}
std::cout << "the total costs of all the dogs are $" << sum << std::endl;
return os;
}
You didnt seem to realize that you have to use '->' with an iterator
You also need to make this a friend of Dog (cost is private)
// this streaming operator works but not the one I used in Owner
friend std::ostream& operator<<(std::ostream& os, Dog dog);
friend std::ostream& operator<<(std::ostream& os, Owner& o);

C++ class not printing? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I'mm getting a bit confused why this isn't printing the name!
I've got a human.cpp :
#include <string>
#include <iostream>
#include "human.h"
human::human(int age, human *n){
m_age=age;
name = new char[2];
human::~human() = default;
void human::printDetails(){
std::cout <<"name is " << name << " age is " << m_age << std::endl;
}
and human.h:
class human {
public: //: needed
human(int age, human *name);
~human();
void printDetails();
private :
char *name;
int m_age;
};
and finally the main.cpp:
#include <iostream>
#include <string>
#include "human.h"
int main()
{
human *Alex = new human(10, Alex); //pointer // needs argument //should have both age and name
Alex->printDetails(); //print not Print
}
So my issue is: it prints the age, but does not print the name? Any suggestions? Thanks :)
There is no need for any new in your code. Since you #included <string> in your code I assume you want to use it:
#include <string>
#include <iostream>
class Person
{
int age;
std::string name;
public:
Person(int age, std::string name)
: age { age },
name { name }
{}
int get_age() const { return age; }
std::string const& get_name() const { return name; }
void print_details() const {
std::cout << "My name is " << name << ". I am " << age << " years old.\n";
}
};
int main()
{
Person p{ 19, "Alex" };
p.print_details();
}
If you *really* want to do it the hard waytm:
#include <cstring> // std::strlen()
#include <utility> // std::exchange(), std::swap()
#include <iostream>
class Person
{
char *name_;
int age_;
public:
Person(int age, char const *name) // constructor
// we don't want to call std::strlen() on a nullptr
// instead allocate just one char and set it '\0'.
: name_ { new char[name ? std::strlen(name) + 1 : 1]{} },
age_ { age }
{
if (name)
std::strcpy(name_, name);
}
Person(Person const &other) // copy-constructor
: name_ { new char[std::strlen(other.name_) + 1] },
age_ { other.age_ }
{
std::strcpy(name_, other.name_);
}
Person(Person &&other) noexcept // move-constructor
: name_ { std::exchange(other.name_, nullptr) }, // since other will be
age_ { other.age_ } // wasted anyway, we
{} // "steal" its resource
Person& operator=(Person other) noexcept // copy-assignment operator
{ // since the parameter other got
std::swap(name_, other.name_); // copied and will be destructed
age_ = other.age_; // at the end of the function we
return *this; // can simply swap the pointers
} // - know as the copy&swap idiom.
~Person() { delete[] name_; } // destructor
void print_details() const
{
std::cout << "I am " << name_ << ". I am " << age_ << " years old.\n";
}
};
int main()
{
Person p{ 19, "Alex" };
p.print_details();
}
If you don't want to implement the special member functions you'd have to = delete; them so the compiler-generated versions - which won't work correctly for classes managing their own resources - won't get called by accident.
#include <iostream>
#include <cstring>
#include <new>
using namespace std;
class human {
public:
human(int age, const char * name)
{
m_age=age;
m_name = new char[strlen(name)+1];
strcpy(m_name,name);
}
~human()
{
delete[] m_name;
}
void printDetails()
{
std::cout <<"name is " << m_name << " age is " << m_age << std::endl;
}
private :
char *m_name;
int m_age;
};
int main()
{
human *Alex = new human(10, "alex"); //pointer // needs argument //should have both age and name
Alex->printDetails(); //print not Print
delete Alex;
return 0;
}
You need to read more. The example you shared was wrong at many level, also read about dynamic memory management.
I guess you're confused with the second parameter of the human constructor. Look at this changes:
human.h
class human {
public:
human(int age, char *name);
human(const human& h);
human& operator=(const human& h);
void printDetails();
virtual ~human();
private:
int age;
char *name;
};
human.cpp
human::human(int _age, char *_name) {
age = _age;
name = new char[strlen(_name)+1];
strcpy(name, _name);
}
human::human(const human& _h) { //Copy constructor
age = _h.age;
name = new char[strlen(_h.name)+1];
strcpy(name, _h.name);
}
human& human::operator=(const human& _h) { //Copy assignment operator
age = _h.age;
name = new char[strlen(_h.name)+1];
strcpy(name, _h.name);
return *this;
}
void human::printDetails(){
std::cout <<"name is " << name << " age is " << age << std::endl;
}
human::~human() { //Destructor
delete[] name;
}
main.cpp
int main() {
human *alex = new human(10, "Alex");
alex->printDetails();
human *anotherAlex = new human(*alex);
anotherAlex->printDetails();
delete alex;
delete anotherAlex;
}
A suggestions: I would use Human as the class names and alex for the variable names. (See how I capitalized them)
For beginner you may use std::string, so you already included it. ))
human.h
#include <string>
class human
{
public: //: needed
human(int age, std::string name);
void printDetails();
private :
std::string name;
int m_age;
};
human.cpp
#include <iostream>
#include "human.h"
human::human(int age, std::string n)
{
m_age = age;
name = n;
}
void human::printDetails()
{
std::cout <<"name is: " << name << " age is: " << m_age << std::endl;
}
main.cpp
#include "human.h"
int main()
{
human *Alex = new human(10, "Alex");
Alex->printDetails();
}

Retrieving values of an object from an array?

Pardon the example but in this case:
#include <iostream>
#include <string>
using namespace std;
class A {
private:
string theName;
int theAge;
public:
A() : theName(""), theAge(0) { }
A(string name, int age) : theName(name), theAge(age) { }
};
class B {
private:
A theArray[1];
public:
void set(const A value) {theArray[0] = value; }
A get() const { return theArray[0]; }
};
int main()
{
A man("Bob", 25);
B manPlace;
manPlace.set(man);
cout << manPlace.get();
return 0;
}
Is it possible for me to retrieve the contents of the "man" object in main when I call manPlace.get()? My intention is to print both the name (Bob) and the age (25) when I call manPlace.get(). I want to store an object within an array within another class and I can retrieve the contents of said array within the main.
You need to define a ostream::operator<< on your A class to accomplish that - otherwise the format how age and name should be generated as text-output is undefined (and they are private members of your A class).
Take a look at the reference for ostream::operator<<. For your A class, such a operator could be defined like this:
std::ostream& operator<< (std::ostream &out, A &a) {
out << "Name: " << a.theName << std::endl;
out << "Age: " << a.theAge << std::endl;
return out;
}
Which would output something like:
Name: XX
Age: YY
So your complete code would be:
#include <iostream>
#include <string>
using namespace std;
class A {
private:
string theName;
int theAge;
public:
A() : theName(""), theAge(0) { }
A(string name, int age) : theName(name), theAge(age) { }
friend std::ostream& operator<< (std::ostream &out, A &a) {
out << "Name: " << a.theName << std::endl;
out << "Age: " << a.theAge << std::endl;
return out;
}
};
class B {
private:
A theArray[1];
public:
void set(const A value) { theArray[0] = value; }
A get() const { return theArray[0]; }
};
int main()
{
A man("Bob", 25);
B manPlace;
manPlace.set(man);
cout << manPlace.get();
return 0;
}
which will output:
Name: Bob
Age: 25

Initialize member in print() from another function

Not sure where I am at the moment, trying to figure it out. I need to initialize the members in print() const as it is giving me random gibberish. No matter what I try to do, it does not seem to work. Not sure what to even do. Can anyone give me a hand?
*edit: Added in the rest of the code. Forgot it when I submitted the first time.
Student.cpp
#include "student.h"
//implement the required 3 functions here
Student::Student(const char initId[], double gpa)
{
// initialize a newly created student object with the passed in value
}
bool Student::isLessThanByID(const Student& aStudent) const
{
// compare the current student object with the passed in one by id.
if (strcmp(id, aStudent.id) > 0)
{
return true;
}
else
{
return false;
}
}
bool Student::isLessThanByGpa(const Student& aStudent) const
{
// compare the current student object with the passed in one by gpa
if (gpa < aStudent.gpa)
{
return true;
}
else
{
return false;
}
}
void Student::print() const
{
cout << id << '\t' << gpa << endl;
}
student.h
#ifndef STUDENT_H
#define STUDENT_H
#include <iostream>
using namespace std;
class Student
{
public:
Student(const char initId[], double gpa);
bool isLessThanByID(const Student& aStudent) const;
bool isLessThanByGpa(const Student& aStudent) const;
void print()const;
private:
const static int MAX_CHAR = 100;
char id[MAX_CHAR];
double gpa;
};
#endif
app.cpp
#include "student.h"
int main()
{
Student s1("G10", 3.9);
Student s2("G20", 3.5);
s1.print();
s2.print();
if(s1.isLessThanByID(s2))
{
cout << "about right!" << endl;
}
else
{
cout << "uhmm ..." << endl;
}
if(!s1.isLessThanByGpa(s2))
{
cout << "about right!" << endl;
}
else
{
cout << "uhmm ..." << endl;
}
system("pause");
return 0;
}
There is nothing in that code that sets the values of Student::id and Student::gpa. Your constructor has parameters initId and gpa; you should copy those into your object. Based on the declaration of Student that you provided, something this should be appropriate:
Student::Student(const char initId[], double gpa) : gpa(gpa)
{
strncpy(id, initId, Student::MAX_CHAR-1);
id[Student::MAX_CHAR-1] = '\0';
}

Using an enum from a class (C++)

I'm reading a book on C++ and was writing out some code to practice using the interface and implementation of a class. I've searched for solutions to my issue for a while to no avail.
I have a class with an enumeration inside of it. While trying to instantiate an object of that class, I am having trouble accessing the enum types outside of the class. I have tried using Book::Horror, Biblo::Horror, Biblo::Book::Horror, Horror, and even things like Biblo::Book::Genre::Horror. Can't seem to get it to let me access the types of the enum for the instantiation of my object in the main.cpp file.
Any help is appreciated! The more complex uses of C++ are still new to me. Here is my source:
book.h
#include <iostream>
#include <string>
using namespace std;
namespace Biblo{
class Book{
public:
enum Genre{
No_Genre, Horror, Comedy, Romance, Mystery
};
// The rest of this header is working fine I think, just this enum
class Invalid{}; // Used for throwing errors
Book(int n_ISBN, int n_copyrightYear, string n_title, string n_author, Genre n_genre);
Book();
// Accessors (non-modifying)
int getISBN() const { return ISBN; }
int getCopyrightYear() const { return copyrightYear; }
string getTitle() const { return title; }
string getAuthor() const { return author; }
string getGenre() const;
// Mutators
void changeAuthor(string newAuthor);
private:
int ISBN;
int copyrightYear;
string title;
string author;
Genre genre;
}; // End Book
// Helper Functions go here
bool operator==(const Book& a, const Book& b);
bool operator!=(const Book& a, const Book& b);
} // End Biblo
and main.cpp
#include <iostream>
#include "book.h"
using namespace std;
int main()
{
Biblo::Book book(100, 2012, "The Walrus", "The Eggman", Book::Horror); // THIS LINE GIVES ERROR
cout << "ISBN: " << book.getISBN() << endl;
cout << "Copyright: " << book.getCopyrightYear() << endl;
cout << "Title: " << book.getTitle() << endl;
cout << "Author: " << book.getAuthor() << endl;
cout << "Genre: " << book.getGenre() << endl;
return 0;
}
Edit: here is the book.cpp file
#include <iostream>
#include "book.h"
#include <string>
namespace Biblo{
Book::Book(int n_ISBN, int n_copyrightYear, string n_title, string n_author, Genre n_genre)
:ISBN(n_ISBN), copyrightYear(n_copyrightYear), title(n_title), author(n_author), genre(n_genre)
{
// constructor
}
Book::Book()
:ISBN(0), copyrightYear(0), title(""), author(""), genre(Genre::No_Genre)
{
// Default constructor
}
// Accessors
string Book::getGenre() const
{
if (Book.genre == Genre::No_Genre)
return "No Genre!";
if (Book.genre == Genre::Horror)
return "Horror";
if (Book.genre == Genre::Comedy)
return "Comedy";
if (Book.genre == Genre::Romance)
return "Romance";
if (Book.genre == Genre::Mystery)
return "Mystery";
}
// Mutators
void Book::changeAuthor(string newAuthor)
{
author = newAuthor;
}
// Helper Functions
bool operator==(const Book& a, const Book& b)
{
if (a.getISBN() != b.getISBN())
return false;
if (a.getCopyrightYear() != b.getCopyrightYear())
return false;
if (a.getTitle() != b.getTitle())
return false;
if (a.getAuthor() != b.getAuthor())
return false;
if (a.getGenre() != b.getGenre())
return false;
return true;
}
bool operator!=(const Book& a, const Book& b)
{
return !(a==b);
}
} // End Biblo
It seems you tried everything but the thing you needed! The enum is nested inside the Book class which is within the Biblo namespace. The code you are looking for is:
int main()
{
Biblo::Book book(100, 2012, "The Walrus", "The Eggman", Biblo::Book::Horror);
return 0;
}
You need to include the enum class. eg.:
Biblio::Book::Genre::Horror
Bunch of things that are going wrong really.
As others mentioned your enum is stashed one level deeper than you think it is, and your complaint about fixing it then producing an undefined reference is probably because at that point you bump into the fact there's nothing much initialized, and if you got past that you are returning items somewhat poorly when it comes to the enumerator.
If you use the right name space, quickly put in an actual implementation for the constructor, and get the most immediate return for your enum (an int might work) it should work, and probably look like this:
#include <iostream>
#include <string>
using namespace std;
namespace Biblo{
class Book{
public:
enum Genre{
No_Genre, Horror, Comedy, Romance, Mystery
};
// The rest of this header is working fine I think, just this enum
class Invalid{}; // Used for throwing errors
Book(int n_ISBN, int n_copyrightYear, string n_title, string n_author, Genre n_genre);
Book();
// Accessors (non-modifying)
int getISBN() const { return ISBN; }
int getCopyrightYear() const { return copyrightYear; }
string getTitle() const { return title; }
string getAuthor() const { return author; }
int getGenre() const { return genre; }
// Mutators
void changeAuthor(string newAuthor);
private:
int ISBN;
int copyrightYear;
string title;
string author;
Genre genre;
}; // End Book
Book::Book(int n_ISBN, int n_copyrightYear, string n_title, string n_author, Genre n_genre){
ISBN = n_ISBN; copyrightYear = n_copyrightYear; title = n_title; author = n_author;
};
}
using namespace std;
int main()
{
Biblo::Book book(100, 2012, "The Walrus", "The Eggman", Biblo::Book::Horror); // THIS LINE GIVES ERROR
cout << "ISBN: " << book.getISBN() << endl;
cout << "Copyright: " << book.getCopyrightYear() << endl;
cout << "Title: " << book.getTitle() << endl;
cout << "Author: " << book.getAuthor() << endl;
cout << "Genre: " << book.getGenre() << endl;
return 0;
}
Are you sue the book you're following isn't discussing details such as initializer lists or something else for constructors concurrent to, or previous to, the subjects you're looking at?
The code looked somewhat incomplete.
Edited in line here on SO, so bear with the poor formatting and the merged h/cpp look :)