Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

What things can I do to improve my program? Criticism appreciated.

$
0
0
What are some things I should do to make this program more efficient, easier to read, etc.
I'm sure my program is pretty noobish but I would like to improve.

#include <iostream>
#include <string>
#include <sstream>
#include <conio.h>
using namespace std;

void displayMonth(int maxDays, int monthNum);

int main()
{
    short month = 0;
    string myStr;
    
    while (month <= 0 || month > 12) //Asks for user input until valid number recieved
    {
        int monthDays[12] = {31,28,31,30,31,30,31,31,30,31,30,31}; //Max number of days per month
        
        cout << "Enter the number for any month of 2013 (1-12): ";
        getline(cin, myStr);
        stringstream(myStr) >> month;
        
        switch (month)
        {
        case 1:
            displayMonth(monthDays[0], 1); //Second parameter represents the month number
            break;                         //Ex: 5 will display May 
        case 2:
            displayMonth(monthDays[1], 2);
            break;
        case 3:
            displayMonth(monthDays[2], 3);
            break;
        case 4:
            displayMonth(monthDays[3], 4);
            break;
        case 5:
            displayMonth(monthDays[4], 5);
            break;
        case 6:
            displayMonth(monthDays[5], 6);
            break;
        case 7:
            displayMonth(monthDays[6], 7);
            break;
        case 8:
            displayMonth(monthDays[7], 8);
            break;
        case 9:
            displayMonth(monthDays[8], 9);
            break;
        case 10:
            displayMonth(monthDays[9], 10);
            break;
        case 11:
            displayMonth(monthDays[10], 11);
            break;
        case 12:
            displayMonth(monthDays[11], 12);
            break;
        default:
            cout << "Sorry but you must enter 1-12, restarting...\n";
        }
    }
        
    getch();
    return 0;
}
     
void displayMonth(int maxDays, int monthNum) //Displays the month and days 
{                                            //recieves the month number and ammout of days per month
    short ctr = 0;
    short ctr2 = 0;
    
    string weeks[7] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat",};
    string monthName[13] = {"", "January", "February", "March", "April",
                           "May", "June", "July", "August",
                           "September", "October", "November", "December"};
    
    cout << endl << monthName[monthNum] << ":\n"; //displays the month name by recieving month number
    for (int i = 0; i <=6; ++i)
    {
        for (int j = 0,; j < 7; ++j)
        {
            if (ctr < 7) //used to display Sunday-Monday before displaying the days
            {
                cout << weeks[j] << "\t";
                ctr++;
            }
            else if (ctr2 < maxDays) //limits ctr2 under the number of days in a month
                cout << ++ctr2 << "\t";
        }
            cout << endl;
    }   
} 



Viewing all articles
Browse latest Browse all 51036

Trending Articles