I need some help with a calculator program. I am not too good with programming so I am having so many difficulties with this calculator program. I have started it and solved a few minor problems on my own, but I still couldn't solve the following:
1-some operations were not functioning right or sometimes the compiler won't recognize them and give me an error.
2- when I tried multiplying a big number by another big one - both positive numbers- it gave me back a negative value.
3- I am trying to make the program quit by typing 'q' at anytime, but that does not seem to be working.
here is my code:
// Oct 22, 2011
// Calculator Program
#include <iostream> // for cout, endl;
using namespace std;
int main()
{
char op;
do
{
int x, y;
cout << ("Welcome to Calculator")<< endl;
cout << "Enter an operand: " << endl;
cin>> x;
cout << "Enter an operation: " << endl;
cin >> op;
//Select the operation ('Q' to quit);
//+, -, *, /, !,^, L, > !;
cout << "Enter an operand: " << endl;
cin >> y;
cout << "Enter Q to quit" << endl;
switch (op)
{
case '+':
cout << " = :" << x + y << endl;
break;
case '-':
cout << " = :" << x - y << endl;
break;
case '*':
cout << " = :" << x * y << endl;
break;
case '/':
cout << " = :" << x / y << endl;
break;
case '%':
cout << " = :" << x % y << endl;
break;
default: cout << " Invalid operator! " << endl;
}
cout << " Thank you for using calculator program ";
cin >> op;
}
while (op == 'y');
return 0;
system ("pause");
return EXIT_SUCCESS;
}
I hope someone can help me with this code to make it better.
Thank you-
The issue of multiplying two numbers together is purely based on binary calculations, If you are using a int plainly it assumes the integer is signed where the first bit in the number is a sign flag. so if you do two multiplications or additions when you roll past say 31^2 to 32^2, assuming a 32 bit integer, the sign flag is set and the number results in an negative. If you want to avert this problem you make the integer unsigned by adding the keyword unsigned before the int.
One thing I note about your program is it doesn't check for 0 for y when doing a divide. Divide by 0 is undefined.
for the factorial, you will have to write the function for that. There are many ways described out on the web, and I don't know what you have covered in your c++ class to get to any of them, sometimes it uses Recursion and other versions don't. Not all of them out there are written in c++.
Hi,
Im still having problems with exponents. I caouldnt even proceed to the next function because the program wont run. I changed my code, and you can see it differs from the previous version but I still couldnt figure it out. I hope you can help me. Thanks,
Think about the logic of mine verses yours. Where do you state that op will ever equal 'y' from the user inputs? Q is for quit, and your operators and nothing is mentioned for y. Scope is minor issue and you have a lot of redundant code.
// Oct 22, 2011
// Calculator Program
#include <iostream> // for cout, endl;
#include <cmath>
usingnamespace std;
double getYvalue()
{
// the goal here is to get the second value if it is needed or not
double y = 0.0;
cout << "Enter the Y value :" << endl;
cin >> y;
return y;
}
int main()
{
// the character for the operand
char op;
// the x value...
double x = 0.0;
// the y value...
double y = 0.0;
// the answer is stored here.
double answer =0;
// am I done with my loop?
bool done = false;
// did I report an Error?
bool error = false;
do
{
cout << "Welcome to Calculator" << endl;
cout << "Enter a value for X: " << endl;
cin >> x;
cout << "Enter an operand: (Q - to Quit)" << endl;
cin>> op;
//Select the operation ('Q' to quit);
//+, -, *, /, !,^, L, > !;
switch (op)
{
case'+':
// we add two values
y = getYvalue();
answer =x + y;
break;
case'-':
// we subtract two values
y = getYvalue();
answer = x - y;
break;
case'*':
// we multiply two values.
y = getYvalue();
answer = x * y;
break;
case'/':
// we divid two values.
y = getYvalue();
// make sure y isn't 0.
if(y != 0)
{
answer = x / y;
}
else
{
cout << "sorry cannot divide by zero!!" << endl;
error = true;
}
break;
case'%':
// we get the % of two values
y = getYvalue();
// again I need to make sure Y isn't 0.
if(y != 0)
{
answer = x % y;
}
else
{
cout << "sorry cannot divide by zero!!" << endl;
error = true;
}
break;
case'!': // factorial....
// this function I am not writing for you.
answer = factorial(x); // calculate x factorial.
break;
case'L': // log 10...
// find the log base 10 of x
answer = log10(x);
break;
case'^':
y = getYvalue();
answer = pow(x, y);
break;
case'q':
case'Q':
// q- was hit and now I need to exit my loop so I set done to true and error to true, I don't need to print an answer.
done = true;
error = true;
break;
default:
cout << " Invalid operator! " << endl;
error = true;
}
// did I report an error? If true I don't print an answer...
if(error == false)
{
cout << "The answer was: " << answer << endl;
}
// was Q hit at some point, if so done becomes true and I will exit the loop.
} while (done == false ); // note the difference here to yours...
system ("pause");
return EXIT_SUCCESS;
}
I could write this in a completely different way to get the logic to flow a little cleaner, but I am following your logic to get the code I just posted. For example: the first thing I would ask is operator, then I could decide in the switch which numbers to ask the user for or quit out. All of your math structures need the x and not all need the y. Quitting doesn't need any of them.
I figure you are not reading my code differences to yours because the error you encountering are in the differences to the two code.
Thank you for your help. I finally noticed the differences. It took me a while because I am a beginner. Its my first time taking this class. I will try working on the factorial hopefully I find an answer. Thanks,