Question:
Can somebody help with this c++ programming question?
Sparto
2009-08-23 02:35:11 UTC
I have to write a switch statement with a function called no_in_month,that outputs the number of days for any month.For example if the input is 1 is passed to this function the output is 31(month 1 has 31 days),if 2 is passed the output is 28 and so on.

Thanks in advance.
Four answers:
Kras
2009-08-23 03:29:49 UTC
Hi,

here is a complete program, so you can compile and run/test.

I've taken a bit different approach with the case statements: combined the similar ones :)



#include

using namespace std;



void no_in_month(int month)

{

switch (month)

{

case 1 :

case 3 :

case 5 :

case 7 :

case 8 :

case 10 :

case 12 : cout<<"31 days \n";

break;

case 4 :

case 6 :

case 9 :

case 11 : cout<<"30 days \n";

break;

case 2 : cout<<"28 days \n";

break;

default : cout<<"Please provide a month number betwee 1-12.\n";

break;

}

}



int main(void)

{

int month = 0;

cout << "please enter a month number: ";

cin >> month;

no_in_month (month);



return 0;

}
Ciaron
2009-08-24 12:56:49 UTC
The function would require the month number as a parameter and return an integer value. So the function would look like this



int no_in_month (int month_no)

{ switch (month_no)

{ // the months with 31 days would return the value 31

case 1:

case 3:

case 5:

case 7:

case 8:

case 10:

case 12: return 31;

// the months that have 30 days would return the value 30

case 4:

case 6:

case 9:

case 11: return 30;

// the case for February is 28 though a flag could be done to pick up if it //is a leap year and returns 29 instead

case 2: return 28;

// the default case can return -1 as an error

default : return -1;

}

}
2009-08-23 09:58:48 UTC
firstly u will declare the fucntion above the voud main like this

int no_in_month (int,in,int);

void main()

{

here u call the function

no_in_month(a,b,c);

}

int FindString()

{

here u can use the swith stament

switch(choice);

case 'a';

{

cout<<"press 1 for how many days in the first month";

if(x==1)

{

cout<<"there are 31 days in this month"<
}

}

}

if u have any problem mail on my addres iw ill give u the solution

richard.george44@yahoo.com
Syed Ali Raza
2009-08-23 09:45:43 UTC
void no_in_month(int month)

{

switch (month)

{

case 1 : cout<<"31 days \n";break;

case 2 : cout<<"30 days \n";break;

case 3 : cout<<"31 days \n";break;

case 4 : cout<<"30 days \n";break;

case 5 : cout<<"31 days \n";break;

case 6 : cout<<"30 days \n";break;

case 7 : cout<<"31 days \n";break;

case 8 : cout<<"31 days \n";break;

case 9 : cout<<"30 days \n";break;

case 10 : cout<<"31 days \n";break;

case 11 : cout<<"30 days \n";break;

case 12 : cout<<"31 days \n";break;

default : cout<<"Not a valid input..Please enter (1-12)..\n";break;

}

}


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...