Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Sep 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Calculator Problem

    Assignment:
    Write a program, which will act as a simple four-function calculator. That is it will read a number, read an operator, read another number, then do the operation. The calculator works with integers and uses four functions: +, -, *, and /. After the first operation is completed, the program will read another operator and uses the result of the previous operation as the first value for the next operation. If the user enters a C the result is cleared and then the user starts entering a new number. If the user enters an X, the calculator is turned off. The various input values (i.e. numbers, operators, commands) will be followed by the ENTER key. Your program should prompt the user on what the user is to do. The commands C and X may be entered in place of an operator

    This is what I did. I am not sure whether it is right or not. Can anyone help me with this?
    Thanks

    #include <iostream>

    using namespace std;

    #include <stdlib.h>

    void main()
    {
    bool InvalidOp;
    int Number1;
    int Number2;
    char op;

    cout << "Please enter a number and press enter: ";
    cin >> Number1;

    cout << "Please enter another number and press enter: ";
    cin >> Number2;

    cout << "Please enter a a valid operator, 'c' to clear, or 'x' to turn off and press ENTER : ";
    cin >> op;
    do {
    cout << "Enter an operator [+-*/ccXx]: ";
    cin >> Op;
    InvalidOp = false;
    switch (op)
    {
    case '+':
    cout << "The result is: " << Number1 + Number2 << endl;
    break;

    case '-':
    cout << "The result is: " << Number1 - Number2 << endl;
    break;

    case '*':
    cout << "The result is: " << Number1 * Number2 << endl;
    break;

    case '/':
    cout << "The result is: " << Number1 / Number2 << endl;
    break;

    case 'C':
    case 'c':
    break;

    case 'X':
    case 'x':
    cout << "Bye" << endl;
    exit (0);

    default:
    cout << "Invalid operator" << endl;
    InvalidOp = true;
    }
    } while (InvalidOp);

    }

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,026
    Thanks
    23
    Thanked 588 Times in 587 Posts


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •