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
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:
- How can I set Outlook Express to recieve my emails?Best solution by Yahoo! Answers
- How can I set up outlook to download mail?Best solution by Yahoo! Answers
- How can I set music as a ringtone on my iPhone?Best solution by Yahoo! Answers
- How can I set a wireless camera to record on my computer?Best solution by Yahoo! Answers
- How can I set up my computer to alert me when I get an incoming email?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
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.