Please check my C++

This is a discussion on Please check my C++ within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by matsp Are you sure that you want to cast it to int&? no, that '&' is not ...

  1. #91
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by matsp View Post
    Are you sure that you want to cast it to int&?
    no, that '&' is not suppose to be there

    Are you sure the problem with operator>> on tankstatus isn't that you are declaring two
    outputs instead of one input and one output?
    The errors i'm getting are related to '<<' not '>>'...


    Code:
     : error C2084: function 'std::ostream &operator <<(std::ostream &,Car::TankStatus)' already has a body
    
    error C2264: 'operator <<' : error in function definition or declaration; function not called
    
    different error.. 
    
    TankStatus Car::getTankStatus()
    {
    	return tankStatus;
    }
    syntax error : missing ';' before 'Car::getTankStatus'
    I'm not missing any semicolons, please check below..

    Code:
    // Implementing function to return the name of current status
    
    #include "car.h"
    
    //#include "Menu.h"
    
    using namespace std;
    
    Car::Car() {};	
    
    std::string Car::getTankStatusName(TankStatus status)
    {
    	switch (status)
    	{
    		case unknown: 
    			return "unknown";
    			break;
    		case empty: 
    			return "Empty";
    			break;
    		case half: 
    			return "Half-Empty";
    			break;
    		case full: 
    			return "Full";
    			break;
    		default:
    			throw std::runtime_error("Invalid tank status");
    			return "";
    	} 
    }
    
    void Car::setCarDetails(string carMake, string carModel, unsigned yrModel, 
    						unsigned engCap, TankStatus tnkStatus)
    {
    	make = carMake;
    	model = carModel;
    	yearModel = yrModel;
    	engineCapacity = engCap;
    	tankStatus = tnkStatus;
    }
    
    // Operator < will be used by sort() function 
    
    bool Car::operator<(const Car& car) const
    {
    	return make < car.make;
    }
    
    ostream& operator << (ostream& out, Car::TankStatus enumTStatus)
    {
    	out << reinterpret_cast<int&>(enumTStatus);
    	return out;
    }
    
    istream& operator >> (istream& in, Car::TankStatus& enumTStatus)
    {
    	int tStatus;
    	in >> tStatus;
    	enumTStatus = reinterpret_cast<Car::TankStatus&>(tStatus);
    	return in;
    }
    
    // Overload for reading input streams
    istream& operator >> (istream& in, Car& car)
    {
    	string make, model;
    	int ymodel, engCap;
    	Car::TankStatus ts;
    
    	in >> make >> model;
    	in >> ymodel;
    	in >> engCap;
    	in >> ts;
    	
    	car.setCarDetails(make, model, ymodel, engCap, ts);
    }
    
    void Car::read(istream& indata, Car& car, bool isKeyboard)
    {
    	Date dates;
    
    	indata >> car;
    	if(isKeyboard) {
    		readDates(dates);
    	}
    }
    
    void readDates(Date& dates)
    {
    	int day, month, year;
    
    	cout << "\nEnter return dates [DAY - MONTH - YEAR]" << endl;
    	cin >> day;
    	cin >> month; 
    	cin >> year;
    	
    	dates.setTimeDate(day, month, year, 20, 59);
    }
    
    // Overload for enum variable when writing data
    ostream& operator << (ostream& out, Car::TankStatus enumTStatus)
    {
    	out << reinterpret_cast<int>(enumTStatus);
    	return out;
    }
    
    // Ouput the data to screen or file
    void Car::write(ostream& out, bool isScreen, Date& date)
    {
    	if(isScreen)	// To the output screen
    	{
    		out << setw(6) << make << setw(8) << model;
    		out << setw(10) << yearModel;
    		out << setw(12) << engineCapacity;
    		out << setw(9) << date.getDay() << setw(3) << date.getMonth() << setw (5) << date.getYear();
    		out << setw(13) << getTankStatusName(getTankStatus()).c_str();
    		puts("");
    	}
    	else			// To the file
    	{
    		out << " " << make << " " <<  model;
    		out << " " << yearModel;
    		out << " " << engineCapacity;
    		out << " " << date.getDay() << " " << date.getMonth() << " " << date.getYear();
    		out << " " << getTankStatus();
    	}
    }
    
    // Comparing the user entry with curent record
    bool Car::sameModel(const std::string& car_model) const
    {
    	return model == car_model;	
    }
    
    string Car::getModel()
    {
    	return model;
    }
    
    string Car::getMake()
    {
    	return make;
    }
    
    unsigned Car::getEngineCap()
    {
    	return engineCapacity;
    }
    
    unsigned Car::getYearModel()
    {
    	return yearModel;
    }
    
    TankStatus Car::getTankStatus()
    {
    	return tankStatus;
    }

  2. #92
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You do have this function twice:
    Code:
    // Overload for enum variable when writing data
    ostream& operator << (ostream& out, Car::TankStatus enumTStatus)
    {
    	out << reinterpret_cast<int>(enumTStatus);
    	return out;
    }
    Which is probably why the compiler complains about it.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #93
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by matsp View Post
    You do have this function twice:
    Code:
    // Overload for enum variable when writing data
    ostream& operator << (ostream& out, Car::TankStatus enumTStatus)
    {
    	out << reinterpret_cast<int>(enumTStatus);
    	return out;
    }
    Which is probably why the compiler complains about it.

    --
    Mats
    Hey thanks, my code is growing i'm making stupid mistakes...


    Down to one problem... Can't seem to return the enum 'TankStatus' from getTankStatus()... I had to change the return type to int, by also casting tankStatus to int before return. But the code compiles... Am about to test the operation, that's gonna be the big one!

  4. #94
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,027
    Plus
    TankStatus Car::getTankStatus()
    should be
    Car::TankStatus Car::getTankStatus()
    Because TankStatus is defined/declared inside Car.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #95
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by Elysia View Post
    Plus
    TankStatus Car::getTankStatus()
    should be
    Car::TankStatus Car::getTankStatus()
    Because TankStatus is defined/declared inside Car.
    hey cool, that solved my prob... out to test it now!

  6. #96
    Registered User
    Join Date
    Apr 2008
    Posts
    610

    Sort function

    Ok, since i've removed the date from Car class, i'm facing a sorting challenge. Well i understand that this is a logic problem which i shouldn't ask here.. Anyway, will take my chances!

    code...

    Code:
    std::vector<Car> fleet;
    
    sort(fleet.begin(), fleet.end());
    As you can see fleet is a vector of cars. Car class excludes the dates(month, day, year ,minute, etc..)... This means sort doesn't sort the dates... If i was using bubble sort which am used to, this was gonna be easy... But am not that familiar with vector sort, and not sure how to solve the dates problem....

    Thanks in advance!!!

  7. #97
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Well, if you are going to sort something based on dates, the date obviously has to be part of the "thing" you are sorting.

    I'm sure there's a fancy name/bad smell explanation for what I'm trying to say, but here is my explanation in my way of describing it:
    What you need is something like a "rental contract" which ties the car with the related driver/renter and the dates (issue and return), and perhaps other components.

    To make it simple, you probably want to have something to identify each car (registration number would be one such "unique" key that could be used) that you can use locating the actual car in question (you don't store the car itself in the contract, just which car the contract is referring to).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #98
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by matsp View Post
    Well, if you are going to sort something based on dates, the date obviously has to be part of the "thing" you are sorting.
    No that's not the initial plan... Currently the sort attempts to sort by make (Opel)... But then when fleet is displayed, dates should show next to each car!

    What you need is something like a "rental contract" which ties the car with the related driver/renter and the dates (issue and return), and perhaps other components.
    i think you asking me to add a new functionality to this program which should have been part of it anyway, well, i like new challenges!

  9. #99
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    An important part of object oriented design (as with data-base programming) is to know where different bits of data belongs, and how they hang together to make the whole application work. In general, all data should be held in one object, and other objects should either hold references or "keys" to the original object holding the data itself.

    You could of course make the date a variable within the car - that would make it possible to display the return date as part of the car itself. However, it is not how I would see the correct functionality of such a system. A car is not in itself a contract, and the contract is what ties a renter to a car. The renter is also a separate object (which you may temporarily want to ignore, as I expect that your assignment is not quite as complex as a real system would require).

    I would also note here that I'm prejudiced in this case. As I mentioned before, I implemented a full [1], commercial car rental package as my first professional job. I may be tinted by this and think in ways that match my implementation - there may be other solutions that can be used to solve the problem.

    [1] No direct support for bookings, however.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #100
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by matsp View Post
    An important part of object oriented design (as with data-base programming) is to know where different bits of data belongs, and how they hang together to make the whole application work. In general, all data should be held in one object, and other objects should either hold references or "keys" to the original object holding the data itself.

    You could of course make the date a variable within the car - that would make it possible to display the return date as part of the car itself. However, it is not how I would see the correct functionality of such a system. A car is not in itself a contract, and the contract is what ties a renter to a car. The renter is also a separate object (which you may temporarily want to ignore, as I expect that your assignment is not quite as complex as a real system would require).

    I would also note here that I'm prejudiced in this case. As I mentioned before, I implemented a full [1], commercial car rental package as my first professional job. I may be tinted by this and think in ways that match my implementation - there may be other solutions that can be used to solve the problem.

    [1] No direct support for bookings, however.

    --
    Mats
    Well i'm talking to the right person because i'm taking this project further than the 'assignment'... BTW my goal of this project is to convert it to Win API, which i also learned a lot from you and others in this forum /blushes...

    So, after reading your comment, i've being contemplating on adding two more classes [Driver.cpp & Contract.cpp]...

    Driver will have

    Code:
    class Driver 
    {
    
     .....
    
    private:
    	string firstName;
    	string lastName;
    	unsigned idNo;
    	unsigned license;
    	string address;
    	string contact;
    };
    and contract will have

    Code:
    class Contract
    {
    
     .......
    
    private:
           Date issue, return;
           Driver lender;
           Car car;
           int contractID;
    };
    I'm busy thinking this through but please comment on the idea...
    Last edited by csonx_p; 07-14-2008 at 08:55 AM.

  11. #101
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Sounds like an interesting challenge.

    Not quite (IMO). You would put the car id and the driver id in the contract, not the car and the driver themselves.

    Technically, you could have Car &car, so you hold a reference to a car - but I would just store the id itself, and look it up when/if I need it.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #102
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by matsp View Post
    Sounds like an interesting challenge.

    Not quite (IMO). You would put the car id and the driver id in the contract, not the car and the driver themselves.

    Technically, you could have Car &car, so you hold a reference to a car - but I would just store the id itself, and look it up when/if I need it.

    --
    Mats
    Well even that, i think i still need the two classes... But as you say, remove the Car & Driver from contract class and replace them with car registration & person's ID No.... But the use the driver class for calling up the lender when needs be....

    Looking forward to this!

  13. #103
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by csonx_p View Post
    Well even that, i think i still need the two classes... But as you say, remove the Car & Driver from contract class and replace them with car registration & person's ID No.... But the use the driver class for calling up the lender when needs be....

    Looking forward to this!
    Yes, you obviously need a driver class, and a contract class - my only objection was the actual content of the contract class.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  14. #104
    Registered User
    Join Date
    Apr 2008
    Posts
    610

    My new classes

    Code:
    // Class CONTRACT: Handles the rental contract details
    
    #include <string>
    #include "Date.h"
    
    using namespace std;
    
    class Contract
    {
    public:
    	Contract();
    	void setContractDetails(Date i, Date r, unsigned carR, 
    		unsigned lndrID, string contID);
    	string getContractID();
    	Date getIssueDate();
    	Date getReturnDate();
    	unsigned getLenderID();
    	unsigned getCarReg();
    
    private:
    	string contractID;
    	Date issued, returned;
    	unsigned lenderID;
    	unsigned carRegistration;
    };
    Code:
    // Class ADDRESS : Handles the physical address of the lender
    
    #include <string>
    
    using namespace std;
    
    class Address						
    {
    public:
    	Address();
    
    	Address(string firstLine, string secondLine, string surbub, 
    		string city, short code);
    	void setAddress(string firstLine, string secondLine, string surbub, 
    		string city, short code);
    	string getFirstLine();
    	string getSecondLine();
    	string getSurbub();
    	string getCity();
    	short getCode();
    private:
    	string firstLine;
    	string secondLine;
    	string surbub;
    	string city;
    	short code;
    };
    Code:
    // Class DRIVER : Handles the driver's or lender's details
    
    #include <string>
    #include "Address.h"
    
    using namespace std;
    
    class Driver						
    {
    public:
    	Driver();
    
    	void SetDriverDetails(string fname, string lname, unsigned IDno, 
    		string contact, string license);
    	string getFName();
    	string getSName();
    	Address getAddress();
    	string getContact();
    	unsigned getIDNo();
    	string getLicense();
    
    private:
    	string firstName;
    	string lastName;
    	unsigned idNo;
    	string license;
    	Address address;
    	string contact;
    };
    Code:
    // Class DATE : Handles the fleet return and issue dates
    
    #ifndef DATE
    #define DATE 1
    #endif
    
    class Date						
    {
    public:
    	Date();
    	~Date();
    	Date(int d,int m,int y,int h,int mn);
    	void setTimeAndDate(int d,int m,int y,int h,int mn);
    	int getDay();
    	int getMonth();
    	int getYear();
    	int getHour();
    	int getMinute();
    private:
    	int day;
    	int month;
    	int year;
    	int hour;
    	int minute;
    };
    I think that's about enough classes i need for this program ...
    Last edited by csonx_p; 07-16-2008 at 03:01 AM.

  15. #105
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Looks ok, but I would not store the postal code of an address as "short" - it prevents your code from being portable to other countries. In the UK for example, the postal code is X(X)9(9) 9XX, where X is "any letter", and 9 is any digit, the () being optional. So that would require a string. Canada uses a similar patter, but slightly jumbled up.

    In Sweden and the US, the postal code is a 5-digit number that may reach 99999 [and in the US, there's a sub-zip code of another 4 digits, so the zip-code for a particular location may be 99999-9999, which means that you probably want a string to store that too].

    A second note on address would be that you may want a country code in there too - as in car rental it isn't entirely unusual to rent a car in a different country than the country of residence.

    On a more coding side of things:
    Code:
    	Address(string firstLine, string secondLine, string surbub, 
    		string city, short code);
    	void setAddress(string firstLine, string secondLine, string surbub, 
    		string city, short code);
    ...
    	void SetDriverDetails(string fname, string lname, unsigned IDno, 
    		string contact, string license);
    All of those strings should be const reference values.

    I expect you need to be able to "set" the address of a driver as well?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BN_CLICKED, change button style
    By bennyandthejets in forum Windows Programming
    Replies: 13
    Last Post: 07-06-2010, 12:42 AM
  2. how to check input is decimal or not?
    By kalamram in forum C Programming
    Replies: 3
    Last Post: 08-31-2007, 08:07 PM
  3. Please check this loop
    By Daesom in forum C++ Programming
    Replies: 13
    Last Post: 11-02-2006, 01:52 AM
  4. A way to check for Win98 or WinXP
    By Shadow in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 10-31-2002, 11:06 AM
  5. how to check for end of line in a text file
    By anooj123 in forum C++ Programming
    Replies: 6
    Last Post: 10-25-2002, 12:21 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21