In C write a program to model a simple calculator Each data

In C++

write a program to model a simple calculator. Each data line should consist of the next operation to be performed from the list below and the right operand. Assume the left operand is the accumulator value (initial value of 0). You need a function scan_data with two output parameters that returns the operator and right operand scanned form a data line. You need a function do_next_op that performs the required operation, do_next_op has two input parameters (the operator and operand) and one input/output parameter (the accumulator). The valid operators are: + add - subtract * multiply / divide ^ power (raise left operand to power of right operand) q Quit Your calculator should display the accumulator value after each operation. A sample Run follows.

+ 10.0
Result so far is 10.0

/0
Error: division by zero

Result so far is 10.0
/ 2.0
Result so far is 5.0

&2
Error: invalid operand

Result so far is 5.0 ^2
Result so far is 25.0

q0
Final result is 25.0

This is my coding

#include <iostream>
#include <cmath>
using namespace std;

double scan_data(char&, double&);
void do_next_op(char, double, double&);


int main()
{
   double accum = 0.0;
   char op;
   double num;
   accum = scan_data(op, num);
   do_next_op(op, num, accum);

  
  
   return 0;


}

double scan_data(char& op, double& num)
{
   double accum = 0.0;

   while(op != \'q\')
   {

       cin >> op;
       cin >> num;


       do_next_op(op, num, accum);
      

       if(op != \'q\')
       {
           cout << \"Result os far is \" << accum << endl;

       }
       else
       {
           cout << \"Final result is \" << accum << endl;
       }


   }
  

   return accum;
  
}

void do_next_op(char op, double num, double& accum)
{
  
   if(num == 0)
   {
       cout << \"Error: division by zero\" << endl;
   }
  
   if(op == \'+\')
   {
       accum += num;
   }
   else if(op == \'-\')
   {
       accum -= num;
   }
   else if(op == \'*\')
   {
       accum *= num;
   }
   else if(op == \'/\')
   {
       accum /= num;
   }
   else if(op == \'^\')
   {
       accum = pow(accum, num);
   }
   else
   {
       cout << \"Error: invalid operand\" << endl;
   }


  
}

When I divid it by zero I get \"inf\" in the output.

So can anyone correct my coding to get the output as the sample pleasse.

Solution

Your code is almost correct ,check out bold part of program for chances

if(num!=0)            // this will perform division operation only when denominator is non zero

       {
            accum /= num;
       }
      else accum;

-----------------------------------------------------------------------.-

Your complete code

--------------------------------------------------------------------

#include <iostream>
#include <cmath>
using namespace std;

double scan_data(char&, double&);
void do_next_op(char, double, double&);


int main()
{
   double accum = 0.0;
   char op;
   double num;
   accum = scan_data(op, num);
   do_next_op(op, num, accum);

   return 0;


}

double scan_data(char& op, double& num)
{
   double accum = 0.0;

   while(op != \'q\')
   {

       cin >> op;
       cin >> num;


       do_next_op(op, num, accum);


       if(op != \'q\')
       {
           cout << \"Result os far is \" << accum << endl;

       }
       else
       {
           cout << \"Final result is \" << accum << endl;
       }


   }


   return accum;

}

void do_next_op(char op, double num, double& accum)
{

   if(num == 0)
   {
       cout << \"Error: division by zero\" << endl;
   }

   if(op == \'+\')
   {
       accum += num;
   }
   else if(op == \'-\')
   {
       accum -= num;
   }
   else if(op == \'*\')
   {
       accum *= num;
   }
   else if(op == \'/\')
   {
       if(num!=0)            // this will perform division operation only when denominator is non zero

       {
            accum /= num;
       }
      else accum;

   }
   else if(op == \'^\')
   {
       accum = pow(accum, num);
   }
   else
   {
       cout << \"Error: invalid operand\" << endl;
   }

}

In C++ write a program to model a simple calculator. Each data line should consist of the next operation to be performed from the list below and the right opera
In C++ write a program to model a simple calculator. Each data line should consist of the next operation to be performed from the list below and the right opera
In C++ write a program to model a simple calculator. Each data line should consist of the next operation to be performed from the list below and the right opera
In C++ write a program to model a simple calculator. Each data line should consist of the next operation to be performed from the list below and the right opera

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site