Thread: A Diary Gone Nuts

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    14

    A Diary Gone Nuts

    Hi, as part of my course at uni we have started to learn C++. One of the projects we are working on is a diary. Its supposed to do the following:

    The Diary objects created in previous weeks were hardly realistic as they had either a single meeting object inside or a single day containing an array of meetings. This practical attempts to build a more realistic diary having 365/6 day objects built in (depending on leap year or not), each with the capability of holding a variable number of meetings and having the ability to both add and delete meetings.

    The new Diary class will provide the following :-

    * A constructor specifying the year for the diary as an integer
    * An addMeeting function/method which specifies a meeting object and the date of the meeting
    * A deleteMeeting function/method which specifies the date and time of the meeting to be removed
    * A display function/method which specifies the date and displays all meetings on that day
    * A displayFirst function/method which specifies a date and displays the first meeting on that day or returns an empty string if none
    * A displayNext function/method which specifies the date and displays the next meeting on that date or returns an empty string if no more

    The day class should be revised to provide for the following :-

    * A variable number of meeting objects - using a Vector collection class rather than an array
    * Meetings should be added in time of day order
    * If an attempt to add a meeting starting at the same time as another on any given day, this should not be allowed and an error signalled to the user
    * Meetings may overlap but an error message should be displayed to the user

    The meeting class should be revised to provide for the following :-

    * A function/method to return the start time of the meeting (e.g. in minutes from midnight)
    * A function/method to return the duration of the meeting


    The part i'm having trouble with at the moment is related to addingmeetings. Even when meetings do not overlap the user is given a message informing them that they do. I was hoping that someone could point out where my logic was failing. The add meeting function is in day.cpp.

    Thanks in advance for any help!!

  2. #2
    Registered User
    Join Date
    Nov 2008
    Posts
    14
    the other files...

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    I'm feeling quite lazy. What file is the code for addingMeetings in?
    Mainframe assembler programmer by trade. C coder when I can.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Code:
    if(new_end > meeting->start_time())
            clash = true;
    Here, you are checking the to-be-added meeting against itself. Rather, you should check the to-be-added meeting against appointment.at(i).

  5. #5
    Registered User
    Join Date
    Nov 2008
    Posts
    14
    so it should be

    if(new_end > appointments.at(i)->start_time())
    clash = true;
    }

    if ive understood you??

  6. #6
    Registered User
    Join Date
    Nov 2008
    Posts
    14
    The add meeting function is in day.cpp.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by chris Ray View Post
    so it should be

    if(new_end > appointments.at(i)->start_time())
    clash = true;
    }

    if ive understood you??
    Right.

  8. #8
    Registered User
    Join Date
    Nov 2008
    Posts
    14
    In that case i still get a clash error, i'm so confused :-( sorry!!

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    With what data do you get an error?

  10. #10
    Registered User
    Join Date
    Nov 2008
    Posts
    14
    Please enter your Name: chris
    Please enter Year for Diary (YYYY): 2008
    Simple Diary
    1) Add Meetings
    2) Delete Meeting
    3) Display All
    4) Display One by One
    5) Exit
    Please choose an option: 1

    Enter name of person, type end to quit: jim
    Enter location: uni
    Enter subject: C
    Enter time (hour): 10
    Enter time (mins): 00
    Enter duration(mins): 10
    Enter Month (MM) : 10
    Enter Day of Month (DD): 10
    looking at 283start time = 600
    here
    Succeded

    Enter name of person, type end to quit: bob
    Enter location: home
    Enter subject: tea
    Enter time (hour): 10
    Enter time (mins): 30
    Enter duration(mins): 10
    Enter Month (MM) : 10
    Enter Day of Month (DD): 10
    looking at 283start time = 630
    here
    start time = 630
    start time = 600
    start time = 630
    start time = 600
    start time = 600
    start time = 630
    start time = 600
    start time = 630
    Meeting Clashed, but added

    Enter name of person, type end to quit:

    The first meeting should finish at 10:10 and the second starts at 10:30 and finishes at 10:40, there should not be a clash!!

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    First curious thing, why does diary.h include diary.h ?

    First bad thing, meeting.h has using namespace std;
    Putting "using" inside a header file forces the namespace onto anyone who includes the file. And if they didn't want all of the std namespace, there is no way out.
    You should have std::string in the header fle, and no "using" statement.

    First wrong thing.
    if(clash = false)
    What's the difference between = and == ?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Second wrong thing:
    Code:
    bool Diary::delete_meeting(tm date, int hour, int min)
    {
      if(day[date.tm_yday] == NULL)
      {
        return "No meetings on this Date";
    I'm quite sure that you shouldn't be returning a string (well, a const char* in effect) from a function that returns bool.

    Another strange thing:
    Code:
    string Day::display_first()
    {
      position = 1;
      return appointments[1]->display();
    }
    I'm too lazy to figure out if that's actually what you want, but keep in mind that [0] is the first element in arrays and array-like things in C++. (And what if there are no appointments?)

    Another probably bad thing:
    Code:
        delete &appointments.at(i);            
    // ...
          delete appointments.at(i); //remove object
    I imagine that one of those statements isn't as it should be.

    Another strange thing:
    Code:
    Diary::~Diary()
    {
      delete[]day;              
    }
    Are you sure you want to delete an array that was never dynamically allocated with new? (Hint: no, you don't want to.)
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  13. #13
    Registered User
    Join Date
    Nov 2008
    Posts
    14
    Firstly thanks for all the help, i'm making some progress now!!

    Right a quick update:

    I added the missing = sign in the if checking for clashes. i can now add meetings without a problem!!

    Should this become a string?
    Code:
    string Diary::delete_meeting(tm date, int hour, int min)
    
    bool Diary::delete_meeting(tm date, int hour, int min)
    {
      if(day[date.tm_yday] == NULL)
      {
        return "No meetings on this Date";
    You made a good point about the first element in the array being 0 and not 1 so the display first function now works quite well.

    Can you explain a bit more about this line???
    Code:
        delete &appointments.at(i);            
    // ...
          delete appointments.at(i); //remove object
    I have changed this to read

    Code:
    Diary::~Diary(){
      
    }
    All i really need to get working now is the display all function, so any help with this would be great!!

    If you want an updated bunch of source files they can be found at this address:
    Code:
    www.rayjchris.co.uk/simplediary.zip

  14. #14
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Quote Originally Posted by chris Ray View Post
    Should this become a string?
    Code:
    string Diary::delete_meeting(tm date, int hour, int min)
    
    bool Diary::delete_meeting(tm date, int hour, int min)
    {
      if(day[date.tm_yday] == NULL)
      {
        return "No meetings on this Date";
    Probably not. You don't really expect a delete_meeting function to return a string; a bool makes sense here. Just make sure you return either true or false, depending on whether the function succeeded in deleting the meeting or not, and not a string!
    Can you explain a bit more about this line???
    Code:
        delete &appointments.at(i);            
    // ...
          delete appointments.at(i); //remove object
    I was just indicating that, since those two lines were referring to the same variable, there's probably something wrong with one of them. You're deleting the .at() directly in one, and the address of the .at() in the other.
    Last edited by dwks; 12-09-2008 at 12:14 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  15. #15
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Looking at your source code again, it seems to me that this version
    Code:
    delete appointments.at(i);
    is the proper one, since appointments is a vector of pointers.

    I find it a little bizzare that the variable in main() for storing a year is called "hour"!

    What if Day::display_first()'s if fails? You won't be returning anything at all . . . .

    Also, having "using namespace std" in a header file isn't the best idea. Search the forums for why.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  2. std::string: Has my compiler gone nuts??
    By Andruu75 in forum C++ Programming
    Replies: 9
    Last Post: 09-28-2007, 04:02 AM
  3. Finding LCM--Driving me nuts
    By RealityFusion in forum C++ Programming
    Replies: 2
    Last Post: 06-05-2006, 01:23 AM
  4. This compiler is driving me nuts
    By martin_00 in forum C Programming
    Replies: 32
    Last Post: 12-31-2002, 03:25 PM
  5. driving me nuts
    By boontune in forum C++ Programming
    Replies: 3
    Last Post: 10-07-2002, 04:35 AM