Thread: Please check my C++

  1. #61
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by matsp View Post
    I don't really see the point of why you need the "extra fields"... Can you explain why inputting from keyboard should be different [in which fields are being filled in, that is] than reading from a file?

    --
    Mats
    Dates, i read from keyboard when i enter new card details... By the user shouldn't worry about entering the dates... But i do read dates from file...

    Also, when outputting, check the difference below...
    Code:
    ostream& operator << (ostream& out, Car& car)
    {
    	char make_temp[15], model_temp[15];
    
    	strcpy(make_temp, car.make.c_str());
    	strcpy(model_temp, car.model.c_str());
    
    	if(isScreen)	// To the output screen
    	{
    		out << setw(6) << make_temp << setw(8) << model_temp;
    		out << setw(10) << car.yearModel;
    		out << setw(12) << car.engineCapacity;
    		out << setw(9) << car.returnDate.day << setw(3) << car.returnDate.month << setw (5) << car.returnDate.year;
    		out << setw(13) << getTankStatusName(car.tankStatus).c_str();
    		puts("");
    	}
    	else			// To the file
    	{
    		out << make_temp << model_temp;
    		out << car.yearModel;
    		out << car.engineCapacity;
    		out << car.returnDate.day << car.returnDate.month << car.returnDate.year;
    		out << reinterpret_cast<int&>(car.tankStatus);
    	}
    }

  2. #62
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    First of all, I don't think "return date" is part of the car - it's part of the rental contract for the car at a given time, just like the out-date, the driver(s), etc. But be that as it may.

    Code:
    	char make_temp[15], model_temp[15];
    
    	strcpy(make_temp, car.make.c_str());
    	strcpy(model_temp, car.model.c_str());
    Why can't you just output car.make and car.model? [And you should, definitely, use strncpy() to ensure that you don't overflow your make_temp and model_temp, if you determine that it is NECESSARY to do this.]

    And certainly you need to at least put spaces between the outputs to file?

    --
    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. #63
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by matsp View Post
    And certainly you need to at least put spaces between the outputs to file?
    Mats
    Is it fine to put more than once space between the fields, as with those used for screen "setw(...)"? Why is it certain to use space BTW? i mean when i read i don't experience problems because & i don't have to worry about spaces...

    Will look at the dates, but then they would still need to be saved somewhere, maybe then i need a different text file for them... But that could be making my life more difficult cause i need to match each date with a specific car... Regardless of where the date structure is in the program, i may still read them using the same read function, correct?

  4. #64
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Are you absolutely sure you don't have a problem?

    If you don't have some sort of whitespace between fields, you would get one long line, e.g. "FordEscort20071800120720083", which will be read in as one string by the first string-capable read function.

    Have you looked at your output file?

    --
    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.

  5. #65
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by matsp View Post
    Are you absolutely sure you don't have a problem?

    If you don't have some sort of whitespace between fields, you would get one long line, e.g. "FordEscort20071800120720083", which will be read in as one string by the first string-capable read function.

    Have you looked at your output file?

    --
    Mats
    Yep, no problems at all....

  6. #66
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    k, here's my C++ being tested

    Code:
    // Overload for reading input streams
    istream& operator >> (istream& in, Car& car)
    {
    	in >> car.make >> car.model;
    	in >> car.yearModel;
    	in >> car.engineCapacity;
    	in >> car.tankStatus;
    }
    Complains....

    Code:
    error C2248: 'Car::make' : cannot access private member declared in class 'Car'

  7. #67
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by csonx_p View Post
    Yep, no problems at all....
    Somehow, I don't believe that. Can you post your file that is being generated (if it's long, perhaps post the first 4 lines or such).

    --
    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. #68
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by csonx_p View Post
    k, here's my C++ being tested

    Code:
    // Overload for reading input streams
    istream& operator >> (istream& in, Car& car)
    {
    	in >> car.make >> car.model;
    	in >> car.yearModel;
    	in >> car.engineCapacity;
    	in >> car.tankStatus;
    }
    Complains....

    Code:
    error C2248: 'Car::make' : cannot access private member declared in class 'Car'
    You either need to make it a of the friend class, or remove Car &car [which by the way should be const] and move it into the Car class. Either will work.

    --
    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.

  9. #69
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    class Car
    {
        friend istream& operator >> (istream& in, Car& car);
    };
    Be sure to put the prototype for operator >> before the Car class definition.
    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.

  10. #70
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by Elysia View Post
    Code:
    class Car
    {
        friend istream& operator >> (istream& in, Car& car);
    };
    Be sure to put the prototype for operator >> before the Car class definition.
    I was thinking the getMake(), getModel() ,etc was the solution, or is it old style of C++?

  11. #71
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by matsp View Post
    Somehow, I don't believe that. Can you post your file that is being generated (if it's long, perhaps post the first 4 lines or such).

    --
    Mats
    Oh! flip, mats, i'm the liar.. Just opened the file & yes it has spaces... I created the file a while ago with spaces, forgotten.. My bad...

    Regarding the dates .... How's the following?

    Code:
    using namespace std;
    
    #ifndef DATE
    #define DATE 1
    
    #endif
    
    class Date						
    {
    public:
    	Date();
    	~Date();	
    	
    	void setDay(unsigned);
    	void setMonth(unsigned);
    	void setYear(unsigned);
    	unsigned getDay(unsigned );
    	unsigned getMonth(unsigned);
    	unsigned getYear(unsigned);
    
    private:
    	unsigned day;
    	unsigned month;
    	unsigned year;
    };

  12. #72
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by csonx_p View Post
    I was thinking the getMake(), getModel() ,etc was the solution, or is it old style of C++?
    You can do that, too. But then again, perhaps you don't want to expose all the private information of the class? If you don't, better make it a friend. Otherwise, you can use getters.
    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.

  13. #73
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    You can do that, too. But then again, perhaps you don't want to expose all the private information of the class? If you don't, better make it a friend. Otherwise, you can use getters.
    I personally think that a operator>> or operator<< for a class should be able to access the internals of the class, that is, be a friend. You can write it using getters and setters.

    Regarding the date-class, I would probably have a "date-time" class that goes to at least minutes (if not seconds).

    I would also consider that the date/time is one unit - you don't set the month only. So your set/get function should take all the day, month, year, hour, minute (and second) as parameters. A getter may want to exist to get only month or some such, but for setter, you set all at once.

    [But you may not care if the car is picked up at 00:01 and returned at 23:59 on the same day, and thus "free" to rent because it was out "no days", when in fact it was out 23h58m].

    I would also recommend that you store the date/time as one integer (e.g. a 64-bit number representing seconds for 1st Jan 2000 or some such). It makes calculating date/time differences (which you probably will need to do if you are doing car-rentals) much easier. If you don't do that, then you will still need a function to either subtract one date from another and give the result in number of days.

    --
    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. #74
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by matsp View Post
    Regarding the date-class, I would probably have a "date-time" class that goes to at least minutes (if not seconds).

    I would also consider that the date/time is one unit - you don't set the month only. So your set/get function should take all the day, month, year, hour, minute (and second) as parameters. A getter may want to exist to get only month or some such, but for setter, you set all at once.

    [But you may not care if the car is picked up at 00:01 and returned at 23:59 on the same day, and thus "free" to rent because it was out "no days", when in fact it was out 23h58m].

    I would also recommend that you store the date/time as one integer (e.g. a 64-bit number representing seconds for 1st Jan 2000 or some such). It makes calculating date/time differences (which you probably will need to do if you are doing car-rentals) much easier. If you don't do that, then you will still need a function to either subtract one date from another and give the result in number of days.

    Mats
    Is this more like it?
    Code:
    using namespace std;
    
    #ifndef DATE
    #define DATE 1
    
    #endif
    
    class Date						
    {
    public:
    	Date();
    	~Date();
    	Date(int d,int m,int y,int h,int mn,int s);
    	void setTimeDate(int d,int m,int y,int h,int mn,int s);
    	int getDay();
    	int getMonth();
    	int getYear();
    	int getHour();
    	int getMinute();
    	int getSecond();
    private:
    	int day;
    	int month;
    	int year;
    	int hour;
    	int minute;
    	int second;
    };
    .cpp

    Code:
    #include "RentalDate.h"
    
    Date::Date()
    {
    	day=month=year=hour=minute=second=0;
    }
    
    Date:: ~Date() {}
    
    Date::Date(int d,int m,int y,int h,int mn,int s)
    {
    	setTimeDate(d,m,y,h,mn,s);
    }
    
    void Date::setTimeDate(int d,int m,int y,int h,int mn,int s)
    {
    	day=d;
    	month=m;
    	year=y;
    	hour=h;
    	minute=mn;
    	second=s;
    }
    
    
    int Date::getDay()
    {
    	return day;
    }
    
    int Date::getMonth()
    {
    	return month;
    }
    
    int Date::getYear()
    {
    	return year;
    }
    
    int Date::getHour()
    {
    	return hour;
    }
    
    int Date::getMinute()
    {
    	return minute;
    }
    
    int Date::getSecond()
    {
    	return second;
    }

  15. #75
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yeah, that looks like a date class, but sometimes you might not want to specify everything, such as perhaps minutes and seconds.
    You could make them optional
    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.

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-05-2010, 11:42 PM
  2. how to check input is decimal or not?
    By kalamram in forum C Programming
    Replies: 3
    Last Post: 08-31-2007, 07: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-24-2002, 11:21 PM