Hello tohamoa,
I ran across your program a couple of days ago and the first question I have is if there is any way that you could make the program any more difficult to read?
Some blank lines and a little more care with the indenting would help.
Then there is the use of single letter variable names. I think that you missed a few letters.
You wrote the program and to you the variables have some meaning, but to others they have no meaning.
Given the reworking of this part of the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
|
#include<iostream>
#include<string>
#include<cmath>
//using namespace std; // <--- Best not to use.
// A recent post that is worth reading. http://www.cplusplus.com/forum/beginner/258335/
double pow1(double a, double b);
double extractdigit(double* a, int position, int shift);
void greaterpush(double* a, double * b, int del);
void digittoarray(double a, double * b);
int positsija = 0; // <--- position / посиция or позиция. Initializaton here may not be needed.
int smeshenije = 0; // <--- mixing? / смешение?
int z = 0; // <--- Unknown or the z axis.
int main()
{
double digit{}; // numerator / числитель.
double var{}; // <--- делитель / divisor or demoninator / знаменатель.
double zifra{}; // numerator / числитель.
//double частное{}; // <--- quotient / частное. A possiable variable name. Unless I have missed this somewhere.
std::cout << "\n enter a digit: ";
std::cin >> digit;
while (!std::cin /*|| var == 0.0*/)
{
//if (!std::std::cin) // <--- Use the if/else if statement if you use the || part of the while condition.
std::cout << "\n Invalid Input! \n";
//else if (var == 0.0)
// std::std::cout << "\n Divisor can not be (0) zero! \n"; // <--- Change message.
std::cout << "\n enter a digit: ";
std::cin >> digit;
}
std::cout << " vvedi delitel / enter the divider: ";
std::cin >> var;
while (!std::cin || var == 0.0)
{
if (!std::cin)
std::cout << "\n Invalid Input! \n";
else if (var == 0.0)
std::cout << "\n Divisor can not be (0) zero! \n";
std::cout << " vvedi delitel / enter the divider: ";
std::cin >> var;
}
zifra = digit;
int i = 0;
while (digit >= pow1(10, i))
i = i + 1;
double * ptr = new double[i]; // <--- std::vector may be of better use.
double * ptr2 = new double[i];
double * ptr3 = new double[i];
//i = i - 1;
i++; // <--- Does the same as the line above.
double k = i; // <--- Changed. Can be defined and given a value. This makes line 74 unnecessary.
double zeloje; // whole.
double drobnoje; // factorial.
int mn = 0;
int mnb = i;
int l = 0;
//k = i;
while (l <= k)
{
|
With just a few blank lines is this not much easier to read. I hope that the comments on some of the variable names came out correctly or at least close.
Looking over this much of the program:
Lines 14 - 16. Try to avoid using global variables like these. Any line of code in any function following these definitions has access to these variable and if something puts the wrong value in one of these variables it becomes harder to fine what went wrong. It is best to define these variables in "main" and pass them to the functions that need them. As global variables there is a good chance that your compiler may initialize these variables for you, but initializing them your-self is fine.
Inside "main" these variables are different. They are not automatically initialized, so when you write
double digit;
this only reserves space for this variable. Whatever garbage that may be at that bit of memory "digit" is trying to interpret as a "double" and you end up with an unknown number. On my computer this tends to be "-9.2559631349317831e+64". Not something that you want to work with.
The next potential problem is with
std::cin >> digit;
. This is formatted input and "cin" is expecting a number to be entered. Should you enter anything other than a number "cin" will fail and be unusable the rest of the program. There is a chance that "digit" will not be changed and still contain the garbage from when it was defined. Then when you get to
std::cin >> var;
"cin" is in a failed state and nothing happens. I think that the C++14 standards made a change that if "cin" fails that numeric variables will be set to (0) zero, but I am not completely sure about this.
The while loops following the "cin" statements are a way of catching if "cin" has failed and correcting the problem. The second while loop not only checks the status of "cin", but also shows how you can check for (0.0) or even a range for the number. Adjust as your need dictates.
On line 53 I am guessing that "zifra" is a variable the should never change its value in case you need the original value.
I still have not figured out the next while loop and what is is doing.
Lines 60 - 62 use "new" to define an array, but I can not fine anywhere in the program where you "
delete", or free, this memory. I believe this is a memory leak that needs fixed. Instead of creating new memory a "std::vector" might be a better choice and you do not have to worry about deleting any memory as the vector class handles this for you.
As the code shows I do well until the while loop at the end of the code. "l <= k" is where I get confused and loose anything I have figured out when I concentrate on "l" and "k" to try and figure out what they are or are doing.
This is small, but I noticed the while loop at the end of "main". With what little you have this is an endless loop with no way out, so the program gets stuck here until you do something to end the program. Whatever you may do it is not a proper way to end the program.
Some side notes:
White space, blank lines and comments are ignored by the compiler and are not part of the compiled code. So use as much as you need to make the code more easy to read.
When it comes to functions whether you define a variable in the parameters of the function or between the {}s of the function you can use the same names of variables that you defined in "main" because these variables are local to the function. This also makes the function easier to understand because you do not have to deal with a new variable name.
This is a challenge, so it may take longer to understand the program and figure out what it is doing.
I also wanted to mention that "pow1" as a function name is a bit misleading based on what the function does and what "std::pow" is from the "cmath" header file.
Hope that helps,
Andy