I am currently learning C++ though Bjarne Stroustrup book Programming Principles and Practices using C++.
Going through the drills, I am trying to purposefully write efficient and readable code (good coding practices. However, I think I may be overdoing it with my use of functions for tasks within my program.
//Function to check if the entered numbers are similar in value
double similarValues(double valueBigger, double valueSmaller)
{
if ((valueBigger - valueSmaller) < 0.01) {
return 1; //Return 1 if the values are similar
}
else {
return 0;
}
}
//Function checks which value is larger
double valueLarger(double value01, double value02)
{
if (value01 > value02) {
return value01;
}
else if (value01 < value02) {
return value02;
}
else {
return 0; //Return 0 if the values are equal!
}
}
//Function checks which value is smaller
double valueSmaller(double value01, double value02)
{
if (value01 > value02) {
return value02;
}
else if (value01 < value02) {
return value01;
}
else {
return 0; //Return 0 if the values are equal!
}
}
int main()
{
// Reads two integers on each loop and prints them//
double value02;
double value01;
while (cin >> value01 >> value02) {
// Checks if the functions valueSmaller & valueLarger have not retuned 0
if (valueSmaller(value01, value02) != 0 || valueLarger(value01, value02) != 0) {
cout << "The smaller value is: " << valueSmaller(value01, value02) << " , the larger value is: " << valueLarger(value01, value02) << "\n";
//Checks if the values are at least similar, returns 1 if true
if (similarValues(valueLarger(value01, value02), valueSmaller(value01, value02)) == 1) {
cout<<"The numbers are almost equal. \n";
}
}
// If functions valueSmaller & valueLarger have returned 0 then do the below instuction
else if (value01 == value02) {
cout << "Both of these numbers are equal!" << "\n";
}
// If the values entered cannot be processed then output an error message
else {
cout << "Not a value I know! Incorrect input" << "\n";
}
}
}
Please critically (I do not mind if it is harsh) state what programming practices I am not following or following correctly. Also, whether my coding is on par with what you would deem as following great coding conventions.
- Am I overdoing the functions?
- Am I using return values differently then they should be used?
- Is my code readable?
- Are my comments accurate and to the point or are they vague?
std::max
andstd::min
to get larger and smaller values \$\endgroup\$