Page 2 of 2 FirstFirst 1 2
Results 21 to 32 of 32

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

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

    is this the right format?

    bool year = true;
    while (year) {
    year1 > year 2
    }?

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

    Quote Originally Posted by Skie View Post
    is this the right format?

    bool year = true;
    while (year) {
    year1 > year 2
    }?
    That will just loop infinitely. You don't need to loop for that, you just call getYear(date1) > getYear(date2).

  3. #23
    Relic Horn
    Join Date
    Oct 2005
    Posts
    3,132
    BG Level
    7
    FFXI Server
    Unicorn
    WoW Realm
    Shattered Hand

    Either your teacher is awful or you need to switch majors to philosophy with a quickness if its this late in the semester and you're making those mistakes.

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

    Quote Originally Posted by Skie View Post
    is this the right format?

    bool year = true;
    while (year) {
    year1 > year 2
    }?
    This is evidence your teacher has not even taught you to pseudocode yet and that's very unfortunate. The point was that you should compare the years first, then the months if the years are equal, then the days of the month if the months are equal, etc. etc.

    More practically you'd save the timestamp returned from the library function internally and just to an integer comparison but I'm guessing that's maybe not what your teacher wants you to do.

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

    man i am so lost in this project ><

    i'll reply back in a bit see what i come up with. should have posted this earler lol due in 2.5 hours

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

    Quote Originally Posted by Skie View Post
    man i am so lost in this project ><

    i'll reply back in a bit see what i come up with. should have posted this earler lol due in 2.5 hours
    Another programming tip: start early rather than later. So many unforeseen problems can surface, especially in special cases that you possibly didn't consider when you first wrote your code. And if you aren't using an awesome compiler like IntelliJ, you could potentially be missing something as little as a ; and not even know it.

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

    he has us using borland compiler

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

    Here's the pseudocode, I didn't really give it more than a quick glance so it may not be 100% semantically correct but you should get the idea.

    Code:
    before
    
    if (this object's year == input object's year)
    {
    	if (this object's month == input object's month)
    	{
    		return (this object's day of the month < input object's day of the month);
    	}
    	return (this object's month < input object's month);
    }
    
    return (this object's year < input object's year);
    after is just the reverse

    Sneaky way that violates encapsulation but is much much quicker lol

    Code:
    return (this object's unix timestamp < input object's unix timestamp);
    but this would require you to construct the timestamp in the non-default constructor and that may not fly with your prof.

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

    ty everyone for your help submitted what i had.

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

    He went threw it in class with us... half the stuff he went and got off google; rest we've never heard of like swiches but explained them while doing them.

    Code:
    #include <iostream>
    #include <string>
    #include <sstream>
    #include <ctime>
    
    using namespace std;
    
    enum Month {JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC};
    enum DayOfWeek {SUN, MON, TUE, WED, THU, FRI, SAT};
    
    class Date {
    	private:
    		int hours;
    		int minutes;
    		int seconds;
    		int year;
    		int month;
    		int day;
    		
    		int compareTo(Date d);
    		
    	public:
    		Date();
    		int getHours();
    		void seetHours(int h);
    		int getMinutes();
    		void setMinutes(int m);
    		int getSeconds();
    		void setSeconds(int s);
    		int getYear();
    		void setYear(int y);
    		Month getMonth(Month m);
    		int getDay();
    		void setDay(int d);
    		
    		DayOfWeek getDayOfWeek ();
    		string toString();
    		bool before(Date d);
    		bool after(Date d);
    };
    
    
    Date::Date() {
    	time_t ltime;
    	struct tm *Tm;
    	ltime = time(NULL);
    	Tm = localtime(&ltime);
    	hours = Tm->tm_hour;
    	minutes = Tm->tm_min;
    	seconds = Tm->tm_sec;
    	year = Tm->tm_year;
    	month = Tm->tm_mon;
    	day = Tm->tm_mday;
    }
    
    int Date::getHours() { return hours; }
    int Date::getMinutes() { return minutes; }
    int Date::getSeconds() { return seconds; }
    int Date::getYear() { return year; }
    Month Date::getMonth() { return (Month)month; }
    int Date::getDay() { return day; }
    
    void Date::setHours(int h) { if(h >= 0 && h < 24) hours = h; }
    void Date::setMinutes(int m) { if(m >= 0 && m < 60) minutes = m; }
    void Date::setSeconds(int s) { if(s >0== 0 && s < 60) seconds = s; }
    void Date::setYear(int y) { if(y >= 0) year = y; }
    void Date::setMonth(Month m) { month = m; }
    void Date:setDay(int d) { if(d >= 0 && d < 32) day= d; }
    
    DayOfWeek Date::getDayOfWeek() {
    	return SUN;
    }
    	
    string Date::toString() {	
    	stringstream s;
    	string smonth;
    	swich(month) {
    		case 0: smonth = "January"; break;
    		case 1: smonth = "Febuary"; break;
    		case 2: smonth = "March"; break;
    		case 3: smonth = "April"; break;
    		case 4: smonth = "May"; break;
    		case 5: smonth = "June"; break;
    		case 6: smonth = "July"; break;
    		case 7: smonth = "August"; break;
    		case 8: smonth = "September"; break;
    		case 9: smonth = "October"; break;
    		case 10: smonth = "November"; break;
    		case 11: smonth = "December"; break;
    	}
    	s << smonth << " " << (day) << ", " << (year + 1900) << " ";
    	s << hours << " : " << minutes << " : " << seconds;
    }
    
    
    bool Date::compareTo(date d) {
    	if(year < d.year) return -1;
    	else if(year > d.year) return 1;
    	else if(month < d.month) return -1;
    	else if(month > d.month) return 1;
    	else if(day < d.day) return -1;
    	else if(day > d.day) return 1;
    	else if(hours < d.hours) return -1;
    	else if(hours > d.hours) return 1;
    	else if(minutes < d.minutes) return -1;
    	else if(mintues > d.minutes) return 1;
    	else if(seconds < d.seconds) return -1;
    	else if (seconds> d.seconds) return 1;
    	else return 0;
    	
    bool Date::before(Date d) {	return compareTo(d) < 0; }
    bool Date::after(Date d) { return comopareto(d) > 0; }
    	
    void main() {
    	Date d;
    	cout << d.toString() << endl;
    }

  11. #31
    Canada
    Join Date
    Oct 2006
    Posts
    1,482
    BG Level
    6
    FFXIV Character
    Mlle Skjie
    FFXIV Server
    Hyperion
    FFXI Server
    Sylph
    WoW Realm
    Madoran

    Your teacher sounds incredibly shitty if he's teaching things after the assignment is handed in...

  12. #32
    Relic Weapons
    Join Date
    Jul 2008
    Posts
    364
    BG Level
    4
    FFXI Server
    Bismarck

    Thats a 300 level course in the CS department and you are just now learning switches? What the fuck. (Not trying to call you stupid in any way, it sounds like the department at your school is a bit retarded). We had a full year of required intro programming classes before we took any upper division courses (and switches were in the very first one).

Page 2 of 2 FirstFirst 1 2

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