Hello MoreUmph,
This is an interesting program to work on. Even though I have not found the answer yet.
Some small points that will be useful in the future:
A long time ago I read something about the standard arrays being dangerous somehow |
Just so you know you are using a C style array not a "std:array". There is a big difference. The "std::array" requires the header file "<array>" and would be defined as
std::array <double, 1000> num{};
. The empty {}s at the end initializes each element to zero the same as
= {}
.
num{};
is just a shorter way of writing this.
Although there is nothing wrong with what you have done, and I will make more than one point here:
1 2 3 4 5 6 7 8 9 10
|
void off()
{
//if (choice == 0)
//{
// on = false;
//}
if (!choice)
on = false;
}
| |
The commented code is your original and the other is my revision. As you can see when the {}s line up in the same column it is easier to find the match. The extra white space makes no difference to the compiler.
The other point is with the if statement. When there is only one line the {}s are optional. The same is true for an else, for or while loop. Mostly it is less to worry about when writing.
This is kind of minor, but makes things easier to understand when something is displayed on the screen:
Your code:
1 2
|
cout << "\n\nEnter number\n" << endl;
cin >> num[i];
| |
To many "\n"s and "endl" before the "cin".
My revision with an added option:
1 2 3 4 5 6 7 8 9
|
cout << "\nEnter number: ";
cin >> num[fstOppIdx];
ui.display_operation();
cin >> choice;
off();
if (!on)
return;
| |
The point of lines 8 and 9 is to check if "on" has become false in which case there is no point to continue just return to main where the program will end.
For the program. I see little use for the class since all the functions are public and there are no private variables associated with the class. A regular function would work just as well. It mostly defeats the the purpose of using a class.
I do not see the use of the array or why it has to be there. Using the variables like "num1", "num2" and "result" will achieve the same results as the array. For the function "first_operation" you send the variables "num1" and "num2" to be calculated and store the answer in "result". For the "consecutive_operation" function you would send "result an "num2" to be calculated and store the answer in "result" ready for the next calculation. This makes the program easier to follow and to understand what is being sent to a function. Also this would help make it easier when sending variables to a function and when receiving variables by a function. Note the variables used in a function are local to that function, so the same names for variables can be used without a conflict, although there may still be a problem when there are variables in the global scope.
One thing I did notice in the two functions that follow main:
1 2 3
|
ui.display_operand(choice, num[fstOppIdx], num[secOppIdx]);
num[secOppIdx] = calculate.set_operand(choice, num[secOppIdx], num[fstOppIdx]);
| |
I gave "i" and "j" new names. Notice on line 1 you are sending the array elements as "fstOppIdx" and "secOppIdx", but on line 3 they are being sent in reverse order. This may not be a problem for addition and multiplication, but it could be a big problem for the rest. Point 20 - 5 is not the same as 5 - 20. The same can be said for division.
The appearance is that somewhere something is being switched or reversed and I have not figured out where this is yet.
I have been considering making some big changes to the program to see if that makes any difference.
Eventually something will click and I will see some small thing I have been missing.
Hope that helps,
Andy