I have another issue. I've written a program that takes some simple input, performs simple calculations and the output is calculating properly, but the program is truncating the output at the decimal point and I can't figure out why when 100 is input. When 50 is input, the program is giving the surface area to be 0.00. I've narrowed down the problem to the line:
wedgeAngle = float(RADIUS, 2) * PI. It is equal to 0.00 when I run the program with an input of 50. I added some diagnostic lines at the end of the program to figure out where the problem is, but I don't know why that line is giving me bad output.
01 #include <iostream>
02 #include <iomanip>
03 #include <cmath>
04
05 using namespace std;
06
07 const float PI = 3.14159;
08 const float RADIUS = 1738.3;
09
10 int main()
11 {
12 // Input variables
13 int percentage;
14
15 // Local variables
16 float surfaceArea;
17 float wedgeAngle;
18
19 // Input data
20 cout << endl;
21 cout << "Enter the percentage of the moon's face that appears to be "
22 << "illuminated: ";
23 cin >> percentage;
24
25 // Calculations
26 wedgeAngle = float(percentage / 100) * PI;
27 surfaceArea = 2 * pow(RADIUS, 2) * wedgeAngle;
28
29 // Output results
30 cout << endl << fixed << setprecision(2)
31 << "Surface area of lit portion of moon when the face is "
32 << percentage << "% lit: " << endl << surfaceArea
33 << " square kilometers." << endl << endl;
34
35 // I added this output to diagnose the problem and for some reason, all
36 // of my calculations are being truncated at the decimal point.
37 cout << endl << pow(RADIUS, 2) << endl;
38 cout << 2 * pow(RADIUS, 2) << endl;
39 cout << pow(RADIUS, 2) * wedgeAngle << endl;
40 cout << wedgeAngle << endl;
41
42 return 0;
43 }
The code should be pretty straightforward, but can anyone explain the output problem.
Thanks in advance for your help.
[code] Your code goes here [/code] /*26 */ wedgeAngle = float(percentage / 100) * PI; The casting is incorrect. First will do the division (anything below 100 will give you 0) and then convert it to float.