954,504 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

need help for c++ calculator

i have to make a simple calculator program in c++
the program need to have the all the calculation in a function called do_next_op. i tried to make this program but it turns out that my program returns incorrect results, i think it may be caused by the value of total is reset to zero after each loop, and i have no idea how to repair that.

i tried to make the program without do_next_op function and it works perfectly, but unfortunately i have to include thet function, and i cant spot the error. please help
thanks

#include <iostream>
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()
{
    cout << endl;
    cout << "*******************************************************" << endl;
    cout << "                    CALCULATOR                    " << endl;
    cout << "        the initial value is set to 0             " << endl;
    cout << "   please enter an operator and a number to begin " << endl;
    cout << "    the accepted operators are : + - * / ^ only   " << endl;
    cout << "       enter a q, Q or = to exit the program      " << endl;
    cout << "*******************************************************" << endl;
    cout << endl;
}

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);
}
kryptolite
Newbie Poster
7 posts since Jan 2006
Reputation Points: 10
Solved Threads: 0
 

Pass the parameters to float do_next_op by reference. Also you need to add <cmath> in your 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
.0f;                           // 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()
{
    cout << endl;
    cout << "*******************************************************" << endl;
    cout << "                    CALCULATOR                    " << endl;
    cout << "        the initial value is set to 0             " << endl;
    cout << "   please enter an operator and a number to begin " << endl;
    cout << "    the accepted operators are : + - * / ^ only   " << endl;
    cout << "       enter a q, Q or = to exit the program      " << endl;
    cout << "*******************************************************" << endl;
    cout << endl;
}


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);
}
SpS
Posting Pro
599 posts since Aug 2005
Reputation Points: 70
Solved Threads: 32
 

whoa thanks a lot. also, i need to include some error handling in this program. for example, when people entered a not a valid (non numeral) input to the 'newentry' variable, like '+a', then the program will crash. anyone know how to avoid this?

kryptolite
Newbie Poster
7 posts since Jan 2006
Reputation Points: 10
Solved Threads: 0
 
whoa thanks a lot. also, i need to include some error handling in this program. for example, when people entered a not a valid (non numeral) input to the 'newentry' variable, like '+a', then the program will crash. anyone know how to avoid this?


Sorry. no more help...either you or your friend messed it up

SpS
Posting Pro
599 posts since Aug 2005
Reputation Points: 70
Solved Threads: 32
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You