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 :)

Those are not pointers. Here is how you make an array of pointers to strings. With this, your program has only one variable name to contend with, not 12. char *no[]={"1st","2nd","3rd","4th","5th","6th","7th","8th","9th","10th","11th","12th"}; Once you enter a value 1-12 you can easily display the string using that index value, but first you have to convert it to a value 0-11 because the first element of any array is 0, not 1. Then you don't need that switch statement at all. Below is the only line you need. cout<<"It's the "<<no[num-1]<<" month and it's "<<months[num-1]<<"."<<endl;

Edited 6 Years Ago by Ancient Dragon: n/a

gee thanks.. I knew it... come to think of it it does look ugly doesn't it :)
ok off to work thanks man :D

#include<iostream.h>
void main()
{	int num;
	char *no[]={"1st","2nd","3rd","4th","5th","6th","7th","8th","9th","10th","11th","12th"}; 
	char *month[]={"January","Febuary","March","April","May","June","July","August","September","October","November","December"};
	cout<<"Input a number from 1 - 12: ";
	cin>>num;
	cout<<"It's the "<<no[num-1]<<" month and it's "<<month[num-1]<<"."<<endl;
}

Look at that beauty.. :)
imagine all the leaves I can save if I were to print it on paper
can't thank you enough man :)
peace

Your program looks nice, but still has one big flaw. What will happen if I enter a 99 or a negative value? After cin you need to verify that num is between 1 and 12.

well yeah.. but we're still on basics so she ain't asking that much...
but come to think of it time will come and she will D:
mind showing me how? that would really be cool

This question has already been answered. Start a new discussion instead.