Pick on me

Posted 30 October 2008 - 08:44 AM

Ok even though this my 2 year of C++ I still have tons of trouble. I would to thank all of you for your knowledge and helping me understand the language better. Now time for you all to pick on me. I have a Day of The Year Program. It is to display just the month and date. My question is, Is there any way to shorten the code? Please critique .

#include <iostream>
using namespace std;
int main()
{
int num;
cout << "Enter a number 1 - 365: ";
cin >> num;
if (num == 0 || num >365)
{
cout << "The number " << num << " is not valid try again.\n";
cout << "Enter a number 1 - 365: ";
cin >> num;
}
if (num >=1 && num <=31)
switch (num)
{
case 1: //num = 1
cout << "The date is January 1\n";
cout << "The date before is December 31\n";
cout << "The date after is January 2\n";
break;
case 31: // num = 31
cout << "The date is January 31\n";
cout << "The date before is January 30\n";
cout << "The date after is Febuary 1\n";
break;
default:
cout << "The date is January " << num <<"\n";
cout << "The date before is January " << num - 1 <<"\n";
cout << "The date after is January " << num + 1 << "\n";
break;
}
if (num >=32 && num <= 59)
switch (num)
{
case 32:
cout << "The date is Febuary 1\n";
cout << "The date before is January 31\n";
cout << "The date after is Febuary 2\n";
break;
case 59:
cout << "The date is Febuary 28\n";
cout << "The date before is Febuary 27\n";
cout << "The date after is March 1\n";
break;
default:
cout << "The date is Febuary " << num - 31 << "\n";
cout << "The date before is Febuary " << num -32 << "\n";
cout << "The date after is Febuary " << num - 30 << "\n";
break;
}
if (num >= 60 && num <= 90)
switch(num)
{
case 60:
cout << "The date is March 1\n";
cout << "The date before is Febuary 28\n";
cout << "The date after is March 2\n";
case 90:
cout << "The date is March 31\n";
cout << "The date before is March 30\n";
cout << "The date after is April 1\n";
break;
default:
cout << "The date is March " << num - 59 << "\n";
cout << "The date before is March " << num - 60 << "\n";
cout << "The date after is March " << num - 58 << "\n";
break;
}
if (num >= 91 && num <=120)
switch(num)
{
case 91:
cout << "The date is April 1\n";
cout << "The date before is March 31\n";
cout << "The date after is April 2\n";
break;
case 120:
cout << "The date is April 30\n";
cout << "The date before is April 29\n";
cout << "The date after is May 1\n";
break;
default:
cout << "The date is April " << num - 90 << "\n";
cout << "The date before is April " << num - 91 << "\n";
cout << "The date after is April " << num - 89 << "\n";
break;
}
if (num >= 121 && num <= 151)
switch(num)
{
case 121:
cout << "The date is May 1\n";
cout << "The date before is April 30\n";
cout << "The date after is May 2\n";
break;
case 151:
cout << "The date is May 31\n";
cout << "The date before is May 30\n";
cout << "The date after is June 1\n";
break;
default:
cout << "The date is May " << num - 120 << "\n";
cout << "The date is May " << num - 121 << "\n";
cout << "The date in May " << num - 119 << "\n";
break;
}
if (num >= 152 && num <=181)
switch(num)
{
case 152:
cout << "The date is June 1\n";
cout << "The date before is May 31\n";
cout << "The date after is June 2\n";
break;
case 181:
cout << "The date is June 30\n";
cout << "The date before is June 29\n";
cout << "The date after is July 1\n";
break;
default:
cout << "The date is June " << num -151 << "\n";
cout << "The date before is June " << num - 152 << "\n";
cout << "The date after is June " << num - 150 << "\n";
break;
}
if (num >= 182 && num <= 212)
switch(num)
{
case 182:
cout << "The date is July 1\n";
cout << "The date before is June 30\n";
cout << "The date after is July 2\n";
break;
case 212:
cout << "The date is July 31\n";
cout << "The date before is July 30\n";
cout << "The date after is August 1\n:
break;
default:
cout << "The date is July " << num - 181 << "\n";
cout << "The date before is July " << num - 182 << "\n";
cout << "The date after is July " << num - 180 << "\n";
break;
}
if (num >= 213 && num <= 243)
switch(num)
{
case 213:
cout << "The date is August 1\n";
cout << "The date before is July 31\n";
cout << "The date after is August 2\n";
break;
case 243:
cout << "The date is August 31\n";
cout << "The date before is August 30\n";
cout << "The date after is September 1\n";
break;
default:
cout << "The date is August " << num - 212 << "\n";
cout << "The date is August " << num - 213 << "\n";
cout << "The date is August " << num - 211 << "\n";
break;
}
if (num >= 244 && num <= 273)
switch(num)
{
case 244:
cout << "The date is September 1\n";
cout << "The date before is August 31\n";
cout << "The date after is September 2\n";
break;
case 273:
cout << "The date is September 30\n";
cout << "The date before is September 29\n";
cout << "The date after is October 1\n";
break;
default:
cout << "The date is September " << num - 243 << "\n";
cout << "The date before is September " << num - 244 << "\n";
cout << "The date after is September " << num - 242 << "\n";
break;
}
if (num >= 274 && num <= 304)
switch(num)
{
case 274:
cout << "The date is October 1\n";
cout << "The date before is September 30\n";
cout << "The date after is October 2\n";
break;
case 304:
cout << "The date is October 31\n";
cout << "The date before is October 30\n";
cout << "The date after is November 1\n";
break;
default:
cout << "The date is October " << num - 273 << "\n";
cout << "The date before October " << num - 274 << "\n";
cout << "The date after is October " << num - 272 << "\n";
break;
}
if (num >= 305 && num <= 334)
switch(num)
{
case 305:
cout << "The date is November 1\n";
cout << "The date before is October 31\n";
cout << "The date after is November 2\n";
break;
case 334:
cout << "The date is November 30\n";
cout << "The date before is November 29\n";
cout << "The date after is December 1\n";
break;
default:
cout << "The date is November " << num -304 << "\n";
cout << "The date before is November " << num - 305 << "\n";
cout << "The date after is November " << num - 303 << "\n";
break;
}
if num >= 335 && num <= 365)
switch(num)
{
case 335:
cout << "The date is December 1\n";
cout << "The date before is November 30\n";
cout << "The date after is December 2\n";
break;
case 365:
cout << "The date is December 31\n";
cout << "The date before is December 30\n";
cout << "The date after is January 1\n";
default:
cout << "The date is December " << num - 334 << "\n";
cout << "The date before is December " << num - 335 << "\n";
cout << "The date after is December " << num - 333 << "\n";
break;
}
system("pause");
return 0;
}

The only exception is you'll have to build the string manually -- or if you seriously want to compress it, you could do math and based on the "day" of the month, have it append, "st", "nd", "rd", or "th" to the numbers.

Speaking of which, you could honestly further compact what I wrote even tiner by doing that and one other thing!

Re: Pick on me

Posted 30 October 2008 - 02:03 PM

camckee316, on 30 Oct, 2008 - 01:51 PM, said:

we was not instructed to handle year. as for invalid input that is just a few keystrokes away

I didn't post it, but if you change "Invalid" to "December 31st" then when you want January 1st, it'll say the before month is Dec. 31st, which would be proper (in the assumption that it isn't a leap year).

If my post was at all helpful, feel free to click on the "This Post Was Helpful!" link on the bottom of my post. It's appreciated if you do.