-
Functions
Okay, I'm trying to make my code smaller and less ugly. The only problem is that I don't know how to go about doing that. I've tried to set each entry with an appropriate response but it doesn't compile right. I decided to scratch that and just go with what I had.
Code:
#include <iostream.h>
using namespace std;
int main ()
{
int year;
int month;
int day;
cout << "When is your birthday? (mm/dd/yyyy); ie. 04/08/1985" << endl;
cin >> month;
cout << "/";
cin >> day;
cout << "/";
cin >> year;
cout << "You were born ";
if (month == 1)
{
cout << "January ";
}
if (month == 2)
{
cout << "February ";
}
if (month == 3)
{
cout << "March ";
}
if (month == 4)
{
cout << "April ";
}
if (month == 5)
{
cout << "May ";
}
if (month == 6)
{
cout << "June ";
}
if (month == 7)
{
cout << "July ";
}
if (month == 8)
{
cout << "August ";
}
if (month == 9)
{
cout << "September ";
}
if (month == 10)
{
cout << "October ";
}
if (month == 11)
{
cout << "November ";
}
if (month == 12)
{
cout << "December ";
}
if (day == 1)
{
cout << "1st, ";
}
if (day == 2)
{
cout << "2nd, ";
}
if (day == 3)
{
cout << "3rd, ";
}
if (day == 4)
{
cout << "4th, ";
}
if (day == 5)
{
cout << "5th, ";
}
if (day == 6)
{
cout << "6th, ";
}
if (day == 7)
{
cout << "7th, ";
}
if (day == 8)
{
cout << "8th, ";
}
if (day == 9)
{
cout << "9th, ";
}
if (day == 10)
{
cout << "10th, ";
}
if (day == 11)
{
cout << "11th, ";
}
if (day == 12)
{
cout << "12th, ";
}
if (day == 13)
{
cout << "13th, ";
}
if (day == 14)
{
cout << "14th, ";
}
if (day == 15)
{
cout << "15th, ";
}
if (day == 16)
{
cout << "16th, ";
}
if (day == 17)
{
cout << "17th, ";
}
if (day == 18)
{
cout << "18th, ";
}
if (day == 19)
{
cout << "19th, ";
}
if (day == 20)
{
cout << "20th, ";
}
if (day == 21)
{
cout << "21st, ";
}
if (day == 22)
{
cout << "22nd, ";
}
if (day == 23)
{
cout << "23rd, ";
}
if (day == 24)
{
cout << "24th, ";
}
if (day == 25)
{
cout << "25th, ";
}
if (day == 26)
{
cout << "26th, ";
}
if (day == 27)
{
cout << "27th, ";
}
if (day == 28)
{
cout << "28th, ";
}
if (day == 29)
{
cout << "29th, ";
}
if (day == 30)
{
cout << "30th, ";
}
if (day == 31)
{
cout << "31st, ";
}
if ((month == 2) && (day == 29))
{
cout << "So technically your only "<< (2004 - year) / 4 <<" years old, being born in";
}
if ((month == 2) && ((day == 30) || (day == 31)))
{
cout << "That's impossible, you moron!" << endl;
}
if ((month == 4) && (day == 31))
{
cout << "That's impossible, you moron!" << endl;
}
if ((month == 6) && (day == 31))
{
cout << "That's impossible, you moron!" << endl;
}
if ((month == 8) && (day == 31))
{
cout << "That's impossible, you moron!" << endl;
}
if ((month == 10) && (day == 31))
{
cout << "That's impossible, you moron!" << endl;
} else {
cout << year << endl;
}
system("PAUSE");
return 0;
}
See what I mean? It's ugly and repetitive. How would I go about setting each possible entry with the correct response. Having 30+ if statements makes me shudder.
-
Use an array:
Code:
char months[12][20]=
{
"January ",
"February ",
// Etc....
};
and do that for each one, then just access each string like so:
months[0]
to access January, and, well, you should be able to figure out the rest
-
Yes, and then also put it in a seperate file, then It's also going to be more easy for yourself figuring it out, if it suddenly turns into some kind of big project :)
-
When writing a program like that, switch statements are your friend.
Code:
switch (day)
{
case 1: cout << "1st"; break;
case 2: cout << "2nd"; break;
...
default: cout << "Invalid Value"; break;
}
-
Yes, that's a possibility to, they look more "read-friendly" :)