|
|
Subject:
C++
Category: Reference, Education and News Asked by: november25-ga List Price: $20.00 |
Posted:
09 Dec 2002 08:09 PST
Expires: 08 Jan 2003 08:09 PST Question ID: 121816 |
Writing a C++ program that mimics a calculator. The program should take as input two integers and the operation to be performed. It should then output the numbers, the operator, and the result (For division, if the denominator is zero, output an appropriate message.) Some sample outputs follow: 3+4=7 13 * 5=65. |
|
Subject:
Re: C++
Answered By: leapinglizard-ga on 09 Dec 2002 19:07 PST |
I made the assumption that each part of the expression is to be entered separately, and that the user wants to carry out a single calculation each time the program runs. Code follows. //begin calc.cc #include<stdio.h> #include<stdlib.h> #include<iostream> using namespace std; int main() { int a, b, c; char op; cout << "enter first integer: "; cin >> a; cout << "enter second integer: "; cin >> b; cout << "enter operation [+-*/]: "; cin >> op; switch (op) { case '+': c = a+b; break; case '-': c = a-b; break; case '*': c = a*b; break; case '/': if (b == 0) { cout << "error: division by zero\n"; return 0; } c = a/b; break; default: cout << "\nerror: unknown operator\n"; return 0; } cout << a << ' ' << op << ' ' << b << " = " << c << '\n'; return 0; } //end calc.cc If you have trouble compiling the program or if you'd like me to make modifications, please let me know before assigning a rating. Cheers, leapinglizard |
|
There are no comments at this time. |
If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you. |
Search Google Answers for |
Google Home - Answers FAQ - Terms of Service - Privacy Policy |