Thread: A question about my calculator program.

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    3

    A question about my calculator program.

    Hi, I was having problem with my program. It still have some error when I typed in 5.0 and it only showed up 5 or I typed some other words that wasn't digit and it will be a big error. Can someone tell me what should I do to fix this?
    Here's my homework assignment:
    Write a Program that models a simple calculator. Each data entry line should consist of a valid operator (from the list below), and the right-hand operand. Assume that the left-hand operand is the accumulated value in the calculator, with an initial value of 0.0.
    Acceptable operators:
    + ..Add
    - ..Subtract
    * ..Multiply
    / ..Divide
    ^ ..Power (raise left operand to the power of the right operand)
    q or = ..Quit
    Your Calculator should display the accumulated value after each operation. A sample run might be:
    + 5.0
    Result so far is 5.00
    ^ 2
    Result so far is 25.00
    / 2.0
    Result so far is 12.50
    Q 0
    The final result is 12.50
    Include (define and call) at least THREE functions:

    * a function that displays instructions to the user.
    * a function do_next_op() that has 3 input parameters (the operator, the operand, and the current accumulated value), and returns the new value for the accumulated value. An alternative implementation may use 2 input parameters (operator and operand), and 1 input/output parameter (the accumulated value)
    * at least one other function - of your choice! Make sure that it does something useful.


    Here's my program code:
    Code:
    #include <iostream>
    #include <cmath>
    using namespace std;
    
    void instruction ();
    float do_next_op ( float&, float&, char );
    
    int main()
    {
    
    instruction(); // displays instruction
    float total;
    float newentry;
    char op;
    
    total = 0;
    // initialisation
    
    cin >> op;
    while (op != 'Q' && op != 'q' && op != '=')
    {
    cin >> newentry;
    do_next_op (total, newentry, op);
    cin >> op;
    }
    cout << "the final result is " << total << endl;
    
    system ("pause");
    return 0;
    }
    
    void instruction()
    {
    
    }
    
    float do_next_op ( float &total, float &newentry,char op)
    {
    
    switch (op)
    {
    case '+': total = total + newentry;
    break;
    case '-': total = total - newentry;
    break;
    case '*': total = total * newentry;
    break;
    case '/': total = total / newentry;
    if (newentry == 0)
    {
    cout << "divide by zero is unexecutable" << endl;
    }
    break;
    case '^': total = pow (total,newentry);
    break;
    default : cout << " Unacceptable Operator(" << op << ")" << endl;
    }
    
    cout << "result so far is " << total << endl;
    cout << endl;
    
    return (total);
    }

  2. #2
    Registered User
    Join Date
    Nov 2009
    Posts
    3
    Can someone help me out?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM