| | |
C++ Calculator Program
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Nov 2004
Posts: 6
Reputation:
Solved Threads: 0
I am doing something wrong, I have tried for two days and I still cannot figure it out. Can someone help me? Thank you.
:cry: :cry: :cry:
C++ Syntax (Toggle Plain Text)
#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; }
Last edited by alc6379; Nov 2nd, 2004 at 12:06 am. Reason: added [code] tags
Greetings kisseric,
Try changing the line: To: Once I did this, your program ran just fine. I compiled it in Dev-C++.
- Stack Overflow
Try changing the line:
if (newEntry == 0)
{
doDivideZero(double &);
}if (newEntry == 0)
{
doDivideZero(displayedVal);
}- 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
IRC
Channel: irc.daniweb.com
Room: #c, #shell
Also, check this line:
You need to name your variable, such as (double& x)
C++ Syntax (Toggle Plain Text)
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

Follow my Twitter feed! twitter.com/DaniWeb
And if you're interested in Internet marketing there is twitter.com/DaniWebAds
Forget about your doDivideZero function and replace some of your code with
This keeps the program going ...
cpp Syntax (Toggle Plain Text)
case '/': cout << " Enter Number:"; cin >> newEntry; if (newEntry == 0) { cout << "Wrong Operation, Cannot Divide by Zero" << endl; newEntry = 1; } displayedVal = displayedVal / newEntry; break;
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!
•
•
Join Date: Oct 2008
Posts: 2
Reputation:
Solved Threads: 0
#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
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
![]() |
Similar Threads
- java calculator program (Java)
- 40 digit accuracy calculator program (C)
- Write a calculator program using a do-while loop (C++)
- Really need help with the interactive Calculator program, or will be dead (C++)
- starting a calculator program in C (C)
- Java Swing Calculator program not running. It has 0 errors (Java)
- Wierd error messages with calculator program (C++)
- need help with calculator program in C (C)
Other Threads in the C++ Forum
- Previous Thread: STRUGGLING!! I am in dire need of some help
- Next Thread: Identify someone's age by entering the IC number...
Views: 50302 | Replies: 9
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelper iamthwee ifstream image input int java lazy lib loop looping loops map math matrix memory multidimensional multiple newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






