Display the names of the months using an index into a constant array - C++ Data Type

C++ examples for Data Type:Array

Description

Display the names of the months using an index into a constant array

Demo Code

#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;

const char * const pszMonths[] = {"void","January","February","March","April","May","June","July","August","September","October","November","December"};

const char* int2Month(int nMonth){
    if (nMonth < 1 || nMonth > 12){
        return "invalid";
    }//from   w w w.  j a va  2s  .  c o  m
    return pszMonths[nMonth];
}

int main(int nNumberofArgs, char* pszArgs[]){
    cout << "Enter the number of the month to display\n" << "(Enter a negative number to quit)" << endl;
    for(;;){
        cout << "Month?";
        int nMonth;
        cin >> nMonth;
        if (nMonth < 0)
        {
            break;
        }
        cout << "That would be "<<int2Month(nMonth)<<endl;
    }
    return 0;
}

Result


Related Tutorials