By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,852 Members | 2,152 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,852 IT Pros & Developers. It's quick & easy.

about function in C++.....

P: 12
how r u..hope that u r fine..thanks again 4 ur and everybody's help..it really help...(*_*).
By da way,again i have another question.hope u dun mind to teach me coz i need a guide to success and i have learn from a successful guy like u..

1.write a top down design and a C++ program that reads a date in numeric form and prints in english..
For example:
enter a date in the form dd mm yy
27 10 42
twenty-seven October,nineteen hundred forty two

The program should work for any date in the twentieth centery and should print an error message for any invalid date such as 29 2 83(1983 was'nt a leap year).
and i should read the date as one input.....

i want to learn step by step then, i can send my homework to you,guys thus,i can learn from my mistakes coz i am still new in programming....thanks for your guide.......
Sep 20 '06 #1
Share this Question
Share on Google+
8 Replies


P: 12
[please can somebody guides me???i dun want the answer,i juz want you all guys,guide me...thanks.....
Sep 21 '06 #2

P: 12
1.write a top down design and a C++ program that reads a date in numeric form and prints in english..
For example:
enter a date in the form dd mm yy
27 10 42
twenty-seven October,nineteen hundred forty two

The program should work for any date in the twentieth centery and should print an error message for any invalid date such as 29 2 83(1983 was'nt a leap year).
and i should read the date as one input.....

i want to learn step by step then, i can send my homework to you thus,you can tell me where i wrong,thus i can correct and learn from my mistakes coz i am still new in programming....thanks for your guide.......
Sep 21 '06 #3

Banfa
Expert Mod 5K+
P: 8,916
We aren't doing your home work for you, we will help you correct mistakes in it.

Start by writing a program that asks the user to input the date in numeric format, the day, month and year.

Once you can enter these values and successfully print them then consider how you might change that to a text format.

Write some code and post it here.
Sep 21 '06 #4

P: 27
Hi Nor farhana yaakub

You are lucky guy. I have written whole program for you, but next time you do it yourself ok?

Here is the code:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. char* int0_9ToStr(int i, bool ordinal) {
  5.   char* n[] = {" ", "one", "two", "three" ,"four", "five", "six", "seven", "eight", "nine"};
  6.   char* no[] = {" ", "first ", "second ", "thrird " ,"fourth ", "fifth ", "sixth ", "seventh ", "eight ", "ninth "};    
  7.   return (ordinal ? no[i] : n[i]) ;
  8. }
  9.  
  10. char* int10_19ToStr(int i, bool ordinal) {
  11.   char* n[] = {"ten", "eleven", "twelve" ,"thirteen", "fourteen", "fiveteen", "sixteen", "seventeen", "eighteen", "nineteen"};  
  12.   char* no[] = {"tenth ", "eleventh ", "twelfth " ,"thirteenth ", "fourteenth ", "fiveteenth ", "sixteenth", "seventeenth ", "eighteenth ", "nineteenth "};    
  13.   return (ordinal ? no[i-10] : n[i-10]) ;
  14. }
  15.  
  16. char* int0_90ToStr(int i, bool ordinal) {
  17.   char* n[] = {"", "", "twenty", "thirty", "forty" ,"fifty", "sixty", "seventy", "eighty", "ninety"};    
  18.   char* no[] = {"", "", "twentieth", "thirtieth"};    
  19.   return (ordinal ? no[i] : n[i]) ;
  20. }
  21.  
  22. void printDay(int d) {
  23.   cout << (10<=d && d<=19 ? int10_19ToStr(d, 1) : (cout << (d%10 ? int0_90ToStr(d/10, 0) : int0_90ToStr(d/10, 1)), int0_9ToStr(d%10, 1)));
  24. }
  25.  
  26. int main ()
  27. {
  28.   int year, month, day;
  29.  
  30.   cout << "Enter valid twetieth century date in numeric form: ";
  31.   cin >> day >> month >> year;  
  32.   cout << endl;
  33.  
  34.   if (0<=year && year<100)  
  35.   switch (month)
  36.   {
  37.     case 1: if (0<day && day<=31) {printDay(day); cout << "january";}
  38.             else {cout << "Error: January has 31 days.\n"; exit(1);}
  39.             break;
  40.     case 2: if (0<day && ((day<=29 && !(year%4)) || (day<=28 && (year%4))))
  41.             {printDay(day); cout << "february";}
  42.             else if (!(year%4)) {cout << "Error: February has 29 days in year " << year+1900 << ".\n"; exit(1);}
  43.             else {cout << "Error: February has 28 days in year " << year+1900 << ".\n"; exit(1);}
  44.             break;
  45.     case 3: if (0<day && day<=31) {printDay(day); cout << "march";}
  46.             else {cout << "Error: March has 31 days.\n"; exit(1);}
  47.             break;
  48.     case 4: if (0<day && day<=30) {printDay(day); cout << "april";}
  49.             else {cout << "Error: April has 30 days.\n"; exit(1);}
  50.             break;
  51.     case 5: if (0<day && day<=31) {printDay(day); cout << "may";}
  52.             else {cout << "Error: May has 31 days.\n"; exit(1);}
  53.             break;
  54.     case 6: if (0<day && day<=30) {printDay(day); cout << "june";}
  55.             else {cout << "Error: June has 30 days.\n"; exit(1);}
  56.             break;
  57.     case 7: if (0<day && day<=31) {printDay(day); cout << "july";}
  58.             else {cout << "Error: July has 31 days.\n"; exit(1);}
  59.             break;
  60.     case 8: if (0<day && day<=31) {printDay(day); cout << "august";}
  61.             else {cout << "Error: August has 31 days.\n"; exit(1);}
  62.             break;
  63.     case 9: if (0<day && day<=30) {printDay(day); cout << "september";}
  64.             else {cout << "Error: September has 30 days.\n"; exit(1);}
  65.             break;
  66.     case 10: if (0<day && day<=31) {printDay(day); cout << "october";}
  67.             else {cout << "Error: October has 31 days.\n"; exit(1);}
  68.             break;
  69.     case 11: if (0<day && day<=30) {printDay(day); cout << "november";}
  70.             else {cout << "Error: November has 30 days.\n"; exit(1);}
  71.             break;
  72.     case 12: if (0<day && day<=31) {printDay(day); cout << "december";}
  73.             else {cout << "Error: December has 31 days.\n"; exit(1);}
  74.             break;
  75.     default : cout << "Error: Entered month is invalid."; exit(1); break; 
  76.   }
  77.   else {cout << "Error: Entered year is out of twentieth century."; exit(1);}
  78.  
  79.   cout << " nineteen hundred ";
  80.   cout << (10<=year && year<=19 ? int10_19ToStr(year, 0) : (cout << int0_90ToStr(year/10, 0), int0_9ToStr(year%10, 0))) << endl;
  81.  
  82.   return 0;
  83. }
  84.  
by the way, dont anybody know about job for C++ programmer?
Sep 21 '06 #5

Banfa
Expert Mod 5K+
P: 8,916
Nice code but 1 small error in it.

The year 1900 was not a leap year so February only had 28 days in it, unfortunately your code passes 29th February 1900 has a valid date which it is not.
Sep 21 '06 #6

P: 27
I didnt know that. Why is not 1900 leap if 2000 leap is?

anyway, the probability for user to enter such a date is 1:36500, however I believe you Banfa that 1900 is not leap and here is the correction:

Expand|Select|Wrap|Line Numbers
  1.    case 2: if (0<day && ((day<=29 && !(year%4) && year) || (day<=28 && (year%4 || !year))))
  2.             {printDay(day); cout << "february";}
  3.             else if (!(year%4) && year) {cout << "Error: February has 29 days in year " << year+1900 << ".\n"; exit(1);}
  4.             else {cout << "Error: February has 28 days in year " << year+1900 << ".\n"; exit(1);}
  5.             break;
there is also mistake in word "twetieth" in prompt
Sep 21 '06 #7

100+
P: 293
D_C
Expand|Select|Wrap|Line Numbers
  1. if((year % 4) == 0)
  2.   leap = true;
  3. if((year % 100) == 0)
  4.   leap = false;
  5. if((year % 400) == 0)
  6.   leap = true;
That's because each year, on average, is about 365 + 1/4 - 1/100 + 1/400, or approximately 365.2425 days/year.
Sep 21 '06 #8

P: 12
thanks for your help..i dun need you to write the source code for me,coz it makes i become not satisfed with myself...i am juz learning programming within 3months.. by da.. way,thanxs.
i have found the internet by myself about the calculation of the leap year.....

..Ordinal Dates
Ordinal Dates, popularly but incorrectly called Julian Dates (or Julian Calendar), provide a year and the number of days into that year. 2000-January-01 is 2000-001. 1999-December-31 is 1999-365.

To translate between standard Gregorian Dates and Ordinal Dates the following code fragment can provide a helpful model:


short OrdinalDays[] = {
/* days (0-364 or 0-365) BEFORE each month */
0,31,59,90,120,151,181,212,243,273,304,334,365,
0,31,60,91,121,152,182,213,244,274,305,335,366
};
short LeapSw; /* 0 if not a leap year, 13 if leap year */

LeapSw = ( Year%4 || (!(Year%100) && Year%400)) ? 0 : 13;To translate a Gregorian Month (1-12) and Day (1-31), into an Ordinal day number (0-364 or 0-365) for a given Year:

Ordinal = OrdinalDays[Month-1+LeapSw] + Day-1;Note how LeapSw directs the array references to the first or last half of OrdinalDays where the first half is for normal years and the second half is for leap years.

To determine the number of days in the current Year:

DaysInYear = OrdinalDays[12+LeapSw];Note that the OrdinalDays table has 13 month values in it for each type of year just to allow this subscript to work.

To determine the number of days in the current Gregorian Month:

DaysInMonth = OrdinalDays[Month+LeapSw]
- OrdinalDays[Month-1+LeapSw];
Sep 22 '06 #9

Post your reply

Sign in to post your reply or Sign up for a free account.