C++ examples for Data Type:Array
Display the names of the months using an index into a constant array
#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; }