473,379 Members | 1,252 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

about function in C++.....

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
8 3583
[please can somebody guides me???i dun want the answer,i juz want you all guys,guide me...thanks.....
Sep 21 '06 #2
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
9,065 Expert Mod 8TB
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
dush
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
9,065 Expert Mod 8TB
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
dush
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
D_C
293 100+
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
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

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

Similar topics

220
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have...
28
by: David MacQuigg | last post by:
I'm concerned that with all the focus on obj$func binding, &closures, and other not-so-pretty details of Prothon, that we are missing what is really good - the simplification of classes. There are...
3
by: googleboy | last post by:
Hi there. I have defined a class called Item with several (about 30 I think) different attributes (is that the right word in this context?). An abbreviated example of the code for this is: ...
3
by: Minh Khoa | last post by:
Please give me more information about delegate and its usage? Why do i use it and when?
15
by: Sam Kong | last post by:
Hello! I got recently intrigued with JavaScript's prototype-based object-orientation. However, I still don't understand the mechanism clearly. What's the difference between the following...
7
by: Yuri_Юрий | last post by:
I'm confused about the VARIABLE LENGTH ARRAYS. {scanf("%d",&n);float a;} In which compiler can I use it? I tried VC++6.0 SP6,but it's reported error:CONSTANT EXPRESSION! Another question, What...
68
by: James Dow Allen | last post by:
The gcc compiler treats malloc() specially! I have no particular question, but it might be fun to hear from anyone who knows about gcc's special behavior. Some may find this post interesting;...
3
by: Schizoid Man | last post by:
Hi, I'm a novice whose just about migrating to C++ after cutting my teeth on C for a few years. To start with I'm using Microsoft's out-of-the-box Visual C++ Express Edition compiler and I...
35
by: rebeccatre | last post by:
hi can Variant archiving setTimout('.. capability be done without using it? :-)
35
by: RobG | last post by:
Seems developers of mobile applications are pretty much devoted to UA sniffing: <URL: http://wurfl.sourceforge.net/vodafonerant/index.htm > -- Rob
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.