C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old June 16th, 2009, 10:04 AM
wicasacikala wicasacikala is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2009
Posts: 1 wicasacikala User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 49 m 31 sec
Reputation Power: 0
Classes - Classes and Arrays

I'm having trouble writing this program for a class. Here's the 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/2009
December 25, 2009
25 December 2009
Demonstrate the class by writing a complete program implementing it.
Input validation: Do not accept values for month less than 1 and greater than 12. Do not accept values for day less than 1 and greater than 31.

Here's what I have.

class Date
{
private:
int month;
int day;
int year;
public:
void setMonth(int m)
{ month = m; }
void setDay(int d)
{ day = d; }
void setYear(int y)
{ year = y; }

int getMonth() const
{ return month; }
int getDay() const
{ return day; }
int getYear() const
{ return year; }

Date()
Date(int, int,)
{ if (month > 12 || month < 1)
cout << "Please enter a number between 1 and 12 representing the month.";
if (day > 31 || day < 1)
cout << "Please enter a number between 1 and 31 representing the day."; }
Date(int, int, int)

void showDate1();
void showDate2();
void showDate3();
};

Now I'm stuck. I think I need to create an array and a ptr that will take the int for the month and point to the corresponding name of the month in the array. But I'm not sure how to write that. I'm probably overthinking everything and making it harder than it is.

Any help is greatly appreciated.

Aaron

Reply With Quote
  #2  
Old June 17th, 2009, 05:23 AM
MaHuJa MaHuJa is offline
Contributing User
Click here for more information.
 
Join Date: Dec 2007
Posts: 851 MaHuJa User rank is Private First Class (20 - 50 Reputation Level)MaHuJa User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 6 Days 15 h 29 m 24 sec
Reputation Power: 2
Send a message via Skype to MaHuJa Send a message via XFire to MaHuJa
Code:
static const char* monthstrings[] = {  // Can be a member of the class
  "", // 0
  "January", // 1
  "February",
  // etc etc
};
const char* Date::GetMonthText() { return monthstrings[month]; }


You can then have the functions making those strings call this one to know what text to put in.
__________________
Quote:
Programming by Coincidence
Fred types in some more code, tries it, and it still seems to work. [Then] the program suddenly stops working. [...] Fred doesn’t know why the code is failing because he didn’t know why it worked in the first place.

Reply With Quote
  #3  
Old June 17th, 2009, 05:52 AM
MaHuJa MaHuJa is offline
Contributing User
Click here for more information.
 
Join Date: Dec 2007
Posts: 851 MaHuJa User rank is Private First Class (20 - 50 Reputation Level)MaHuJa User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 6 Days 15 h 29 m 24 sec
Reputation Power: 2
Send a message via Skype to MaHuJa Send a message via XFire to MaHuJa
A couple issues with the code.

- After you successfully construct a Date object, you can setDay(12541) and setMonth(-50).

- After fixing that, you might want to make the constructors call the setX() functions to avoid duplicating the code. In particular: if you later are to add validation against 30th of february etc, that will give you a single place to update.

- I would suggest
#include <stdexcept>
and then, when the input validation fails, do
throw std::range_error();
This will force the program to either handle the fact it did something wrong, or terminate. Debuggers tend to allow you to stop right then and there too. Like being there when a car crashes vs figuring out what happened from only the wrecks.


- Error messages should (ideally) go out with cerr rather than cout - it keeps them separate in case of piping (moving your output into the input for another program) and you won't ever need to flush them.


-This is more than what the task requires, but it might help you in the future:
Code:
#include <sstream>
using namespace std;
string Date::get_mmddddyyyy() {
	stringstream s;
	s << day << ' ' << getMonthText() << ' ' << year;
	return s.str();
}


It's also easy to retrofit such functionality on an existing printing function: a parameter (ostream& cout = cout) where you can optionally pass it a stringstream.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Classes - Classes and Arrays


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump




 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

Request Your Free Technology Downloads!
 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

Request Your Free Technology Downloads!
 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

Request Your Free Technology Downloads!
 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

Request Your Free Technology Downloads!
 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

Request Your Free Technology Downloads!
 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 




© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
Stay green...Green IT