Functions

This is a discussion on Functions within the C++ Programming forums, part of the General Programming Boards category; Okay, I'm trying to make my code smaller and less ugly. The only problem is that I don't know how ...

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    2

    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.

  2. #2
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,904
    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

  3. #3
    the Wizard
    Join Date
    Aug 2004
    Posts
    109
    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
    -//Marc Poulsen -//MipZhaP

    He sat down, he programmed, he got an error...

  4. #4
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    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;
    }

  5. #5
    the Wizard
    Join Date
    Aug 2004
    Posts
    109
    Yes, that's a possibility to, they look more "read-friendly"
    -//Marc Poulsen -//MipZhaP

    He sat down, he programmed, he got an error...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Void Functions Help
    By bethanne41 in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2005, 06:30 PM
  2. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 12:50 PM
  3. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 03:35 AM
  4. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 02:41 PM
  5. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 06:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21