Need some help with this math code.

Hey guys, alright I'll get right to it, I'm stumped here. Here's my code as it stands.
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
#include <iostream>
#include <vector>
#include <string>
#include <cmath>
using namespace std;

const float Pi = 3.14159;
//Variables
int planets = 1;
int planetchoice;
float percentage;

int main()
{

    cout << "Name: Nicolas Aguirre" << endl;
    cout << "Filename: P3" << endl;
    cout << "Program Objective: To create a program to calculate the surface area of a lune with parameters specified by the user" << endl;

    do {
        cout << planets << endl;
        planets++;
    } while (planets <= 6);
    cout << "1 = Mercury 2 = Mars 3 = Venus 4 = Saturn 5 = Jupiter 6 = Moon." << endl;
    cout << "Choose a planet or the moon: ";
    cin >> planetchoice;
    if (planetchoice <= 6) {
        cout << "Please enter percentage of lune to calculate: ";
        cin >> percentage;
        if (percentage <= 100)
        {
            vector<float> planetr(6);
            planetr[0] = 2440; //Mercury
            planetr[1] = 3397; //Mars
            planetr[2] = 6052; //Venus
            planetr[3] = 60268; //Saturn
            planetr[4] = 71492; //Jupiter
            planetr[5] = 1738; //Moon

            if (planetchoice == 1)
            {
                cout << 2 * pow(planetr[0], 2) * percentage * .01 << endl; //LEFT OFF DECIMAL PLACE IS WRONG


            }
        }
    }
}


The thing is, my answers are coming out with the decimal in the wrong spot, so something is off with my math. Precedence issue maybe? I'm lost here. Thanks guys!
Last edited on
Please use code tags when posting code - you should still be able to edit your post.
http://www.cplusplus.com/articles/jEywvCM9/

> cout << 2 * pow(planetr[0], 2) * percentage * .01
Never mind the decimal point, your formula for surface area is wrong.
https://en.wikipedia.org/wiki/Sphere#Surface_area

Alright, I added code tags to my post. I'm trying to find the surface area of a lune, not the whole sphere. Formula should be S = 2 * r2 * θ. Where θ is the percentage of the lune to be calculated (user inputs this).
Alright nevermind I got it. After using setprecision(0), it now looks right as expected. Weird that that fixed it. Thanks for your help! :)
No, with that formula, theta is the angle in radians.

When theta is 2.pi you will get 4 pi r^2, the surface area of a sphere (as salem c indicated to you in the link.)
https://en.wikipedia.org/wiki/Spherical_lune.
I get the impression you are trying to do something like this - a special case of a lune.

Of course you'll need to add some error checking to make sure the user selects a planet in the range and the calculation is correct.

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
#include <iostream>
#include <vector>
#include <string>
#include <cmath>

using namespace std;

const double pi = 3.14159

int main()
{
    vector<double> planet_radius;
    
    planet_radius.push_back(2440);  // Mercury
    planet_radius.push_back(3397);  // Mars
    planet_radius.push_back(6052);  // Venus
    planet_radius.push_back(60268); // Saturn
    planet_radius.push_back(71492); // Jupiter
    planet_radius.push_back(1738);  // Moon
    
    cout << "Name: Nicolas Aguirre" << endl;
    cout << "Filename: P3" << endl;
    cout
    << "Program Objective: To create a program to calculate the surface\n"
    << "area of a lune with parameters specified by the user\n";
    
    int i{0};
    do {
        cout << planet_radius[i] << endl;
        i++;
    } while ( i < planet_radius.size() );
    
    cout
    << "1 = Mercury 2 = Mars 3 = Venus 4 = Saturn 5 = Jupiter 6 = Moon\n"
    << "Choose a planet or the moon: ";
    
    int planet_choice;
    cin >> planet_choice;
    
    cout << "Please enter percentage of lune to calculate: ";
    double percentage;
    cin >> percentage;
    
    cout
    << 2 * pow(planet_radius[planet_choice - 1], 2) * percentage/100 * 2 * pi << '\n';
}
Name: Nicolas Aguirre
Filename: P3
Program Objective: To create a program to calculate the surface
area of a lune with parameters specified by the user
2440
3397
6052
60268
71492
1738
1 = Mercury 2 = Mars 3 = Venus 4 = Saturn 5 = Jupiter 6 = Moon
Choose a planet or the moon: 2
Please enter percentage of lune to calculate: 3
692377
Program ended with exit code: 0
Alright, this looks a bit more correct to me. Yeah the whole idea of calculating a lune and the math involved only makes this assignment harder, I'm terrible at math admittedly.... Tomorrow when I get time, I'm going to add some of this code to my project and see what I can do, I'm also new to C++ so that's double challenging. Thanks guys! Appreciate you all! :)
How did you do on your project? I am currently doing this same project. We have to Use vectors to hold planet names and their corresponding radii. And i am not sure how to do that. Yours has the push back?
yes, push_back() more or less treats a vector as if it were a stack, adding a new item onto the highest order index / back end of the container for you. That should do what you need.
@DaisyDoyle,
please post questions like that in the “Lounge” section of this forum. I think you’ll get better directions there.

“Help about coding” sounds a bit vague, doesn’t it?
This is a forum about C++. Have you been suggested to study C++? It looks very far from sociology.
For data analysis maybe Python could just be what you need? I’m not an expert on the topic.
Python is usually considered a good language for a first approach to programming.

@DaisyDoyle is a stealth spammer. Just delete people like that. (Her only other post was a meaningless "thank you" appended to another thread, a common tactic.)
Topic archived. No new replies allowed.