Calculator should display the accumulated value after each operation

//FILE: PROGRAM3.CPP
//AUTHOR : AM NGUYEN 
//COURSE NAME : CS210
//DATE : 
// DESCRIPTION : A SIMPLE CALCULATOR

# include <iostream> 
using namespace std;
void instruction();
int calculator (char);
void divide_by_zero ();
float do_next_op (char, float, float);

void main()
{ 
     //input
     char input;
     float accum,num;
     
      // instructions ();
      accum = 0 
      //get input 
      cout<< " : " ;
      cin >> input;
      
      while (input != 'Q' && input != 'q' && input != '=')
      { 
            cin >> num;
      // Do op
      accum = do_next_op (input, num, accum);
      //display result
      cout << " Result so far : " << accum << endl;
      
      // get next operation 
      cout << "teo mun "<< endl      ;
      } // end not quit
      
      cout << "Final result : " 
     system ("pause") ;  
      return 0 ;
      }
      //instruct user 
      void instruction() 
      cout <<" This program models a simple calculator which can add, subtract,multiply,divide, power" << endl;
      cout << " the calculator accumulate value after each operation " << endl;
      cout << " press Q to quit the program " << endl;
      cout << " Have fun with my program " << endl<< endl;
      
      // function do_next_op ()
      //perform next operation
      // input : operator, operand,and accumulated
      //output : new result
      float do_next_op (char op, float num, float total)
      {
            switch (op)
            {
                   case ' + ' :
                        total += num
                        break;
                   case ' - ' :
                        total -= num
                        break;
                   case ' * ' :
                        total *= num
                        break;
                   case ' / ' : 
                        total /= num 
                   case ' ^ ' :
                        total ^= num
                        break ;
                        default :
                                cout << " syntax error"
                                << endl;
                                
                                }

it cause the error, please help me to fix those error, more over, I am not sure about the while, for ,do while statement, please explain more for me. thank you

the error is main must return int

I declared the void for main already
yes ,this is most likely my complete code

Dani AI

Generated

Thread replies already hit the main problems: syntax mistakes, missing returns/braces, and a misconception about ^. Below is a concise, practical pattern for a loop-driven calculator, plus pointers that expand on the replies from , and .

A minimal robust loop (illustrative; adapt to your needs):

#include <iostream>
#include <cmath>

int main() {
    char op;
    double accum = 0.0, num;
    while (std::cin >> op && op != 'Q' && op != 'q') {
        if (op == '=') break;
        if (!(std::cin >> num)) break;
        if (op == '+') accum += num;
        else if (op == '-') accum -= num;
        else if (op == '*') accum *= num;
        else if (op == '/') {
            if (num == 0.0) { std::cerr << "Divide by zero\n"; continue; }
            accum /= num;
        } else if (op == '^') accum = std::pow(accum, num);
        else { std::cerr << "Unknown operator\n"; continue; }
        std::cout << "Result so far: " << accum << '\n';
    }
    std::cout << "Final result: " << accum << '\n';
    return 0;
}

Use int main() and return a value as suggested; handle input failures and guard division by zero. Note ^ is bitwise XOR in C++; for exponentiation use std::pow from <cmath> (std::pow). For loop choice: while fits this "read until quit" pattern; do-while runs the body at least once; for is for counted iterations ().

If you get compiler errors, fix them one at a time. Compile with warnings enabled (e.g., -Wall -Wextra) to catch missing semicolons, unmatched braces, or fall-through switch issues. The language entry for main is useful background: .

Recommended Answers

All 7 Replies

Is this your complete code ? Because I don't see any ending braces for your do_next_op function. Also what errors do you get, elaborate !!

yep, this is, I just edit mycode

1. Please use "int main()". You are returning 0 at the end of main(), yet you declare that main() return void.

2. You lack a closing brace "}" and a return statement for do_next_op().

There are a host of compile errors in your code. accum = 0 .. missing semi-colon at the end.

Go through your compile errors and try to fix as many of those as you can, otherwise how are you going to understand what went wrong ?

Go through your compile errors and try to fix as many of those as you can, otherwise how are you going to understand what went wrong ?

he might expects us to do so.......

well I hope not :) .. one should really put in some effort !

" This program models a simple calculator which can add, subtract,multiply,divide, power"

I don't see any power operation in the program. If you meant to the ^ operator, it doesn't do power (at least in c++). It's an operator which works on bits. For power you need to implement a function.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.