Menu program not exiting right

I was trying to re-arrange my code to make all my functions void and make int main call them but ran into a problem. First, I know global variables are frowned upon but unfortunately our professor wants us to include them in our program.


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

using namespace std;

void userMenu();
void userInput();
void calculatingWeight();

const double MERCURY = 0.4155;
const double VENUS = 0.8975;
const double EARTH = 1.0000;
const double MOON = 0.1660;
const double MARS = 0.3507;
const double JUPITER = 2.5374;
const double SATURN = 1.0677;
const double URANUS = 0.8947;
const double NEPTUNE = 1.1794;
const double PLUTO = 0.0899;

double weight;
string planet;

int main()
{
    userMenu();
    userInput();
    calculatingWeight();
}

void userMenu()
{
    int choice;
    do
    {
    cout << 
            "\n"
            "______________________________________\n"  
            " |This program calculates the weight |\n" 
            " |of a person in one of the celestial|\n" 
            " |bodies: Mercury, Venus, Earth, Mars|\n" 
            " |Jupiter, Saturn, Uranus, Neptune,  |\n" 
            " |Pluto, and Moon.                   |\n" 
            " |-----------------------------------|\n" 
            " |                  MENU             |\n"
            " |      0: Quit Program              |\n"
            " |      1: Calculate the weight      |\n"
            " |-----------------------------------|\n"
            " |      Enter your choice: ";
    cin >> choice;
    
    switch(choice)
    {
    case 0: cout << "Exiting Program" << endl;
         break;
    case 1: userInput();
            calculatingWeight();
         break;
    default: cout << "Invalid choice, try again!" << endl;
    }
}
    while( choice != 0 );
    
}

void userInput()
{
    
    cout << "Please enter your weight in pounds: " << endl;
    cin >> weight;
    
    cout << "Enter the name of a celestial body: " << endl;
    cin >> planet;
}    
void calculatingWeight()
{
    double planetWeight;
    
    cout << fixed << showpoint << setprecision(2);

    if (planet == "Mercury")
        planetWeight = weight * MERCURY;
    
    else if (planet == "Venus")
        planetWeight = weight * VENUS;
        
    else if (planet == "Earth")
        planetWeight = weight * EARTH;
    
    else if (planet == "Moon")
        planetWeight = weight * MOON;
    
    else if (planet == "Mars")
        planetWeight = weight * MARS;
    
    else if (planet == "Jupiter")
        planetWeight = weight * JUPITER;
    
    else if (planet == "Saturn")
        planetWeight = weight * SATURN;
    
    else if (planet == "Uranus")
        planetWeight = weight * URANUS;
    
    else if (planet == "Neptune")
        planetWeight = weight * NEPTUNE;
    
    else if (planet == "Pluto")
        planetWeight = weight * PLUTO;
    
    else
    {
        cout << "Incorrect name of the celestial body." << endl;
        cout << "The name must be typed as: Mercury, Venus, Earth, Mars, " 
        "Jupiter, Saturn, Uranus, Neptune, Pluto, or Moon. " << endl;
        
        return;
    }
        cout << "Your weight of: " << weight << " lbs on " << planet << " is: " 
        << planetWeight << " lbs." << endl;
}


When I run the code it runs like it's supposed to, well almost.
My issue is when I hit 0 to exit the program it asks me to enter my weight and planet name even though I hit 0 to exit. What am I missing? That seems to be my only problem
Your main function calls userMenu(), which appears to be contain the main loop of your program.
After userMenu returns, userInput() called from main. Perhaps you should just remove lines 28 and 29.
Thank you! I realized that almost 5 minutes later
Topic archived. No new replies allowed.