Thread: assignment help please

  1. #1
    Registered User
    Join Date
    Feb 2014
    Posts
    6

    Question assignment help please

    i am pretty new to programming and i can't figure out what i'm doing wrong so if someone could please take a look at my code and give me some hints on what i'm doing wrong, that would be awesome!

    assignment:

    Design a class called Date. The class should store a date in three integers: month, day and year. There should be member functions to print the date in the following forms:
    12/25/2012
    December 25, 2012
    25 December 2012
    Demonstrate the class by writing a complete program implementing it. Do not accept values for the day greater than 31 or less than 1. Do not accept values for the month greater than 12 or less than 1.
    Modify the Date class you wrote. The class should implement the following exception classes:
    InvalidDay-Throw when an invalid day (<1 or >31) is passed to the class.
    InvalidMonth-Throw when an invalid month (<1 or >12) is passed to the class.

    my code:

    Date.h file:
    Code:
    #pragma once
    #include <string>
    #include <iostream>
    #ifndef DATE_H
    #define DATE_H
    using namespace std;
    
    class Date
    {
    private:
        int month;
        int day;
        int year;
    
    public:
        Date() {month = 0; day = 0; year = 0;}
        Date(int m, int d, int y) {month = m; day = d; year = y;}
    
        int getMonth() {return month;}
    
        void setMonth(int m) {month = m;}
    
        void InvalidMonth(int m)
        {if (month >= 1 && month <= 12)
                month = m;
        else
        {        cout << "You have entered an invalid number. Please enter "; 
                cout << "a number between 1 and 12: " << endl; 
                cin >> m; }}
    
        int getMonth1() {return month;}
        
        void setMonth1(int m) 
        {if (m == 1)
        {cout << "January";}
        else if (m == 2)
        {cout << "February";}
        else if (m == 3)
        {cout << "March";}
        else if (m == 4)
        {cout << "April";}
        else if (m == 5)
        {cout << "May";}
        else if (m == 6)
        {cout << "June";}
        else if (m == 7)
        {cout << "July";}
        else if (m == 8)
        {cout << "August";}
        else if (m == 9)
        {cout << "September";}
        else if (m == 10)
        {cout << "October";}
        else if (m == 11)
        {cout << "November";}
        else
        {cout << "December"; }}
        
        int getDay() const {return day;}
    
        void setDay(int d) {day = d;}
    
        void InvalidDay(int d)
        {if (day >= 1 && day <= 31)
                day = d;
        else
        {        cout << "You have entered an invalid number. Please enter "; 
                cout << "a number between 1 and 31: " << endl; 
                cin >> d; }}
    
        int getYear() const {return year;}
    
        void setYear(int y) 
        {if (year <= 1899 || year >= 2014)
                year = y;
        else
        {        cout << "You have entered an invalid number. Please enter "; 
                cout << "a number between 1900 and 2014: " << endl; 
                cin >> y; }}
    
    };
    #endif
    
    Date.cpp file:
    
    #include "Date.h"
    #include <iostream>
    #include <string>
    using namespace std;
    
    void main() {
        Date mdy = Date();
        int m;
        int d;
        int y;
    
        cout << "Please enter the month (1-12): " << endl;
        cin >> mdy.setMonth();
            
        cout << "Please enter the day (1-31): " << endl;
        cin >> mdy.setDay();
    
        cout << "Please enter the year (1900-2014): " << endl;
        cin >> mdy.setYear();
    
        cout << mdy.getMonth() << "/" << mdy.getDay() << "/" << &Date::getYear << endl;
        cout << mdy.getMonth1() << " " << mdy.getDay() << ", " << &Date::getYear << endl;
        cout << mdy.getDay() << " " << mdy.getMonth1() << " " << &Date::getYear << endl;
        system("pause");
    
    }
    thanks in advance for any and all help!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Why do you believe there is something wrong?

  3. #3
    Registered User
    Join Date
    Feb 2014
    Posts
    6
    i keep getting compiling errors and when it does work, it shows the date as 0/0/1...also the if-else statements to guard against inputting wrong information never seem to work...

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    And what compile errors are you getting?

    Post the complete error messages exactly as they appear in your development environment.


    Jim

  5. #5
    Registered User
    Join Date
    Feb 2014
    Posts
    6
    a dialog box pops up saying "Unable to start program 'C:\blah\blah\blah\visual studio 2010\Projects\Date\Debug\Date.exe'. The system cannot find the file specified." and then down in the debug window it keeps saying "1>------ Build started: Project: Date, Configuration: Debug Win32 ------
    1> Date.cpp
    1>c:\users\elisa\documents\visual studio 2010\projects\date\date\date.cpp(13): error C2660: 'Date::setMonth' : function does not take 0 arguments
    1>c:\users\elisa\documents\visual studio 2010\projects\date\date\date.cpp(16): error C2660: 'Date::setDay' : function does not take 0 arguments
    1>c:\users\elisa\documents\visual studio 2010\projects\date\date\date.cpp(19): error C2660: 'Date::setYear' : function does not take 0 arguments
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped =========="

  6. #6
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    c:\users\elisa\documents\visual studio 2010\projects\date\date\date.cpp(13): error C2660: 'Date::setMonth' : function does not take 0 arguments
    Let's go from left to right.

    c:\users\elisa\documents\visual studio 2010\projects\date\date\date.cpp
    The name of the file that the error occurs in.

    (13)
    The line of the file that the error appears to occur on.

    error C2660
    Don't really know what this means I guess it's a compiler error code maybe. In any case the error tells us that it is an error rather than a warning (errors cannot be recovered from whereas warnings can [you should fix warnings anyway though!]).

    Date::setMonth
    An identifier. We don't really know what this means without context yet but we know the problem has something to do with the setMonth function of the Date class.

    function does not take 0 arguments
    Saying that our Date::setMonth function does not take 0 arguments. We must have given it zero arguments for it to tell us this.

    Open Date.cpp to line 13 and look for anything that says "setMonth" and then look at how many arguments it has and how many arguments it should have. cin doesn't really work quite as you're expecting it to.

    I'm sorry to come off kind of cold here but you know what they say: Catch a man a bug and feed him for a day. Teach a man to catch a bug and feed him for a lifetime.

    Your code is a fairly good start otherwise. One piece of advice I'd give is to try to avoid putting code inside header files. You've got the code for your Date class inside your Date.h file. Inside your Date.h file should just be the class declaration and then you should define the functions in a Date.cpp file.

  7. #7
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by DeadPlanet View Post
    Catch a man a bug and feed him for a day. Teach a man to catch a bug and feed him for a lifetime.
    I know that in many places bugs (e.g. cicadas) are a delicacy, but next time you invite me to dinner I will think twice about those "prawns" you serve

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    error C2660
    Don't really know what this means I guess it's a compiler error code maybe. In any case the error tells us that it is an error rather than a warning (errors cannot be recovered from whereas warnings can [you should fix warnings anyway though!]).
    Yes this is the error code the Microsoft compiler produced, you can Google this error and find much more information about what that number means (the first link).

    Jim

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The code is pretty unreadable, unfortunately. You must practice code style more. Use a known code style such as Allman, and you will have good readable code!
    To even figure out what's wrong with your code, I'd have to reformat it. That's how bad it is.
    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. #10
    Registered User
    Join Date
    Feb 2014
    Posts
    6
    well, like i said, i'm pretty new to this whole programming thing so i'm more worried with getting the code right and not with the format for now...but i will definitely work on that in the future. i also understand based on the output from the compiler that the error is occurring somewhere with my setMonth, setDay, setYear...i just don't understand what i'm doing wrong with them...

  11. #11
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I've seen much worse as far as formatting goes. There are some options for formatting shown on this page. Try to emulate one of those, and I would be surprised if your development environment or editor can't reformat code for you before you post it or submit it somewhere.

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by x2erlien View Post
    well, like i said, i'm pretty new to this whole programming thing so i'm more worried with getting the code right and not with the format for now...but i will definitely work on that in the future. i also understand based on the output from the compiler that the error is occurring somewhere with my setMonth, setDay, setYear...i just don't understand what i'm doing wrong with them...
    Code must be maintainable and readable. Thereby, code formatting is extremely important. As important, really, as getting the code right. You should not neglect formatting in favour of other things. You should get on fixing the formatting immediately and develop a way to format your code as you go. It's not something you can just bolt on later when you're done.
    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. #13
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Code must be maintainable and readable. Thereby, code formatting is extremely important.
    Replacing thereby with its definition we get: "Code must be maintainable and readable. As a result of that, code formatting is extremely important." As a result of what? You were talking about what code must be, there is a lack of action verbs to talk about any sort of process in the previous sentence. Thereby is improperly used here.

    I would suggest the word therefore, instead. Replacing it with the definition again: "Code must be maintainable and readable. For that reason, code formatting is extremely important."

    I know people hate this sort of correction; they always say communication matters most, don't criticize, blah blah blah, but I am a major stickler for using words that you know mean what you mean.

    That and code can easily become impossible to maintain in spite of legibility. Frequently, code needs to be refactored, or it was just poorly designed to begin with. A well-indented God object is a god object still. A well indented program with main() as the only function is still terrible. In the grand scheme of things, if badly indented code is preventing maintenance, indent it then.

  14. #14
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by Elysia View Post
    Code must be maintainable and readable. Thereby, code formatting is extremely important. As important, really, as getting the code right. You should not neglect formatting in favour of other things. You should get on fixing the formatting immediately and develop a way to format your code as you go. It's not something you can just bolt on later when you're done.
    In some ways I agree with Elysia, and in other ways I disagree. But I think that I'm on the same wavelength as them overall. The bit I disagree with is the statement that that "it's not something you can just bolt on later when you're done." My disagreement is minor: you certainly *can* tack it on later, however (and this is probably want Elysia really meant), you should not &$*@&$@* have to reformat stuff like indentation and braces and all that other rubbish at then end even though you, theoretically, can do that because it should reflect your organisation and thoughts in the first place. The reason you don't do it is because your source code, in my opinion, must reflect what you're thinking -- the interactions between syntax and semantics and organisation. The compiler doesn't care about all this stuff (within reason) but you should because it really is a reflection of your thought processes and how you break up blocks of code.

    When I see badly formatted code the first thing I think is "this person doesn't understand what the hell they're doing". I don't mean minor slips here and there (we all do that), but overall the layout of your code really is important. First and foremost it makes it easier to read (for example, unless I need to I don't read every single line of code written, I just kind of scan over it to get an overview of the code, the structure and the flow. If the formatting is inconsistent I can't do that and it's pretty annoying. I guess it's a bit like how the position and shape and capitilisation of glyphs that make up a single English word enable most of us to read more quickly -- we recognise shape and form before we resort to close inspect of every single character making up a single word).

    I guess what I'm saying is don't underestimate the influence of consistent, logical and "well formatted code". Nine times out of 10 I, and this is me personally, don't even really look at individual statements or expressions unless I have to. The overall structure of the program or algorithm is extremely important and this is what code formatting does (it does nothing else).

    Edit:

    Glancing at this:
    Code:
     void setMonth1(int m)     {if (m == 1)
        {cout << "January";}
        else if (m == 2)
        {cout << "February";}
        else if (m == 3)
        {cout << "March";}
    I honestly have no idea what you meant to write[1] and I can't be bothered looking any further. Sorry

    [1] Well, I do, but I'd rather not have to "read" it
    Last edited by Hodor; 02-04-2014 at 07:36 AM.

  15. #15
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Well, had the code been perfectly indented, there was no way of knowing if it was nearly always like that, or if it was indeed tacked on later. So if you really do think that people know what they're doing when their code looks neat, I think you will wind up disappointed.

    Plus I don't really mean to disagree or underestimate anything. I'm annoying; I am very good at finding nuances in discussions like these.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assignment help please
    By KC Teo in forum C++ Programming
    Replies: 4
    Last Post: 02-02-2014, 06:21 AM
  2. Help for my assignment!
    By 110abbidi in forum C Programming
    Replies: 5
    Last Post: 08-09-2012, 11:25 PM
  3. Assignment help !
    By Adam_Amaral in forum C Programming
    Replies: 10
    Last Post: 10-14-2011, 04:22 AM
  4. I need some help with an assignment..please.
    By ronaldh12 in forum C Programming
    Replies: 3
    Last Post: 09-30-2011, 09:33 AM
  5. Replies: 3
    Last Post: 04-26-2009, 08:54 AM