Thursday, July 30, 2009

C++ Month, Day, and Year Program?

I have to write a C++ program. This program ask for the date in mm dd yyyy form. Then it will output the all that in words. like 01 01 2007, it would output January First Two Thousand and Seven.


I am having an extremely hard time with this, and I thought I could figure it out myself. I have the Switch statements working for the month and day. I am having the most trouble with the year. I can't figure it out. This program has to be a loop unless


-1 is inputed. Also the leap year has to be in here somewhere. This is my forth week of class, and just overwhelmed right now. If someone class give me a hint or help with this.





Comment me back here, or my e-mail is jurzzy81@yahoo.com

C++ Month, Day, and Year Program?
If you are allowed to use arrays, you don't even need one switch statement, but otherwise you must have four switch statements.





Switch1. For printing the numbers one to nineteen


Switch2. For printing the months January to December


Switch3. For printing twenty (=2) ,thirty (=3) ,... to ninety


Switch4. For printing first, second, third ... nineteenth





You can write these yourself easily.





Now you can break the numbers down:





We have mm dd yyy which are all ints.





int digit1 , digit2 , half1 , half2;





For mm: Switch2 (mm); // January / February...





For dd:


if(dd %26lt; 20)


{


Switch4 (dd); // first / second,..


}


else


{


// split into digits


digit1 = dd / 10; // integer division loses remainder


digit2 = dd - (digit1 * 10); // remainder





Switch3 (digit1); // twenty / thirty...


Switch4 (digit2); // first / second...


}





For yyyy:





// split into pairs of digits


half1 = yyyy / 100; // same principle as above


half2 = yyyy - (half1 * 100);





// deal with half1 first





digit1 = half1 / 10; // integer division loses remainder


digit2 = half1 - (digit1 * 10); // remainder





if(digit2 == 0)


{


Switch1 (digit1);


cout %26lt;%26lt; " thousand"; // one thousand , two thousand...


}


else


{


if(half1 %26lt; 20)


{


Switch1 (half1); // one / two,..


}


else


{


Switch3 (digit1); // twenty


Switch1 (digit2); // one / two,..


}


cout %26lt;%26lt; " hundred"; //


}





// finally deal with half2





if(half2 %26gt; 0)


{


if(half1 %26gt; 0)


{


cout %26lt;%26lt; " and ";


}


if(half2 %26lt; 20)


{


Switch1 (half2); // one / two,..


}


else


{


digit1 = half2 / 10; // integer division loses remainder


digit2 = half2 - (digit1 * 10); // remainder





Switch3 (digit1);


Switch1 (digit2);


}


}





There are a couple of tricks in here, but fill in the functions for switch1 , switch2 and try out.





NOTE: For leap years, you only have to divide the last digit by four and check that. You will also have to adjust spaces between words a little, but that should be easy.
Reply:Figuring out the year in my thinking would go like this:


1) Determine how many characters are there in the year. So that you can figure out the range - thousands, hundreds, tens, or ones. (2007, 207, 27, 2). Now you know your ranges.


2) Have a function which returns the word for all the bits in the year. Like if you pass 2000 to it, it should return "two thousand", "two hundred" for 200.


3) Pass all the bits rounded off to closet factor of 1000, 100, 10 or 1 depending on the bit position. For example for year 1999, pass the following: 1000, 900, 90, 9.
Reply:You're right. Switch statement can solve the problem for days and months.


I suggest you to ( for years ) that:


divide the year number to two pieces. 2007 = 20 and 07


and use two more switch statements for both of them. But I think you should enter the words manually. Because if a 4 in the first digital place it may be "fourteen" or "twenty-four"


Maybe for left most 2 digital places you can divide them 2 another pieces.


Finally you have at most 6 switch statements. But like I said you should enter the words.


Good Luck
Reply:Any year less than 2000 is written "xxx hundred", while years %26gt;= 2000 are "two thousand". (This assumes your range of years is limited to less than 2100.) The lower two digits are always spoken numerically (twenty-seven, sixty-five) except for 2000 itself.





By the way, for the months, a cleaner, more elegant solution to a switch statement is to have an array containing the month names, then index into the array with the month ordinal.





Do you have to validate the data for illegal month and day values?


No comments:

Post a Comment