Page 1 of 2 1 2 LastLast
Results 1 to 20 of 32

Thread: C++ easy code help     submit to reddit submit to twitter

  1. #1
    Cerberus
    Join Date
    Oct 2008
    Posts
    436
    BG Level
    4

    C++ easy code help

    a default constructor that initializes the Date to the current date/time
    get/set the month (0-11)
    get/set the date (the day of the month, which should be 1-31)
    get/set the year (the calendar year - 1900, so 2009 would be returned as 109)
    get/set the hours (0-23)
    get/set the minutes (0-59)
    get/set the seconds (0-59)
    get the day of the week (0-6)
    getting the string representation of the date (e.g., "March 5, 2009 12:15:32")
    compare to another Date to see whether this comes before it (bool before(Date))
    compare to another Date to see whether this comes after it (bool after(Date))
    Enumerations would probably be wise for some of these things.



    anyone know how the hell to do this. so far i have:



    #include <string>
    #include <iostream>

    using namespace std;


    class Date {
    private:
    int month;
    int day;
    int year;
    int hours;
    int minutes;
    int seconds;
    string weekDay;

    public:
    int getMonth();
    void setMonth(int m);
    int getDay();
    void setDay(int d);
    int getHours();
    void setHours(int h);
    int getMinutes();
    void setMinutes(int min);
    int getSeconds();
    void setSeconds(int s);
    string getweekDay();
    void setweekDay(string wd);
    };

    int Date::getMonth() { return month; }
    void Date::setMonth(int m) {
    if(m >= 1) {
    month = m;
    }
    }

    int Date::getDay() { return day; }
    void Date::setDay(int d) {
    if(d >= 1) {
    day = d;
    }
    }

    int Date::getHours() { return hours; }
    void Date::setHours(int h) {
    if(h >= 0) {
    hours = h;
    }
    }

    int Date::getMinutes() { return minutes; }
    void Date::setMinutes(int min) {
    if(min >= 0) {
    minutes = min;
    }
    }

    int Date::getSeconds() { return seconds; }
    void Date::setSeconds(int s) {
    if(s >= 0) {
    seconds = s;
    }
    }

    string Date::getweekDay() { return weekDay; }
    void Date::setweekDay(string wd) { weekDay = wd; }

  2. #2
    Relic Weapons
    Join Date
    Oct 2006
    Posts
    313
    BG Level
    4

    do you ask the user to input the date/time? if so you're doing it wrong on the function part.

  3. #3
    Naver
    Guest

    Those are just accessors and mutators. >_> My only suggestion is to also cap the time/day/month/year instead of just checking to see if it's above zero.

    i.e.
    void Date::setMonth(int m)
    {
    if(m >=1 && m<= 12)
    month = m;
    }

    but according to the OP it asks for specifics like (0-11) for the month? So I suppose it would be if(m >= 0 && m <= 11)

    edit: Would probably also be faster on the linker / compiler to inline those accessors/mutators

  4. #4
    blax n gunz
    Join Date
    May 2005
    Posts
    11,141
    BG Level
    9

    You might want to follow the instructions on using enumerations, as well.

  5. #5
    Bagel
    Join Date
    Oct 2005
    Posts
    1,383
    BG Level
    6

    (e.g., "March 5, 2009 12:15:32")
    My birthday, rare enough occurrence to mention saying whoahi.

  6. #6
    Cerberus
    Join Date
    Oct 2008
    Posts
    436
    BG Level
    4

    Quote Originally Posted by Naver View Post
    Those are just accessors and mutators. >_> My only suggestion is to also cap the time/day/month/year instead of just checking to see if it's above zero.

    i.e.
    void Date::setMonth(int m)
    {
    if(m >=1 && m<= 12)
    month = m;
    }

    but according to the OP it asks for specifics like (0-11) for the month? So I suppose it would be if(m >= 0 && m <= 11)

    edit: Would probably also be faster on the linker / compiler to inline those accessors/mutators
    oh i was wonering about that part. The teacher said we can use the current time generater, doesnt realy care how and ty for this bit of code i was kinda confued on how to do that part. but any idea about the last part?

  7. #7
    E. Body
    Join Date
    Jan 2007
    Posts
    2,103
    BG Level
    7

    If your teacher has discussed switches, look into that. For comparing dates, you have functions which you can use to help you, hint: you've already written them. Also, you aren't going to learn how to code by having others write it for you. How do you think you should go about comparing two different dates?

  8. #8
    Cerberus
    Join Date
    Oct 2008
    Posts
    436
    BG Level
    4

    Quote Originally Posted by Eliseos View Post
    If your teacher has discussed switches, look into that. For comparing dates, you have functions which you can use to help you, hint: you've already written them. Also, you aren't going to learn how to code by having others write it for you. How do you think you should go about comparing two different dates?

    Maybe a for loop? but not sure how i would call it.

    and did i do it right?
    void main() {

    month = Date::getMonth(setmonth);

    -------------------------------------------------------------

    #include <string>
    #include <iostream>

    using namespace std;


    class Date {
    private:
    int month;
    int day;
    int year;
    int hours;
    int minutes;
    int seconds;
    string weekDay;

    public:
    int getMonth();
    void setMonth(int m);
    int getDay();
    void setDay(int d);
    int getHours();
    void setHours(int h);
    int getMinutes();
    void setMinutes(int min);
    int getSeconds();
    void setSeconds(int s);
    string getweekDay();
    void setweekDay(string wd);
    };

    int Date::getMonth() { return month; }
    void Date::setMonth(int m) {
    if(m >= 1 && m <=12) {
    month = m;
    return m;
    }
    }


    string Date::getweekDay() { return weekDay; }
    void Date::setweekDay(string wd) { weekDay = wd; }

    void main() {

    month = Date::getMonth(setmonth);

    then go into a for loop?

  9. #9
    E. Body
    Join Date
    Jan 2007
    Posts
    2,103
    BG Level
    7

    First, in your OP, you specified day of the week as an integer, but you implemented it as a string, which can lead to all kinds of issues if someone enters a string that isn't a day of the week (this is where enums are useful). Second, if you are implementing your two comparing functions in your class method (which you should), you already have the values you need to compare the two, just cycle through the ones that are pertinent to comparing a date and time.

  10. #10
    New Merits
    Join Date
    Jan 2008
    Posts
    211
    BG Level
    4

    Like someone else said, you really need to figure it out for yourself if you plan to be any good at this in the future. I haven't done C++ in years, but it shouldn't be that difficult. Reference your books and notes and I'm sure you'll get it.

  11. #11
    Cerberus
    Join Date
    Oct 2008
    Posts
    436
    BG Level
    4

    Quote Originally Posted by Eliseos View Post
    First, in your OP, you specified day of the week as an integer, but you implemented it as a string, which can lead to all kinds of issues if someone enters a string that isn't a day of the week (this is where enums are useful). Second, if you are implementing your two comparing functions in your class method (which you should), you already have the values you need to compare the two, just cycle through the ones that are pertinent to comparing a date and time.
    ok i changed the last part to:

    int Date::getweekDay() { return weekDay; }
    void Date::setweekDay(int wd) { weekDay = wd; }

    Second:

    i think best way to be to ask the user for the date? im not really sure what hes wanting but i dont think it matters, he just wants to see if we can do it. So maybe say like:


    void main() {
    cout << "Enter month:" << endl;
    cin >> month;
    rest of the day/time/seconds

    cout <<" Enter the new month:" <<endl;
    cin >> month2;
    rest of blabla
    }

    then before the main do like:

    if (month1 < month2) {
    cout >> Month1
    else:
    cout >> month2
    }

  12. #12
    Cerberus
    Join Date
    Oct 2008
    Posts
    436
    BG Level
    4

    Quote Originally Posted by Crisco View Post
    Like someone else said, you really need to figure it out for yourself if you plan to be any good at this in the future. I haven't done C++ in years, but it shouldn't be that difficult. Reference your books and notes and I'm sure you'll get it.
    we dont use a book. and i only have one source for notes and its a tic tak toe game. which i did use for what i have currently, but unsure how to fit the rest to fix the new program. Im not looking for the compleate code handed to me, just need pointed in the right direction and help seeing if im on right track or not.

  13. #13
    E. Body
    Join Date
    Jan 2007
    Posts
    2,103
    BG Level
    7

    What? What school are you going to? Also, what type of programming class is this (IE, is it part of the Comp Sci department?)?

  14. #14
    Cerberus
    Join Date
    Oct 2008
    Posts
    436
    BG Level
    4

    Quote Originally Posted by Eliseos View Post
    What? What school are you going to? Also, what type of programming class is this (IE, is it part of the Comp Sci department?)?
    Jacksonville State University

    and its cs321 intro to computer programing and yes part of the cs department

  15. #15
    E. Body
    Join Date
    Jan 2007
    Posts
    2,103
    BG Level
    7

    Sorry, but that sounds like a shitty CS department if that's how they are teaching the material. It's understandable why you are pretty lost on this.

    More questions: How does your teaching present the material in class? Does he give you stub code that you fill in, or are you writing it all from scratch (I know you said you got that code from a tic tac toe game, but did he give that to you, or did you write it?)

    I don't know how much they've taught you as to how c++ and object-oriented languages in general work, but what you're doing in your program, is creating an object called a date. Each date has special properties to it, the month, day, year, etc, which you are defining with the private member variables of those names. You can access these data members either directly if you are calling it from within the date class, or from outside the class using the get___ member functions. You should now be able to have a better handle on how to compare dates, using the private member variables. I can tell you it will be more efficient to compare larger time frames first, ie year -> month -> day, etc if it's less than or greater than you can simply return true or false, if it's equal, go to the next largest time frame.

  16. #16
    Cerberus
    Join Date
    Oct 2008
    Posts
    436
    BG Level
    4

    Quote Originally Posted by Eliseos View Post
    Sorry, but that sounds like a shitty CS department if that's how they are teaching the material. It's understandable why you are pretty lost on this.

    More questions: How does your teaching present the material in class? Does he give you stub code that you fill in, or are you writing it all from scratch (I know you said you got that code from a tic tac toe game, but did he give that to you, or did you write it?)

    I don't know how much they've taught you as to how c++ and object-oriented languages in general work, but what you're doing in your program, is creating an object called a date. Each date has special properties to it, the month, day, year, etc, which you are defining with the private member variables of those names. You can access these data members either directly if you are calling it from within the date class, or from outside the class using the get___ member functions. You should now be able to have a better handle on how to compare dates, using the private member variables. I can tell you it will be more efficient to compare larger time frames first, ie year -> month -> day, etc if it's less than or greater than you can simply return true or false, if it's equal, go to the next largest time frame.
    baiscy we go in sit down and he puts it on a projector and types it himself we just watch and listen, but thats about the extent of it. and basicy to comapre im going to do like

    if year 1 > 2
    return year1

    else if month 1 > 2
    return year 1

    else if day 1 > 2
    return year 1


    ext?

    and would it be better to use bolean's?

  17. #17
    E. Body
    Join Date
    Jan 2007
    Posts
    2,103
    BG Level
    7

    Yeah, the instructions say you need to return a boolean for that, so substitute the year for the proper bool value. You could even merge the two compare functions into one relatively easily.

    I'd suggest (if he doesn't go through it fast enough, that is) to write down the code that he types out, and write notes about what he explains it does. Also, C++ Reference [C++ Reference] is a great reference site to use. Does he at least release the code he uses in class online? Something tells me he doesn't.

  18. #18
    New Merits
    Join Date
    Jan 2008
    Posts
    211
    BG Level
    4

    I have to agree with Eliseos. To be almost 1/2 way through this semester and only know that much is a shame. And doing it without a book to reference is even worse. I feel bad for you and anyone else in that class w/o any prior programming experience. If I remembered more C++ syntax I could be of more assistance, although this seems fairly straightforward.

    - Define the default constructor (month, day, year, hour, min, sec)
    - From the looks of your instructions, I don't see this default date being used, unless the date you're prompting the user for is going to be compared to it.

    - Define your get/set methods (one for each as you have it, but you forgot the year)

    - Either define a compare method or use an inline compare condition in your while loop (seen later)

    - Define your main method
    - create any local variables you'll need for prompting the user and calling your get/set methods
    - There's a couple ways to go about getting the String version of the dayOfWeek. You can create an array of the string names and reference that when outputting and comparing your dates, or you can use a switch, putting the string into a local variable
    - Instantiate your default date
    - create a while loop which will:
    - prompt the user for each part of the date
    - call each set method with the user data
    - compare the dates of each object
    - output the dates and the result of the compare
    - continuing the loop as long as the user wishes to enter valid dates

    Not sure if some of this is beyond your knowledge, but it's the best I could come up with at the moment. If any of it helps, great. If not, good luck anyway.

  19. #19
    Cerberus
    Join Date
    Oct 2008
    Posts
    436
    BG Level
    4

    Quote Originally Posted by Eliseos View Post
    Yeah, the instructions say you need to return a boolean for that, so substitute the year for the proper bool value. You could even merge the two compare functions into one relatively easily.

    I'd suggest (if he doesn't go through it fast enough, that is) to write down the code that he types out, and write notes about what he explains it does. Also, C++ Reference [C++ Reference] is a great reference site to use. Does he at least release the code he uses in class online? Something tells me he doesn't.

    how did you ever guess! i'll look up how to do bolean loop i hate those lol i'll post what i get in a few

  20. #20
    E. Body
    Join Date
    Jun 2005
    Posts
    2,226
    BG Level
    7
    FFXI Server
    Caitsith

    I typed out a basic solution of what I see the class looking like based upon your description. You seem to have the basic portion down, but I left out my implementation of setting the Day and Month because if I gave a full solution it wouldn't help you learn it. >.< I implemented up to the ComesBefore / ComesAfter, but thats rather easy once you deal with populating the class from the user input.

    Code:
    #include <time.h>
    #include <stdio.h>
    #include <string.h>
    #include <iostream>
    
    using namespace std;
    
    class Date {
    
    public:
    
    enum DAY { Sunday = 0,
    					Monday,
    					Tuesday,
    					Wednesday,
    					Thursday,
    					Friday,
    					Saturday
    					};
    					
    enum MONTH { 	Janurary = 0,
    				February,
    				March,
    				April,
    				May,
    				June,
    				July,
    				August,
    				September,
    				October,
    				November,
    				December
    				};
    	Date();
    	Date(MONTH m, DAY d, short int y, short int h, short int m, short int s);
    	
    	MONTH GetMonth();			// Get Month of Date
    	void SetMonth(MONTH m);		// Set Month of Date
    	char* Date();				// Get String Rep of the Entire Date
    	
    	int GetDayOfTheMonth();
    	void SetDayOfTheMonth(short int dotm){if(dotm >=1 || dotm <= 31) dayofthemonth = dotm;};
    	
    	int GetYear(){return (year-1900);};
    	void SetYear(short int y){if(y >= 0 && year <= 2009) year = y;};
    	
    	int GetHours(){return hours};
    	void SetHours(short int h){if(h >= 0 || h <= 23) hours = h;};
    	
    	int GetMinutes(){return minutes};
    	void SetMinutes(short int m){if(m >= 0 || m <= 59) minutes = m;};
    	
    	int GetSeconds(){return seconds};
    	void SetSeconds(short int s){if(s >= 0 || s <= 59) seconds = s;};
    	
    	DAY GetDayOfTheWeek(){return day;};
    	void SetDayOfTheWeek(DAY d);
    	
    	bool ComesBefore(Date d);
    	bool ComesAfter(Date d);
    	
    private:
    	
    	MONTH month;
    	DAY day;
    	short int hours, minutes, seconds, year, dayofthemonth;
    }
    edit: I don't know how far along you are, but I typed this up rather quickly just to shed another perspective on how I would go about it. D: in CS, there are always (usually) many solutions to a problem (with some being faster). So anyway, there's my take on it.

    edit2: also, I assumed correct user input on days for each month as well as leap year. You could calculate if there are 31 days in the month etc, but I'd rather just assume it at this point since the code is nicer, lol, although it's not that difficult to add if it's necessary.

Page 1 of 2 1 2 LastLast

Similar Threads

  1. Simple HTML Coding Help
    By Pirian in forum General Discussion
    Replies: 11
    Last Post: 2010-09-17, 18:28
  2. javascript code help.
    By Bunnykillah in forum General Discussion
    Replies: 3
    Last Post: 2008-11-18, 22:57