Skip to content

Instantly share code, notes, and snippets.

@jcarter62
Created June 14, 2018 22:56
Show Gist options
  • Select an option

  • Save jcarter62/fbd188f1b9c4a664b9f77ecf461c6ea4 to your computer and use it in GitHub Desktop.

Select an option

Save jcarter62/fbd188f1b9c4a664b9f77ecf461c6ea4 to your computer and use it in GitHub Desktop.

Revisions

  1. jcarter62 created this gist Jun 14, 2018.
    72 changes: 72 additions & 0 deletions calculator.cpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,72 @@
    //
    // main.cpp
    // calculator
    //
    // Created by Jim Carter on 6/14/18.
    //

    #include <iostream>
    using namespace std;

    int main(int argc, const char * argv[]) {
    // insert code here...
    std::cout << "Hello, World!\n";

    int a;
    int b;
    char op;
    int opOk;
    int result;
    int done;

    done = 0;
    while ( done <= 0 ) {
    char again;

    cout << "Please provide two numbers, and an operator (+-*/)\n";
    cin >> a;
    cin >> b;
    cin >> op;

    opOk = 0;
    if ( ( op == '+' ) || ( op == '-' ) || ( op == '*' ) || ( op == '/' ) ) {
    opOk = 1;
    }

    if ( ! opOk ) {
    cout << "Invalid Operator, please use one of (+-*/) \n";
    } else {
    result = 0;
    if ( op == '+' ) {
    result = a + b;
    }
    if ( op == '-' ) {
    result = a - b;
    }
    if ( op == '*' ) {
    result = a * b;
    }
    if ( op == '/' ) {
    result = a / b;
    }
    cout << a << op << b << " = " << result << "\n";

    bool ynQ = false;
    while ( ! ynQ ) {
    cout << "Again ? (Y/N) \n";
    cin >> again;
    if (( again == 'Y' ) || ( again == 'y' )) {
    done = 0;
    ynQ = true;
    }
    if ( ( again == 'N' ) || ( again == 'n' ) ) {
    ynQ = true;
    done = 1;
    }
    }
    }

    }
    cout << "..... see you later .....\n";
    return 0;
    }