Hi SGM3,
It's been awhile, but I am back.
In your last post, the two while loops are almost the same - why can't you have a GetNumber function that is called twice?
1 2 3 4
|
if (operators == '+' || operators == '-' || operators == '*' || operators == '/')
{
std :: cout << num1 << " " << operators << " " << num2 << " = " << answer << std :: endl;
}
|
The problem with this is that it is hard to extend. If you had 20 operators, then your if statement would have 20 comparisons. Really the if isn't necessary - the cout statement could be in a function called PrintAnswer and that could be called from each of the case statements.
The use of functions for Add Subtract etc aren't needed because the can easily done with a short statement in the switch. If the operator required more than several lines of code, then it would be reasonable to make it a function. An example might be convert radians to degrees, minutes, seconds.
But, when I enter anything but a number, it skips out of the loop and outputs a 0 for the first number input and a computer generated number for the second. |
1 2
|
while ((std :: cout << "Enter second number: " << std :: endl)
&& (std :: cin >> entNum2) && (entNum2 == 0))
|
So, to understand what is happening with the while condition. You have 3 things separated by && operators. So all of the 3 things have to evaluate to true for the code in braces to execute. The first is easy, a cout which will nearly always be true. The second a cin, will be false if the user doesn't enter a number - this is the problem - the code in braces isn't executed. The third has the same effect if zero is entered, but only if entNum2 is an integer type.
&& (entNum2 == 0.0)
won't work either because you can't compare floats / doubles that way. So if any of the 3 things fail, the error reporting doesn't happen.
a computer generated number for the second. |
Probably means entNum2 wasn't initialised.
Now I have just seen this, which you posted much earlier. It is quite different.
1 2
|
while ((std :: cout << "Enter first number: " << std :: endl)
&& (std :: cin >> entNum1) || (entNum1 < 1))
|
The difference is the || operator, but the logic seems backwards to me - the body of the loop should only execute if there is a problem, right? You could negate the cin with the ! operator like this:
1 2
|
while ((std :: cout << "Enter first number: " << std :: endl)
&& !(std :: cin >> entNum1) || (entNum1 < 1))
|
Anyway, I wouldn't recommend using it for doubles because of the zero comparison.
Also, be careful using && and || without using parentheses to force the intended precedence. In this example the cout and cin are bound together because && has higher precedence than ||.
Just wondering where you found this code? Could be interesting to read all of what they said.
Here's a couple things you could have a go at:
Write the calculator program using a single class. The design is still the same:
Get a number
get an operator
get a number
calc answer
|
Your IDE should create .h file and a .cpp file when you create the class. Put all the declaration in the .h file, and the definitions of functions in the .cpp file. Create the object in the main.cpp file. You should have minimal code in main().
Think about which functions will be private and which are public. See how you go, once it works for basic functions, you could extend it to cope with lots of extra functions.
The other little project could be to implement K&R calculator that uses a stack.
I found this, The C programming book by Kernighan & Ritchie Second edition. Well worth reading - takes you through all the basics very well.
Chapter 4 has the calculator - there is a website that has all the code in one place, I had trouble finding it.
The K&R Reverse Polish Notation (RPN) calculator works like this:
//Get some input
//if it's a number push it onto stack
// if it's an operator, pop stack & calc answer
|
Have a go at implementing this using a class and a <stack> STL container.