Hello citizen of Daniweb may I ask a little help from you guys please :)
see there's this program that our professor had asked us to make and It's about
pointers. as a newbie to this world I was able to (gladly) make it but i feel like it could be simplified further. may ye pro's and leets teach me a simpler alternative?
the problem is: Make a C++ program that prompts the user to enter a number from the display 1st –12th (1-12) and output the equivalent month. The display 1st – 12th and month must be initialised to a pointer array. Use pointer variable *no[ ] for the number and *month[ ] for the output month.
and my code is..
#include<iostream.h>
void main()
{ int num;
char no1[]="1st",no2[]="2nd",no3[]="3rd",no4[]="4th",no5[]="5th",no6[]="6th";
char no7[]="7th",no8[]="8th",no9[]="9th",no10[]="10th",no11[]="11th",no12[]="12th";
char month1[]="January",month2[]="Febuary",month3[]="March",month4[]="April",month5[]="May",month6[]="June";
char month7[]="July",month8[]="August",month9[]="September",month10[]="Otober",month11[]="Novermber",month12[]="December";
char *a, *b;
cout<<"Input a number from 1 - 12: ";
cin>>num;
switch(num)
{
case 1:
{
a = &no1[0];
b = &month1[0];
cout<<"It's the "<<a<<" month and it's "<<b<<"."<<endl;
}
break;
case 2:
{
a = &no2[0];
b = &month2[0];
cout<<"It's the "<<a<<" month and it's "<<b<<"."<<endl;
}
break;
case 3:
{
a = &no3[0];
b = &month3[0];
cout<<"It's the "<<a<<" month and it's "<<b<<"."<<endl;
}
break;
case 4:
{
a = &no4[0];
b = &month4[0];
cout<<"It's the "<<a<<" month and it's "<<b<<"."<<endl;
}
break;
case 5:
{
a = &no5[0];
b = &month5[0];
cout<<"It's the "<<a<<" month and it's "<<b<<"."<<endl;
}
break;
case 6:
{
a = &no6[0];
b = &month6[0];
cout<<"It's the "<<a<<" month and it's "<<b<<"."<<endl;
}
break;
case 7:
{
a = &no7[0];
b = &month7[0];
cout<<"It's the "<<a<<" month and it's "<<b<<"."<<endl;
}
break;
case 8:
{
a = &no8[0];
b = &month8[0];
cout<<"It's the "<<a<<" month and it's "<<b<<"."<<endl;
}
break;
case 9:
{
a = &no9[0];
b = &month9[0];
cout<<"It's the "<<a<<" month and it's "<<b<<"."<<endl;
}
break;
case 10:
{
a = &no10[0];
b = &month10[0];
cout<<"It's the "<<a<<" month and it's "<<b<<"."<<endl;
}
break;
case 11:
{
a = &no11[0];
b = &month11[0];
cout<<"It's the "<<a<<" month and it's "<<b<<"."<<endl;
}
break;
case 12:
{
a = &no12[0];
b = &month12[0];
cout<<"It's the "<<a<<" month and it's "<<b<<"."<<endl;
}
break;
default:
cout<<"Check your input only numbers from 1 - 2"<<endl;
break;
}
}
thanks in advance :)