Answer to Question #53154 in C++ for farah

Question #53154
i'm solving in uri online judge but i have a wrong problem please tell me where is the wrong (i'm a beginner programmer this is the link of the problem https://www.urionlinejudge.com.br/judge/en/problems/view/1052 and this is the code #include <iostream> using namespace std; int F; int main () { cin >> F; if (F==1) cout <<"January\n"; else if (F==2) cout <<"February\n"; else if (F==3) cout <<"March\n"; else if (F==4) cout <<"April\n"; else if (F==5) cout <<"May\n"; else if (F==6) cout <<"June\n"; else if (F==7) cout <<"July\n"; else if (F==8) cout <<"August\n"; else if (F==9) cout <<"September\n"; else if (F==10) cout <<"October\n"; else if (F==11) cout <<"November\n"; else (F==12); cout <<"December"<<endl; return 0; }
Expert's answer
If you want to write few instructions you should use {} braces like this: If(case) {operator1; operator2;} . But it is not comfortable. Better use switch operator:

#include <iostream>
using namespace std;

int main ()
{
int F;
cin >> F;

switch(F){
case 1:
cout <<"January\n";
break;
case 2:
cout <<"February\n";
break;
case 3:
cout <<"March\n";
break;
case 4:
cout <<"April\n";
break;
case 5:
cout <<"May\n";
break;
case 6:
cout <<"June\n";
break;
case 7:
cout <<"July\n";
break;
case 8:
cout <<"August\n";
break;
case 9:
cout <<"September\n";
break;
case 10:
cout <<"October\n";
break;
case 11:
cout <<"November\n";
break;
case 12:
cout <<"December\n";
break;
}

return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be first!

Leave a comment

Ask Your question

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS
paypal