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

C++ Calculator Program

I am doing something wrong, I have tried for two days and I still cannot figure it out. Can someone help me? Thank you.

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

void instructUser();
double doDivideZero(double &);




int main()
{
	instructUser();
	
	
	double displayedVal;
	double newEntry;
	char command_character ;

	
	displayedVal = 0.0;

	
	cout << "  Enter accepted Operator:" ;
	cin >> command_character;
	while (command_character != 'Q' || command_character != 'q')
	{
		switch(command_character)
		{
		case 'c':
		case 'C': displayedVal = 0.0;
				  break;
		case '+': cout << "  Enter Number:";
				  cin >> newEntry;
				  displayedVal = displayedVal + newEntry;
				  break;
		case '-': cout << "  Enter Number:";
				  cin >> newEntry;
				  displayedVal = displayedVal - newEntry;
				  break;
		case '*': cout << "  Enter Number:";
				  cin >> newEntry;
				  displayedVal = displayedVal * newEntry;
				  break;
		case '/': cout << "  Enter Number:";
			      cin >> newEntry;
				  displayedVal = displayedVal / newEntry;
				  if (newEntry == 0)
				  {
					  doDivideZero(double &);
				  }
				  
				  break;
		case '^': cout << "  Enter Number:";
				  cin >> newEntry;
				  displayedVal = pow (displayedVal,newEntry);
				  break;
				  default : cout << "  Unacceptable Operator(" << command_character << ")" << endl;
		}
		cout << "  The result so far is: " <<displayedVal<< endl;
		cout << "  Enter Operator:";
		cin >> command_character;
		
		
	}




	system ("pause");
	return 0;
}

void instructUser()

{
		cout << "                    " <<endl;
		cout << "  ***************************************************************************" <<endl;
		cout << "  *  This program takes your input and selected mathematical operator       *" <<endl;
		cout << "  *  and returns the answer to the screen. If an illegal operator is        *" << endl;
		cout << "  *  selected, an error message will be displayed. Be careful which         *" <<endl;
		cout << "  *  operator you select because the program depends on you for input.      *" <<endl;
		cout << "  *  The only error check function it has, is for unacceptable opreators.   *" <<endl;
		cout << "  *  Acceptable operators are : (+ , - , / , * ,^,c). The character c sets  *" <<endl;
		cout << "  *  the value stored to ZERO. Enter Q to exit. ENJOY YOUR PROGRAM !!!!!!   *" <<endl;
		cout << "  ***************************************************************************" <<endl;
		cout << "                     " <<endl;



}

double doDivideZero(double &)
{
	double newEntry;
	double displayedVal;
	newEntry =0;
	displayedVal = 0.0;
	


	if (newEntry !=0)
	{
		displayedVal = displayedVal / newEntry;
	} else cout << "Wrong Operation, Cannot Divide by Zero" << endl;
	
		
		
	
	return 0;	
}

:cry: :cry: :cry:

kisseric
Newbie Poster
6 posts since Nov 2004
Reputation Points: 10
Solved Threads: 0
 

Greetings kisseric,

Try changing the line:

if (newEntry == 0)
{
	doDivideZero(double &);
}


To:

if (newEntry == 0)
{
	doDivideZero(displayedVal);
}


Once I did this, your program ran just fine. I compiled it in Dev-C++.

-Stack Overflow

Stack Overflow
Junior Poster
193 posts since Sep 2004
Reputation Points: 26
Solved Threads: 4
 

Thanks for the tip. It compiled OK but it messed up the other operations. For example after trying to divide by Zero, I am unable to do any other operations. How do I for the program to quit after an attempt to divide by Zero?

kisseric
Newbie Poster
6 posts since Nov 2004
Reputation Points: 10
Solved Threads: 0
 

Also, check this line:

double doDivideZero(double &)


You need to name your variable, such as (double& x)

cscgal
The Queen of DaniWeb
Administrator
19,422 posts since Feb 2002
Reputation Points: 1,474
Solved Threads: 230
 

Forget about your doDivideZero function and replace some of your code with

case '/': cout << "  Enter Number:";
			      cin >> newEntry;
				  if (newEntry == 0)
				  {
		              cout << "Wrong Operation, Cannot Divide by Zero" << endl;
		              newEntry = 1;
				  }
				  displayedVal = displayedVal / newEntry;
				  break;

This keeps the program going ...

vegaseat
DaniWeb's Hypocrite
Moderator
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 

#include

using namespace std;


float sub (float a, float b)
{
float dif;
dif=a - b;
return dif;
}

float addition (float c, float d)
{
float sum;
sum=c+d;
return sum;
}


float divi (float e, float f)
{
float quo;
quo = e/f;
return quo;
}

float multi (float f, float g)
{
float pro;
pro=f * g;
return pro;
}

int main ()
{
float a;
float b;
float opt;

cout << "Welcome to the calculator" << endl;
cout << "There are four options " << endl;
cout << "Option 1 is subtraction, Option 2 is addition, option 3 is division, and option 4 is multiplication " << endl;
cout << "Enter your option and two digits " << endl;
cin >> opt >> a >> b;

if (opt == 1)
{
float temp= sub (a, b);
cout << "Your difference is " << temp << endl;
}

else
if (opt==2)
{
float temp2= addition (a, b);
cout << "Your sum is " << temp2 << endl;
}
else
if (opt == 3)
{
float temp3= divi (a,b);
cout << "Your quotient is " << temp3 << endl;
}
else
if (opt == 4)
{
float temp4= multi (a,b);
cout << "Your product is " << temp4 << endl;
}
else
{
cout << "That function is not supported in calculator!" << endl;
}
cout << "Thank you for using calculator" << endl;

return 0;
}
//Trademark of JASE Inc

jase728
Newbie Poster
2 posts since Oct 2008
Reputation Points: 6
Solved Threads: 0
 

nice one dude..!it helps a lot..tnx u so much..!

cyvee
Newbie Poster
1 post since Oct 2009
Reputation Points: 10
Solved Threads: 0
 

hay guys i just have to ask that i wana make a programe which will take a input from user and then print it linewise on screen for example if i enter 3214 then it will print
3
2
1
4
please help me soon ..its my assignment

sony_cheema
Newbie Poster
1 post since Oct 2009
Reputation Points: 10
Solved Threads: 0
 

your line numbers dont need to be there, thats part of the reason your code is screwing up that was almost the whole reason......second the complier im using says the "cout" is undeclared and in parentheses it says (first use this function).....the complier i use is Dev-C++ if that matters.

help7946
Newbie Poster
1 post since Nov 2009
Reputation Points: 10
Solved Threads: 0
 

Talk about resurrecting the dead on Halloween.. never seen a post this old get bumped to the top in a long time (this thread originated back in 2004!)

Clinton Portis
Practically a Posting Shark
833 posts since Oct 2005
Reputation Points: 237
Solved Threads: 118
 

make a calculator program to add ,substract,multiply,divide and get the square root of the number...
tnx

hope youwill help me...

hassanors
Newbie Poster
1 post since Feb 2010
Reputation Points: 10
Solved Threads: 0
 

As per forum rules I'm sure you seen on top of this section We only give homework help to those who show effort

peter_budo
Code tags enforcer
Moderator
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902
 

And yet another hijack bump to a 6 year old thread. I don't think he read even one thing he was asked to read upon registering, so why would he read the important stickys and announcements at the top of the forum itself?

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You