GIDForums  

Go Back   GIDForums > Computer Programming Forums > CPP / C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 25-Oct-2007, 06:14
Sosy Sosy is offline
New Member
 
Join Date: Oct 2007
Posts: 4
Sosy is on a distinguished road

Write the declaration for a Date object


the Question was:

a.Write the declaration for a Date object.

The user should be able to instantiate an object as the following:



Date date_1; //current date or Date date_2(01,02,2006);



The Date object should contain the following functions:

-Set the day, month and year. The values should be validated. (assume all months have 30 days)

-Get a string format of the date: day, month and year of the object in one function.

-Print the next date of the date object..

-Print the birth date in the format dd-mm-yyyy.

-Calculate age member function that takes one object parameter of type Date and return the difference between the invoking object (current date) and the passed object (birth date) as a number of days.

Test your class by creating 2 Date objects: today's date and your birth date, then write a menu that allows the user to use the above functions on your birth date i.e. the second date.


ive start solve it:

CPP / C++ / C Code:
class Date()
{
private:
	int day,month,year;//the current date.
public:

	Date(int d,int m,int y)
	{
		day=d;
		month=m;
		year=y;//the current day..
		}

void setDate()
{
	int d_birth,m_birth,y_birth;//date of birth..
	cout<<"enter ur birth day:";
	cin>>d_birth;

	if (d_birth<0 || d_birth>30)
		cout<<"enter ur birth day agin please:";
	cout<<"enter ur birth month:";
	cin>>m_birth;

	if (m_birth<0 || d_birth>12)
		cout<<"enter ur birth month agin please:";

	cout<<"enter ur birth year:";
	cin>>y_birth;

}

string getDate()
{
// i dont know how to convert from int to string :(
}


};


but idont know if i should use pointer or array..?
Last edited by admin : 26-Oct-2007 at 05:06. Reason: Please insert your C/C++ example codes between [CPP] and [/CPP] tags
  #2  
Old 25-Oct-2007, 09:24
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 440
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: could anyone help me please?


This is a simple way to convert it to a string using sprintf().
CPP / C++ / C Code:
string getDate()
{ // make it larger than 11 just in case you have large invalid numbers  
  char cString[64] = "";
   
  sprintf(cString,"%02d-%02d-%04d",month,day,year);
  return string(cString);
}
There might be a different way using some kind of << operator into a stream, then converting it to a string. I don't know.

Do you need to use an array or pointer for what?
  #3  
Old 25-Oct-2007, 20:54
Sosy Sosy is offline
New Member
 
Join Date: Oct 2007
Posts: 4
Sosy is on a distinguished road

Re: could anyone help me please?


i'll be right back :shy:
  #4  
Old 25-Oct-2007, 22:00
Sosy Sosy is offline
New Member
 
Join Date: Oct 2007
Posts: 4
Sosy is on a distinguished road

Re: could anyone help me please?


ok, this is the final answer.. and i hope its good :/


CPP / C++ / C Code:
#include <iostream>
#include <Cstring>

using namespace std;



class Date
{
   private:
	int day,month,year;//the current date.
   public:

	   int d_birth,m_birth,y_birth;//date of birth..

	Date(int d,int m,int y)
	{
		day=d;
		month=m;
		year=y;//the current day..
	}

void setDate()
{
	cout<<"enter ur birth day:";
	cin>>d_birth;

	if (d_birth<0 || d_birth>30)
		cout<<"enter ur birth day agin please:";
	cout<<"enter ur birth month:";
	cin>>m_birth;

	if (m_birth<0 || d_birth>12)
		cout<<"enter ur birth month agin please:";

	cout<<"enter ur birth year:";
	cin>>y_birth;

}


string getDate()
{ 
  char cString[64] = "";
   
  sprintf(cString,"%02d-%02d-%04d",month,day,year);
  return string(cString);
}

void nextDay()
{
	

    if (day>=30)
	{
		day=1;
      	month++;
	}
	else
		day++;

	if (month>12)
	{
		month=1;
	    year++;
	}

	cout<<day<<"-"<<month<<"-"<<year;
}

void print()
{

	if (day<10)
       cout<<"0"<<day;
	if (month<10)
		cout<<"0"<<month;

	cout<<day<<"-"<<month<<"-"<<year;

}

void age()
{

	int age=0,d_dif,m_dif,y_dif,d_m_dif,d_y_dif;

    d_dif=day - d_birth;
	m_dif=month - m_birth;
	y_dif=year - y_birth;

	d_m_dif=m_dif*30;
    d_y_dif=y_dif*360;

	age=d_dif + d_m_dif + d_y_dif;

	cout<<age;

}


};

int main()
{ 
	Date currnt(26,10,2007);
    int s;

	do
	{
    cout<<endl<<"Menu:"<<endl;
	cout<<"1-Set Date"<<endl;
	cout<<"2-Get string format"<<endl;
    cout<<"3-Print the next day"<<endl;
	cout<<"4-Print the birth day in the format dd-mm-yyyy"<<endl;
	cout<<"5-Calculate age"<<endl;
	cout<<"choose One of this menu or '-1' to exit:"<<endl;
	cin>>s;



	switch(s)
	{
	case 1:
		currnt.setDate();
		break;

    case 2:
	    currnt.getDate();
		break;

	case 3:
		currnt.nextDay();
		break;

	case 4:
		currnt.print();
		break;

	case 5:
		currnt.age();
		break;

	case -1:
		break;

	}
	}while (s != -1);

	return 0;
}

when i ask him to Get a string format he doesnt return anything..
is it the string library wrong?

hope u test this program and tell me if i need to fix something or not..
  #5  
Old 25-Oct-2007, 22:06
Sosy Sosy is offline
New Member
 
Join Date: Oct 2007
Posts: 4
Sosy is on a distinguished road

Re: could anyone help me please?


fakepoo

Quote:
sprintf().
could u please explain how it work, or what is it doing?

Quote:
Do you need to use an array or pointer for what?

i dont know, but i think there is way to acsess to the private varible by using pointer..

than u very much for ur help..
  #6  
Old 26-Oct-2007, 07:56
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 440
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: Write the declaration for a Date object


sprintf() is an old C function for formatting strings. Basically, if you had a variable called count that is of type int and you wanted to print a message that said "The count is X" where X is the value of count, you would have to use something like sprintf() when dealing with char*-type strings. Here are the basics:

sprintf( char* Destination, char* Format,[list of arguments, separated by commas)

Destination - the string to print into.
Format - the string containing the desired format.

When creating the format string, you can put any text you want. At the place where you want to insert the value of a variable, you put a % sign followed by a specifier. The specifier depends on the variable data type. For an integer, the specifier is 'd'.

So, for our example above, you would do it like this:
CPP / C++ / C Code:
int count = 6;
char dest[32] = "";
sprintf( dest, "The count is %d", count );
// now, dest = "The count is 6"

If you wanted to add more variables, then put another %[specifier] into the format string and the variable as an additional argument. For example:
CPP / C++ / C Code:
int oldCount = 6;
int newCount = 7
char dest[32] = "";
sprintf( dest, "The old count was %d, the new count is %d", oldCount, newCount );
// now, dest = "The old count was 6, the new count is 7"

For more information about using sprintf(), see:

http://www.cplusplus.com/reference/c...o/sprintf.html

and search "sprintf".

Good luck!
~fakepoo
  #7  
Old 26-Oct-2007, 09:21
Kimmo Kimmo is offline
Member
 
Join Date: Mar 2007
Location: Finland
Posts: 229
Kimmo has a spectacular aura aboutKimmo has a spectacular aura about

Re: Write the declaration for a Date object


Quote:
Originally Posted by fakepoo
For more information about using sprintf(), see:
For information on stringstream, search the same site for "stringstream."
__________________
Music, programming, endless learning..
 

Recent GIDBlog2nd Week of IA Training by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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
Forum Jump

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 06:28.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.