C++ Calculator Program

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Nov 2004
Posts: 6
Reputation: kisseric is an unknown quantity at this point 
Solved Threads: 0
kisseric kisseric is offline Offline
Newbie Poster

C++ Calculator Program

 
-1
  #1
Nov 2nd, 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.

  1. #include <iostream>
  2. #include <iomanip>
  3. #include <cmath>
  4. using namespace std;
  5.  
  6. void instructUser();
  7. double doDivideZero(double &);
  8.  
  9.  
  10.  
  11.  
  12. int main()
  13. {
  14. instructUser();
  15.  
  16.  
  17. double displayedVal;
  18. double newEntry;
  19. char command_character ;
  20.  
  21.  
  22. displayedVal = 0.0;
  23.  
  24.  
  25. cout << " Enter accepted Operator:" ;
  26. cin >> command_character;
  27. while (command_character != 'Q' || command_character != 'q')
  28. {
  29. switch(command_character)
  30. {
  31. case 'c':
  32. case 'C': displayedVal = 0.0;
  33. break;
  34. case '+': cout << " Enter Number:";
  35. cin >> newEntry;
  36. displayedVal = displayedVal + newEntry;
  37. break;
  38. case '-': cout << " Enter Number:";
  39. cin >> newEntry;
  40. displayedVal = displayedVal - newEntry;
  41. break;
  42. case '*': cout << " Enter Number:";
  43. cin >> newEntry;
  44. displayedVal = displayedVal * newEntry;
  45. break;
  46. case '/': cout << " Enter Number:";
  47. cin >> newEntry;
  48. displayedVal = displayedVal / newEntry;
  49. if (newEntry == 0)
  50. {
  51. doDivideZero(double &);
  52. }
  53.  
  54. break;
  55. case '^': cout << " Enter Number:";
  56. cin >> newEntry;
  57. displayedVal = pow (displayedVal,newEntry);
  58. break;
  59. default : cout << " Unacceptable Operator(" << command_character << ")" << endl;
  60. }
  61. cout << " The result so far is: " <<displayedVal<< endl;
  62. cout << " Enter Operator:";
  63. cin >> command_character;
  64.  
  65.  
  66. }
  67.  
  68.  
  69.  
  70.  
  71. system ("pause");
  72. return 0;
  73. }
  74.  
  75. void instructUser()
  76.  
  77. {
  78. cout << " " <<endl;
  79. cout << " ***************************************************************************" <<endl;
  80. cout << " * This program takes your input and selected mathematical operator *" <<endl;
  81. cout << " * and returns the answer to the screen. If an illegal operator is *" << endl;
  82. cout << " * selected, an error message will be displayed. Be careful which *" <<endl;
  83. cout << " * operator you select because the program depends on you for input. *" <<endl;
  84. cout << " * The only error check function it has, is for unacceptable opreators. *" <<endl;
  85. cout << " * Acceptable operators are : (+ , - , / , * ,^,c). The character c sets *" <<endl;
  86. cout << " * the value stored to ZERO. Enter Q to exit. ENJOY YOUR PROGRAM !!!!!! *" <<endl;
  87. cout << " ***************************************************************************" <<endl;
  88. cout << " " <<endl;
  89.  
  90.  
  91.  
  92. }
  93.  
  94. double doDivideZero(double &)
  95. {
  96. double newEntry;
  97. double displayedVal;
  98. newEntry =0;
  99. displayedVal = 0.0;
  100.  
  101.  
  102.  
  103. if (newEntry !=0)
  104. {
  105. displayedVal = displayedVal / newEntry;
  106. } else cout << "Wrong Operation, Cannot Divide by Zero" << endl;
  107.  
  108.  
  109.  
  110.  
  111. return 0;
  112. }
:cry: :cry: :cry:
Last edited by alc6379; Nov 2nd, 2004 at 12:06 am. Reason: added [code] tags
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 185
Reputation: Stack Overflow is an unknown quantity at this point 
Solved Threads: 4
Stack Overflow's Avatar
Stack Overflow Stack Overflow is offline Offline
C Programmer

Re: C++ Calculator Program

 
0
  #2
Nov 2nd, 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 Quick reply to this message  
Join Date: Nov 2004
Posts: 6
Reputation: kisseric is an unknown quantity at this point 
Solved Threads: 0
kisseric kisseric is offline Offline
Newbie Poster

Re: C++ Calculator Program

 
0
  #3
Nov 2nd, 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 Quick reply to this message  
Join Date: Feb 2002
Posts: 12,057
Reputation: cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light 
Solved Threads: 132
Administrator
Staff Writer
cscgal's Avatar
cscgal cscgal is offline Offline
The Queen of DaniWeb

Re: C++ Calculator Program

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

You need to name your variable, such as (double& x)
Dani the Computer Science Gal
Follow my Twitter feed! twitter.com/DaniWeb
And if you're interested in Internet marketing there is twitter.com/DaniWebAds
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,151
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 952
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: C++ Calculator Program

 
0
  #5
Nov 2nd, 2004
Forget about your doDivideZero function and replace some of your code with
  1. case '/': cout << " Enter Number:";
  2. cin >> newEntry;
  3. if (newEntry == 0)
  4. {
  5. cout << "Wrong Operation, Cannot Divide by Zero" << endl;
  6. newEntry = 1;
  7. }
  8. displayedVal = displayedVal / newEntry;
  9. break;
This keeps the program going ...
Last edited by WaltP; Oct 7th, 2008 at 1:46 am. Reason: Someone needs to relearn how to post code ;-)
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2
Reputation: jase728 has a little shameless behaviour in the past 
Solved Threads: 0
jase728 jase728 is offline Offline
Newbie Poster

Re: C++ Calculator Program

 
-2
  #6
Oct 7th, 2008
#include <iostream>



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
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 1
Reputation: cyvee is an unknown quantity at this point 
Solved Threads: 0
cyvee cyvee is offline Offline
Newbie Poster
 
0
  #7
Oct 9th, 2009
nice one dude..!it helps a lot..tnx u so much..!
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 1
Reputation: sony_cheema is an unknown quantity at this point 
Solved Threads: 0
sony_cheema sony_cheema is offline Offline
Newbie Poster
 
-1
  #8
Oct 30th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 1
Reputation: help7946 is an unknown quantity at this point 
Solved Threads: 0
help7946 help7946 is offline Offline
Newbie Poster

i think i figured it out

 
0
  #9
Nov 1st, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 481
Reputation: Clinton Portis is on a distinguished road 
Solved Threads: 58
Clinton Portis's Avatar
Clinton Portis Clinton Portis is offline Offline
Posting Pro in Training
 
0
  #10
Nov 1st, 2009
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!)
Reply With Quote Quick reply to this message  
Reply

Message:




Views: 50302 | Replies: 9
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC