User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 391,588 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,643 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 27751 | Replies: 4
Reply
Join Date: Nov 2004
Posts: 6
Reputation: kisseric is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
kisseric kisseric is offline Offline
Newbie Poster

C++ Calculator Program

  #1  
Nov 1st, 2004
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:
Last edited by alc6379 : Nov 1st, 2004 at 11:06 pm. Reason: added [code] tags
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Sep 2004
Location: Overflow State
Posts: 183
Reputation: Stack Overflow is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 4
Stack Overflow's Avatar
Stack Overflow Stack Overflow is offline Offline
C Programmer

Re: C++ Calculator Program

  #2  
Nov 1st, 2004
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
Following the rules will ensure you get a prompt answer to your question. If posting code, please include BB [code][/code] tags. Your question may have been asked before, try the search facility.

IRC
Channel: irc.daniweb.com
Room: #c, #shell
Reply With Quote  
Join Date: Nov 2004
Posts: 6
Reputation: kisseric is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
kisseric kisseric is offline Offline
Newbie Poster

Re: C++ Calculator Program

  #3  
Nov 1st, 2004
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?
Reply With Quote  
Join Date: Feb 2002
Location: Lawn Guylen, NY
Posts: 10,879
Reputation: cscgal is just really nice cscgal is just really nice cscgal is just really nice cscgal is just really nice cscgal is just really nice 
Rep Power: 32
Solved Threads: 106
Admin
Staff Writer
cscgal's Avatar
cscgal cscgal is offline Offline
The Queen of DaniWeb

Re: C++ Calculator Program

  #4  
Nov 2nd, 2004
Also, check this line:
double doDivideZero(double &)

You need to name your variable, such as (double& x)
Reply With Quote  
Join Date: Oct 2004
Location: Mojave Desert
Posts: 2,394
Reputation: vegaseat will become famous soon enough vegaseat will become famous soon enough 
Rep Power: 9
Solved Threads: 172
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
Kickbutt Moderator

Solution Re: C++ Calculator Program

  #5  
Nov 2nd, 2004
Forget about your doDivideZero function and replace some of your code with
[php] case '/': cout << " Enter Number:";
cin >> newEntry;
if (newEntry == 0)
{
cout << "Wrong Operation, Cannot Divide by Zero" << endl;
newEntry = 1;
}
displayedVal = displayedVal / newEntry;
break;
[/php]
This keeps the program going ...
May 'the Google' be with you!
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C++ Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 11:03 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC