Data File problem

This is a discussion on Data File problem within the C++ Programming forums, part of the General Programming Boards category; Hello everybody. I am having problems with this program: Code: #include <iostream> #include <fstream> using namespace std; class Date { ...

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    18

    Data File problem

    Hello everybody.
    I am having problems with this program:

    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    class Date
    {
    private:
    	int month;
    	int day;
    	int year;
    	static char slash;
    public:
    	void setDate();
    	void showDate();
    	void setMonth();
    	void setDay();
    	void setYear();
    };
    char Date::slash = '/';
    void Date::setDate()
    {
    	cin  >> month;
    	cin.get();
    	cin  >> day;
    	cin.get();
    	cin  >> year;
    	cin.get();
    }
    void Date::setMonth()
    {
    	if(month > 12 || month == 0)
    		month = 12;
    	else
    		month = month;			
    }
    void Date::setDay()
    {
    	if(day > 31 || day == 0)
    		day = 31;
    	else
    		day = day;
    }
    void Date::setYear()
    {
    	if(year <= 999 || year > 2500)
    		year = 2007;
    	else
    		year = year;						
    }
    void Date::showDate()
    {
    	cout << month << slash << day << slash << year << endl;	
    }
    void main()
    {
    	Date aDate[5];
            cout << "Please enter five dates." << endl;
    	cout << "You cant enter any slashes(/), only numbers(12 3 2007)" << endl;
    	ofstream out("H:\\Dates.txt");
    	for(int x = 0; x < 5; ++x)
    	{
    	    aDate[x].setDate();
    	    aDate[x].setMonth();
    	    aDate[x].setDay();
    	    aDate[x].setYear();
    	    out.write((char*)(&aDate[x]), sizeof(aDate[x]));
    	} // end of for
    	cout << endl << "The dates you entered were: " << endl << endl;
    	for(int x = 0; x < 5; ++x)
    		aDate[x].showDate();
    	cout << endl;
    	system("pause");
    }
    The program has to accept 5 dates that the user inputs..
    Like this:
    12 3 1987
    11 4 2000
    And so on. then the program outputs the dates entered whit slashes like this:
    12/3/1987
    11/4/2000

    The problem is that I want to save the dates entered in a .txt file. I've done this before but never with objects. Since the data objects are type int and I think that I need to use the write() function, that only accept chars.
    I need to save the dates without the slashes and then open the document's data in he program, I think I need the read() function but I cant even save what I want in the first place.
    So whats wrong with my code?
    Thanks to everyone for their time.

  2. #2
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    umm what about
    Code:
    ofstream out("H:\\Dates.txt");
    out << date[x].month << '/' << date[x].day << '/' << date[x].year << '\n';
    and with this
    Code:
    out.write((char*)(&aDate[x]), sizeof(aDate[x]));
    you are trying to write a class to a file which cannot be done in such a fashion. You could have your class use the ()operator that return a char* then you could use the write function like that.
    Last edited by Raigne; 10-09-2007 at 03:13 PM.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What do you mean "it doesn't work to save the date"? Your code looks right to me. It may not be the most portable way to store data.

    I also have some concern for the completeness of your error checking (e.g. only some months allow for 31 days, negative numbers are valid in an integer, but you only check for less or equal to zero, etc).

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

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Raigne View Post
    umm what about
    Code:
    ofstream out("H:\\Dates.txt");
    out << date[x].month << '/' << date[x].day << '/' << date[x].year << '\n';
    That's NOT saving it WITHOUT slashes, is 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.

  5. #5
    Registered User
    Join Date
    Jan 2007
    Location
    Euless, TX
    Posts
    144
    Well, I think you have two options. Either declare the day/month/year as chars or strings, or convert your ints after checking them for proper bounds. You can always use atoi() if you use chars. Just a thought to make it easier for what you'd like to do.

    The easiest way to write your data to the file is thus:

    out << aDate[x].setMonth();

    and so on.

    Hope this helps

  6. #6
    Registered User
    Join Date
    Aug 2007
    Posts
    18
    What do you mean "it doesn't work to save the date"? Your code looks right to me. It may not be the most portable way to store data.

    I also have some concern for the completeness of your error checking (e.g. only some months allow for 31 days, negative numbers are valid in an integer, but you only check for less or equal to zero, etc).

    --
    Mats
    Well it looks good to me too, but it just wont save what I want!! and about my error checking, am aware of what you mentioned but thats what my professor asked for, so am not going to torture myself and check for all possible scenarios on entering a date. Its just a simple program to practice the use of static variables and saving to .txt file. But anyway thanks for your time, oh and what do you mean by:
    It may not be the most portable way to store data.

  7. #7
    Registered User
    Join Date
    Aug 2007
    Posts
    18
    I followed matsp and kcpilot's advice and changed my code to this:
    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    class Date
    {
    private:
    	int month;
    	int day;
    	int year;
    	static char slash;
    public:
    	void setDate();
    	void showDate();
    	void setMonth();
    	void setDay();
    	void setYear();
    };
    char Date::slash = '/';
    void Date::setDate()
    {
    	cin  >> month;
    	cin.get();
    	cin  >> day;
    	cin.get();
    	cin  >> year;
    	cin.get();
    }
    void Date::setMonth()
    {
    	if(month > 12 || month == 0)
    		month = 12;
    	else
    		month = month;			
    }
    void Date::setDay()
    {
    	if(day > 31 || day == 0)
    		day = 31;
    	else
    		day = day;
    }
    void Date::setYear()
    {
    	if(year <= 999 || year > 2500)
    		year = 2007;
    	else
    		year = year;						
    }
    void Date::showDate()
    {
    	cout << month << slash << day << slash << year << endl;	
    }
    void main()
    {
    	Date aDate[5];
            cout << "Please enter five dates." << endl;
    	cout << "You can enter any slashes(/), only numbers(12 3 2007)" << endl;
    	ofstream out("H:\\Dates.txt");
    	for(int x = 0; x < 5; ++x)
    	{
                aDate[x].setDate();
    	    aDate[x].setMonth();
    	    aDate[x].setDay();
    	    aDate[x].setYear();
    	    out << aDate[x].month << aDate[x].day << aDate[x].year << endl;
    	}
    	cout << endl << "The dates you entered were: " << endl << endl;
    	for(int x = 0; x < 5; ++x)
    		aDate[x].showDate();
    	cout << endl;
    	system("pause");
    }
    But then it gives me this error:
    Error 1 error C2248: 'Date::month' : cannot access private member declared in class 'Date' h:\documents\c++\oop\program7\program7\program7.cp p 66
    Anyway... thanks!

  8. #8
    Registered User mikeman118's Avatar
    Join Date
    Aug 2007
    Posts
    183
    If you have to convert an int to a char array look here, but you should just use the methods that suggest.

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> Error 1 error C2248: 'Date::month' : cannot access private member declared in class 'Date'

    This tells you the problem. The code in main cannot access the private member called month. The fact that the member is private means that only member functions of the class can access it.

    You need some get functions (like getMonth() and getDay()) and then call those to get the information from the class object.

    Another solution that is probably better would be to overload operator<< to output the date and make that function a friend so it can access the data itself.

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If you are going to save it as text, then you need to put some sort of separator between the numbers, as you can't tell if it's 11 1 2007 or 1 11 2007 for example when the number is compressed like you are trying to do. Space is an excellent separator in this instance.

    And of course, you'll need to produce some "getter" functions to get the month, day and year. These are very similar to your setter functions that you already have, but of course delivers back the data stored, rather than setting something inside the class.

    An alternative is of course to have a variation of "showdate" that produces a string of some sort with the date stored in it, separators and all.

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

  11. #11
    Registered User
    Join Date
    Aug 2007
    Posts
    18
    WTF?
    Okay so I changed my code to this:
    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    class Date
    {
    private:
    	int month;
    	int day;
    	int year;
    	static char slash;
    public:
    	void setDate();
    	void showDate();
    	void setMonth();
    	void setDay();
    	void setYear();
    };
    char Date::slash = '/';
    void Date::setDate()
    {
    	cin  >> month >> day >> year;	
    }
    void Date::setMonth()
    {
    	if(month > 12 || month == 0)
    		month = 12;
    	else
    		month = month;			
    }
    void Date::setDay()
    {
    	if(day > 31 || day == 0)
    		day = 31;
    	else
    		day = day;
    }
    void Date::setYear()
    {
    	if(year <= 999 || year > 2500)
    		year = 2007;
    	else
    		year = year;						
    }
    void Date::showDate()
    {
    	cout << month << slash << day << slash << year << endl;	
    }
    void main()
    {
    	Date aDate[5];
            cout << "Please enter five dates." << endl;
    	cout << "You can enter any slashes(/), only numbers(12 3 2007)" << endl;
    	ofstream out("H:\\Dates.txt");
    	for(int x = 0; x < 5; ++x)
    	{
    	    aDate[x].setDate();
    	    aDate[x].setMonth();
    	    aDate[x].setDay();
    	    aDate[x].setYear();
    	    out << aDate[x].month << " " << aDate[x].day << " ";
    	    out << aDate[x].year << endl;
    	}
    	cout << endl << "The dates you entered were: " << endl << endl;
    	for(int x = 0; x < 5; ++x)
    		aDate[x].showDate();
    	cout << endl;
    	system("pause");
    }
    And it gives me 3 errors:
    Error 1 error C2248: 'Date::month' : cannot access private member declared in class 'Date' h:\documents\c++\oop\program7\program7\program7.cp p 61
    Error 2 error C2248: 'Date::day' : cannot access private member declared in class 'Date' h:\documents\c++\oop\program7\program7\program7.cp p 61
    Error 3 error C2248: 'Date::year' : cannot access private member declared in class 'Date' h:\documents\c++\oop\program7\program7\program7.cp p 62
    But it saves what I need in a .txt file. Why does it give me these errors... the program works perfectly!

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    The program didn't compile, so if you are running something and it is "working", then you aren't running the code above. Perhaps that file already existed with the data in it?

    As far as the errors go, look at the reponses to your last post. We gave you suggestions on how to fix those exact errors.

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Just to re-iterate my suggestion to solve "can't access private member xxx": You need so called "getter" functions, e.g getDay() that returns the day within your class.

    The idea behind having classes declare things private is that you can then re-shape the internal representation of your date (say change the internals so you store the parts of the date as a character array containing roman numerals, for example). The outside world shouldn't know how you stored it, just that when they ask, the day, month and year comes out as (say) an integer.

    --
    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. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 04:02 AM
  2. Bitmasking Problem
    By mike_g in forum C++ Programming
    Replies: 13
    Last Post: 11-07-2007, 11:24 PM
  3. Please Help Reading Data From A File
    By NickHolmes in forum C Programming
    Replies: 5
    Last Post: 05-29-2005, 11:24 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM

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