In this exercise, you will create a program that adds, subtracts, multiplies, or
divides two integers. The program will need to get a letter (A for addition, S for
subtraction, M for multiplication, or D for division) and two integers from the user. If
the user enters an invalid letter, the program should not ask the user for the two
integers. Instead, it should display an appropriate error message before the program
ends. If the letter is A (or a), the program should calculate and display the sum of
both integers. If the letter is S (or s), the program should display the difference
between both integers. When calculating the difference, always subtract the smaller
number from the larger one. If the letter is M (or m), the program should display the
product of both integers. If the letter is D (or d), the program should divide both
integers, always dividing the larger number by the smaller one .

#include <iostream>
using namespace std;

int main()
{
    //declare variables 
    int firstInteger = 0;
    int secondInteger = 0;
    int answer = 0;
    char operation = ' ';

    //enter the operation
    cout << "Enter A (addition), S (subtraction), M (multiplication), D (division): ";
    cin >> operation;
    operation = toupper (operation);

    //determine whether the operation is valid
    if (operation != 'A' && operation != 'S' && operation != 'M' && operation != 'D')
    {
        answer = -1;
        cout << "Invalid operation: " << endl;
    }
    else
    {
        //enter the integers
        cout << "First integer: ";
        cin >> firstInteger;
        cout << "Second integer: ";
        cin >> secondInteger;

        //calculate the answer
                if (operation == 'A')
                    answer = firstInteger + secondInteger;
                else if (operation == 'S')
                    if (firstInteger > secondInteger)
                        answer = firstInteger - secondInteger;
                        else 
                            answer = secondInteger - firstInteger;
                else if (operation == 'M')
                    answer = firstInteger * secondInteger;
                else if (operation == 'D')
                    if (firstInteger > secondInteger)
                            answer = firstInteger / secondInteger;
                    else 
                            answer = secondInteger / firstInteger;

    } 

    //display the answer
    cout << "Answer: " << answer << endl;

    system ("pause");
    return 0;
}   //end of main function

Edited 3 Years Ago by happygeek: spam link removed

Comments
bad form, just dumpong full code in response to a 'gimme teh codez' homework cheat request
This article has been dead for over six months. Start a new discussion instead.