Hello soccer53,
A possible reason you have not received a responce may be due to how difficult your code is to read.
With a few blank lines and some indenting your code could look lime this:
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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
|
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std; // <--- Best not to use.
#define PI acos(-1)
void printSpaces()
{
cout << endl << "##########################################################" << endl << endl;
}
void volumeOfCone()
{
printSpaces();
double radius, height, volume;
// Taking relevant inputs
cout << "Enter radius of base of cone" << endl;
cin >> radius;
cout << "Enter height of cone" << endl;
cin >> height;
// Error Checking
if (radius<0 || height<0)
{
cout << "Incorrect Input. Please Try Again" << endl;
volumeOfCone();
}
/*
Volume = (PI*radius*radius*height)/3
*/
// Calculating
volume = (PI*pow(radius, 2)*height) / 3;
// Printing Outputs
cout << "For the input radius = " << radius << " and height = " << height << endl;
cout << "The volume of Cone is " << fixed << setprecision(2) << volume << endl;
printSpaces();
}
void volumeOfSphere()
{
printSpaces();
double radius, volume;
// Taking relevant inputs
cout << "Enter radius of sphere" << endl;
cin >> radius;
// Error Checking
if (radius<0)
{
cout << "Incorrect Input. Please Try Again" << endl;
volumeOfSphere();
}
/*
Volume = (4*PI*radius*radius*radius)/3
*/
// Calculating
volume = (4 * PI*pow(radius, 3)) / 3;
// Printing Outputs
cout << "For the input radius = " << radius << endl;
cout << "The volume of Sphere is " << fixed << setprecision(2) << volume << endl;
printSpaces();
}
void areaOfOctagon()
{
printSpaces();
double side, area;
// Taking relevant inputs
cout << "Enter the side of Octagon" << endl;
cin >> side;
// Error Checking
if (side<0)
{
cout << "Incorrect Input. Please Try Again" << endl;
areaOfOctagon();
}
/*
Area = 2*(1+sqrt(2))*side*side
*/
// Calculating
area = 2 * (1 + sqrt(2))*pow(side, 2);
// Printing Outputs
cout << "For the input side = " << side << endl;
cout << "The Area of Octagon is " << fixed << setprecision(2) << area << endl;
printSpaces();
}
void distanceBetweenTwoPoints()
{
printSpaces();
double distance;
// Initializing structure for a coordinate
struct Point
{
double x;
double y;
};
Point first, second;
// Taking relevant inputs
cout << "Enter values as asked" << endl;
cout << "First Point" << endl;
cout << "Enter X Coordinate: ";
cin >> first.x;
cout << "Enter Y Coordinate: ";
cin >> first.y;
cout << "Second Point" << endl;
cout << "Enter X Coordinate: ";
cin >> second.x;
cout << "Enter Y Coordinate: ";
cin >> second.y;
/*
distance = sqrt(pow((x1-x2), 2) + pow((y1-y2), 2))
*/
// Calculating
distance = sqrt(pow(first.x - second.x, 2) + pow(first.y - second.y, 2));
// Printing Outputs
cout << "Points were (" << first.x << " , " << first.y << ") and " << "(" << second.x << " , " << second.y << ")" << endl;
cout << "The Distance between these points is " << fixed << setprecision(2) << distance << endl;
printSpaces();
}
int main()
{
int choice{};
bool cont{ true }; // <---Added.
while (cont) // <--- Changed.
{
cout << endl << "Pick an option from choices below" << endl;
cout << "1. Find Volume of Cone " << endl;
cout << "2. Find Volume of Sphere" << endl;
cout << "3. Find Area of Octagon" << endl;
cout << "4. Find Distance between two points" << endl;
cout << "5. Stop" << endl << endl;
cout << "Enter your choice here: "; // <--- Endl nor needed here.
cin >> choice;
//if (choice == 5) // <--- Moved to switch.
// break;
switch (choice)
{
case 1:
volumeOfCone();
break;
case 2:
volumeOfSphere();
break;
case 3:
areaOfOctagon();
break;
case 4:
distanceBetweenTwoPoints();
break;
case 5: // <--- Moved if statement here.
cont = false;
break;
default: cout << "You have entered an invalid option. Please Try again" << endl;
} // End switch.
} // End While loop.
// The next line may not be needid. If you have to press enter to see the prompt it is not needed.
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
std::cout << "\n\n Press Enter to continue";
std::cin.get();
return 0;
}
| |
As you can see the above code is much easier to read and follow.
Changing the while loop to a do/while will make no difference to the program because the while condition always starts as true, so it will always execute one time.
An example of a do/while loop would be:
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
|
int main()
{
int choice{};
bool cont{ true }; // Added.
do
{
cout << endl << "Pick an option from choices below" << endl;
cout << "1. Find Volume of Cone " << endl;
cout << "2. Find Volume of Sphere" << endl;
cout << "3. Find Area of Octagon" << endl;
cout << "4. Find Distance between two points" << endl;
cout << "5. Stop" << endl << endl;
cout << "Enter your choice here: "; // <--- Endl nor needed here.
cin >> choice;
//if (choice == 5) // <--- Moved to switch.
// break;
switch (choice)
{
case 1:
volumeOfCone();
break;
case 2:
volumeOfSphere();
break;
case 3:
areaOfOctagon();
break;
case 4:
distanceBetweenTwoPoints();
break;
case 5: // <--- Moved if statement here.
cont = false;
break;
default: cout << "You have entered an invalid option. Please Try again" << endl;
} // End switch.
} while (cont); // End While / do/while loop. Changed for do/while.
// The next line may not be needed. If you have to press enter to see the prompt it is not needed.
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
std::cout << "\n\n Press Enter to continue";
std::cin.get();
return 0;
}
| |
Right now I see no benefit between the while loop and a do/while loop. As setup they both work the same way.
Hint: try adding the line "#define _USE_MATH_DEFINES" before you include <cmath>. If this should not work the #define needs to go before "#include <math.h>" which is done in the "cmath" header file. Personally I edited my "cmath" file and put the "#define" right before the "#incluse" for "math.h". This opens several "#define" variables like "M_PI" and others. This is found at the end of the "math.h" header file. This way you do not have to calculate "PI" just use "M_PI". It comes out the same.
Suggestion: Most of your lines that require a set of {}s to define a block have the opening { at the end of the line. It is better to press enter and start the opening { on the next line and having the closing } in the same column. Take a look at your functions in the first code for an example. Benefits of this are that it is easier to read and when the {}s line up in the same column, along with proper indenting, it is easier to find one that is messing. Also notice the comments I put on the closing }s to let people know what they go to. This also helps you.
Either way is acceptable and the compiler case nothing about white space, so it is better to make your code readable than compact.
One other thing I noticed is that you should initialize your variables when defined. This is not always necessary, but a goo idea to get use to before it becomes a problem. From C++11 on the use of empty {}s will give numeric variables a value of zero and "char" variables a value of "\0" "floats" and "doubles" would be "0.0". "std::strings" "std::vectors" and some others are defined with a zero size and do not need initialized.
Any other questions let me know.
Hope that helps,
Andy