Google Answers Logo
View Question
 
Q: C++ ( Answered,   0 Comments )
Question  
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.
Answer  
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
Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

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 Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy