6 Replies - 438 Views - Last Post: 30 October 2008 - 02:03 PM Rate Topic: -----

#1 camckee316  Icon User is offline

  • D.I.C Regular

Reputation: 15
  • View blog
  • Posts: 277
  • Joined: 29-August 08

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;
			
}



Is This A Good Question/Topic? 0
  • +

Replies To: Pick on me

#2 bodom658  Icon User is offline

  • Villiage Idiom
  • member icon

Reputation: 113
  • View blog
  • Posts: 1,123
  • Joined: 22-February 08

Re: Pick on me

Posted 30 October 2008 - 09:12 AM

you could make a string array of all the months, so you do not need to have a switch statement for every month, just one statement,

something like string months[12] = {"January", "February", etc...

then they will be usable like a normal array.
Was This Post Helpful? 0
  • +
  • -

#3 Hyper  Icon User is offline

  • Banned

Reputation: 108
  • View blog
  • Posts: 2,129
  • Joined: 15-October 08

Re: Pick on me

Posted 30 October 2008 - 01:09 PM

Here's an implemention of what bodom658 was saying:

    string month[365] = {
           "Invalid",
           
           "January 1st",
           "January 2nd",
           "January 3rd",
           "January 4th",
           "January 5th",
           "January 6th",
           "January 7th",
           "January 8th",
           "January 9th",
           "January 10th",
           "January 11th",
           "January 12th",
           "January 13th",
           "January 14th",
           "January 15th",
           "January 16th",
           "January 17th",
           "January 18th",
           "January 19th",
           "January 20th",
           "January 21st",
           "January 22nd",
           "January 23rd",
           "January 24th",
           "January 25th",
           "January 26th",
           "January 27th",
           "January 28th",
           "January 29th",
           "January 30th",
           "January 31st",

           "February 1st",
           "February 2nd",
           "February 3rd",
           "February 4th",
           "February 5th",
           "February 6th",
           "February 7th",
           "February 8th",
           "February 9th",
           "February 10th",
           "February 11th",
           "February 12th",
           "February 13th",
           "February 14th",
           "February 15th",
           "February 16th",
           "February 17th",
           "February 18th",
           "February 19th",
           "February 20th",
           "February 21st",
           "February 22nd",
           "February 23rd",
           "February 24th",
           "February 25th",
           "February 26th",
           "February 27th",
           "February 28th",
           "February 29th"
           };

    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;
    }
    
    for (int x = 1; x <= 365; x++) {
        if (x == num) {
              cout << "The date is " << month[x] << endl << endl;
        }
    }



Welcome, hope it helps.

PS: You may or may not need to modify it to get the same effect with all your switch statements, simple math with minus x and plus x will display "After" and "Prior to," with ease.

This post has been edited by Hyper: 30 October 2008 - 01:17 PM

Was This Post Helpful? 0
  • +
  • -

#4 Hyper  Icon User is offline

  • Banned

Reputation: 108
  • View blog
  • Posts: 2,129
  • Joined: 15-October 08

Re: Pick on me

Posted 30 October 2008 - 01:15 PM

Here, I did what I was talking about *for* you just because it's fun...

Also I just converted 226 lines of code into 8 lines -- The powers of a for loop are endless!

    for (int x = 1; x <= 365; x++) {
        if (x == num) {
              cout << "The date is " << month[x] << endl;
              cout << "The date is before " << month[x -= 1] << endl;
              cout << "The date after is " << month[x += 2] << endl;
              cout << "--------------------" << endl << endl;
        }
    }



Welcome!

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! :)
Was This Post Helpful? 0
  • +
  • -

#5 GWatt  Icon User is offline

  • member icon

Reputation: 264
  • View blog
  • Posts: 3,059
  • Joined: 01-December 05

Re: Pick on me

Posted 30 October 2008 - 01:21 PM

Instead of Making an array with every single day of the year, I would make an array for the month names and make an array that holds the number of days for the corresponding month.
string[] monthNames = {"January", . . .};
int[] monthDays = {31, . . . };
for (i = 0; i < 12; i++)
{
    if (dayOfYear - monthDays[i] <= 0)
    {
        cout << monthNames[i] << " " << monthDays[i] - dayOfYear + 1 << endl;
        break;
    }
    else
        dayOfYear -= monthDays[i];
}


Obviously that code doesn't handle leap years, or bad input, but it's short and to the point.
Was This Post Helpful? 0
  • +
  • -

#6 camckee316  Icon User is offline

  • D.I.C Regular

Reputation: 15
  • View blog
  • Posts: 277
  • Joined: 29-August 08

Re: Pick on me

Posted 30 October 2008 - 01:51 PM

we was not instructed to handle year. as for invalid input that is just a few keystrokes away
Was This Post Helpful? 0
  • +
  • -

#7 Hyper  Icon User is offline

  • Banned

Reputation: 108
  • View blog
  • Posts: 2,129
  • Joined: 15-October 08

Re: Pick on me

Posted 30 October 2008 - 02:03 PM

View Postcamckee316, 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.

This post has been edited by Hyper: 30 October 2008 - 02:04 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1