943,984 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 93355
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Nov 2nd, 2004
-4

C++ Calculator Program

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

C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kisseric is offline Offline
6 posts
since Nov 2004
Nov 2nd, 2004
0

Re: C++ Calculator Program

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
Reputation Points: 26
Solved Threads: 4
Junior Poster
Stack Overflow is offline Offline
185 posts
since Sep 2004
Nov 2nd, 2004
0

Re: C++ Calculator Program

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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kisseric is offline Offline
6 posts
since Nov 2004
Nov 2nd, 2004
0

Re: C++ Calculator Program

Also, check this line:
C++ Syntax (Toggle Plain Text)
  1. double doDivideZero(double &)

You need to name your variable, such as (double& x)
Administrator
Staff Writer
Reputation Points: 1422
Solved Threads: 162
The Queen of DaniWeb
cscgal is offline Offline
13,645 posts
since Feb 2002
Nov 2nd, 2004
0

Re: C++ Calculator Program

Forget about your doDivideZero function and replace some of your code with
cpp Syntax (Toggle Plain Text)
  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 ;-)
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Oct 7th, 2008
-2

Re: C++ Calculator Program

#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
Reputation Points: 6
Solved Threads: 0
Newbie Poster
jase728 is offline Offline
2 posts
since Oct 2008
Oct 9th, 2009
0
Re: C++ Calculator Program
nice one dude..!it helps a lot..tnx u so much..!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cyvee is offline Offline
1 posts
since Oct 2009
Oct 30th, 2009
-1
Re: C++ Calculator Program
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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sony_cheema is offline Offline
1 posts
since Oct 2009
Nov 1st, 2009
-1

i think i figured it out

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
help7946 is offline Offline
1 posts
since Nov 2009
Nov 1st, 2009
0
Re: C++ Calculator Program
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!)
Reputation Points: 237
Solved Threads: 117
Practically a Posting Shark
Clinton Portis is offline Offline
822 posts
since Oct 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
This thread is currently closed and is not accepting any new replies.
Previous Thread in C++ Forum Timeline: need to replace a part of char array ....!
Next Thread in C++ Forum Timeline: displaying files





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC