Is using the break statement bad practice?

In C++, how can i set the program to out the statement in Case 5?

  • /* Write a programm that mimics a calculator the program is to take as input two integers and write the operation to be performed It should then output the numbers the operator and the result? */ The Code: ------------------- #include <iostream> using namespace std; int main() { double A, B, sum; int op; cout<<"********** WELCOME **********"<<endl; cout<<"* Need Help with Math? Plese Enter the following: *"<<endl; cout<<"First Number:"<<endl; cin >> A; cout<<"Second Number:"<<endl; cin>> B; do { cout<<" Select an Operation"<<endl; cout<<"1) Add"<<endl; cout<<"2) Subtract"<<endl; cout<<"3) Mu;ltipy"<<endl; cout<<"4) Divde"<<endl; cin>> op; } while( op !=1 && op !=2 && op !=3 && op !=4); switch (op) { case 1: sum= A+B; cout<<"The Answer: "<<A<<" + "<<B<<" ="<<sum<<endl; break; case 2: sum= A-B; cout<<"The Answer: "<<A<<" - "<<B<<" ="<<sum<<endl; break; case 3: sum= A*B; cout<<"The Answer: "<<A<<" * "<<B<<" = "<<sum<<endl; break; case 4: sum = A/B; cout<<"The Answer: "<<A<<" / "<<B<<" = "<<sum<<endl; break; case 5: sum= A/B; cout<<"MATH ERROR! CAN NOT DIVIDE BY ZERO"<<endl; break; } cin.get(); return 0; } ______________________________________ I am having a little issue trying to get the program to output the statement that is in case 5, when i run the program, it appears it stays in case 4 and says if user inputs 2 in A and 0 in B output: The Answer: 2/0 = inf But I want the program to output the statement in Case 5 which is MATH ERROR CAN NOT DIVIDE BY ZERO Is there a way to fix this problem?

  • Answer:

    your do/while loop never allows 5 to be entered so it will never reach that condition, incorporate it into case 4 http://ideone.com/GLBv0

Mr AJ at Yahoo! Answers Visit the source

Was this solution helpful to you?

Other answers

AS noted above - can never get to case 5. Test B for 0 in case 4 and report if an error otherwise do the calc.

Allen Miller

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.