Thursday, July 30, 2009

Switch Statement in C++?

#include %26lt;iostream%26gt;


#include%26lt;string%26gt;


#include%26lt;iostream%26gt;


using namespace std;


int mm, dd, yyyy;





void InputData();


void DateCalc();





int main ( )


{





InputData();


DateCalc();


return 0;


}








void InputData( )


{


cout%26lt;%26lt;"Enter a Date or (-1) to exit"%26lt;%26lt; endl;


cin%26gt;%26gt; mm %26gt;%26gt; dd %26gt;%26gt; yyyy;


cout%26lt;%26lt;endl%26lt;%26lt;endl;





}


void DateCalc()


{





switch(mm)


{


case '01' : cout%26lt;%26lt;"January";


break;


case '02' : cout%26lt;%26lt;"Febuary";


break;


case '03' : cout%26lt;%26lt;"March";


break;


case '04' : cout%26lt;%26lt;"April";


break;


case '05' : cout%26lt;%26lt;"May";


break;


case '06' : cout%26lt;%26lt;"June";


break;


case '07' : cout%26lt;%26lt;"July";


break;


case '08' : cout%26lt;%26lt;"August";


break;


case '09' : cout%26lt;%26lt;"September";


break;


case '10' : cout%26lt;%26lt;"October";


break;


case '11' : cout%26lt;%26lt;"November";


break;


case '12' : cout%26lt;%26lt;"December";


break;


}





cout%26lt;%26lt;"The date is: "%26lt;%26lt; mm ;


cout%26lt;%26lt;endl;

Switch Statement in C++?
You might need to change the values of case and remove leading zeros. '1' instead of 01.





In that case, you can't store '01' into an integer, it will automatically be a '1', so you need to use a different variable type if you need to use leading zeros.
Reply:Try:





void DateCalc()


{


cout%26lt;%26lt;"The date is: "%26lt;%26lt; mm ;





switch(atoi(mm))


{


case 1: cout%26lt;%26lt;"January"; break;


case 2: cout%26lt;%26lt;"Febuary"; break;


case 3: cout%26lt;%26lt;"March"; break;


case 4: cout%26lt;%26lt;"April"; break;


case 5: cout%26lt;%26lt;"May"; break;


case 6: cout%26lt;%26lt;"June"; break;


case 7: cout%26lt;%26lt;"July"; break;


case 8: cout%26lt;%26lt;"August"; break;


case 9: cout%26lt;%26lt;"September"; break;


case 10: cout%26lt;%26lt;"October"; break;


case 11: cout%26lt;%26lt;"November"; break;


case 12: cout%26lt;%26lt;"December"; break;


}





cout%26lt;%26lt;endl;
Reply:Do't write '01' just write 01 (because of int )
Reply:#include %26lt;iostream.h%26gt;


#include %26lt;iomanip.h%26gt;


#include %26lt;string.h%26gt;





int mm,dd,yyyy;





void InputData();


char* DateCalc();





int main()


{


cout %26lt;%26lt; "Enter a data (mm dd yyyy) or -1 to exit" %26lt;%26lt; endl;


cin %26gt;%26gt; mm %26gt;%26gt; dd %26gt;%26gt; yyyy;


cout %26lt;%26lt; endl %26lt;%26lt; endl;





cout %26lt;%26lt; "The date is: " %26lt;%26lt; DateCalc() %26lt;%26lt; " " %26lt;%26lt; dd %26lt;%26lt; " " %26lt;%26lt; yyyy %26lt;%26lt; end


l;


}





char* DateCalc ()


{





switch (mm)


{


case 1:


return "January";


case 2:


return "Feburary";


case 3:


return "March";


case 4:


return "April";


case 5:


return "May";


case 6:


return "June";


case 7:


return "July";


case 8:


return "August";


case 9:


return "September";


case 10:


return "October";


case 11:


return "November";


case 12:


return "December";


default:


return "Unknown";


}


}
Reply:There are two problems with your code.


1.For 'case' you are using character instead of integer. The character '1' actually will result in case 49 which is the ascii value of '1'. So this case never gets selected. What you see is only integer date thanks to the last statement of you program,


2. You are using multi-character character constant.


gcc gives this warning.





Corrected code snippet:





switch(mm)


{


case 1: cout %26lt;%26lt; "January"


break;


.


.


.


case 12:cout%26lt;%26lt;"December";


break;


}

nobile

No comments:

Post a Comment