It's my first code. I put alot of effort into it. I just haven't been able to see if it actually works because tomorrow I go the the computer lab to test it. Positive and negative feedback is welcomed. Also, here is a link to the description of what the code is suppose to have.
Link: http://pdfcast.org/pdf/assignment

#include<iostream> 
#include<cmath> 
using namespace std;

int CheckWeight(int weight, string Planet);

int main()
{
int weight,res;
string planetName;

do{
cout << "Please enter your weight" <<endl;
cin >> weight;}
while(cin.good()==false);
cout << " " << endl;
do{ 
cout << "Please enter a planet name from the following list" << endl;
cout << "Mercury, Venus, Earth, Moon, Mars, Jupiter, Saturn, Uranus, Neptune, Pluto" << endl;
cin >> planetName;
cout << " " << endl;
// Call function to check weight
res = CheckWeight(weight, planetName);
}while(res==1);
system("PAUSE");
return 0;
}// end main()

// Function for calculating weight

int CheckWeight(int weight, string Planet)
{
int Result=0;
if(Planet== "Mercury"){
cout << "Your weight on Mercury is " << (weight*0.4155)<< endl;
}
else if(Planet== "Venus"){
cout << "Your weight on Venus is " << (weight*0.8975)<< endl;
}
else if(Planet== "Earth"){
cout << "Your weight on Earth is " << (weight*1.0)<< endl;
}
else if(Planet== "Moon"){
cout << "Your weight on Moon is " << (weight*0.166)<< endl;
}
else if(Planet== "Mars"){
cout << "Your weight on Mars is " << (weight*0.3507)<< endl;
}
else if(Planet== "Jupiter"){
cout << "Your weight on Jupiter is " << (weight*2.5374)<< endl;
}
else if(Planet== "Saturn"){
cout << "Your weight on Saturn is " << (weight*1.0677)<< endl;
}
else if(Planet== "Uranus"){
cout << "Your weight on Uranus is " << (weight*0.8947)<< endl;
}
else if(Planet== "Neptune"){
cout << "Your weight on Neptune is " << (weight*1.1794)<< endl;
}
else if(Planet== "Pluto"){
cout << "Your weight on Pluto is " << (weight*0.0899)<< endl;
}
else{
cout << "You entered a wrong planet name. Please try again " << endl;
cout << " " << endl;
Result=1;
}
return Result;
}

Recommended Answers

All 5 Replies

new the beginning a meant to say "#include iostream" and "#include cmath"

new the beginning a meant to say "#include iostream" and "#include cmath"

No -- the way he has it is correct. The name of standard c++ header files go in angle brackets as shown in the code he posted.

Meh it works except... If u enter "pluto" instead of "Pluto" (capital letter P).. then it gives u an error and asks u to re-enter the planet name.. Other than that, it works as it should.. Another thing: If I enter a decimal, it wont work as the weight is not of type double.

To fix the capitalization problem you should convert the entered string to all lower-case or all upper-case, or use a case-insensitive comparison function.

Or in a more compact way:

const string PLANETS[10] = {"mercury", "venus", "earth", "moon", "mars", "jupiter", "saturn", "uranus", "neptune", "pluto"};
const double GRAVITY[10] = {0.4155, 0.8975, 1.0, 0.166, 0.3507, 2.5374, 1.0677, 0.8947, 1.1794, 0.0899};

string strLower(string s) {
    for(int i = 0; i < s.length(); i++)
        s[i] = tolower(s[i]);
    return s;
}

double CheckWeight(double weight, string planet) {
    planet = strLower(planet);
    for(int i = 0; i < 10; i++)
        if(planet == PLANETS[i])
            return weight * GRAVITY[i];
}

Now instead of having so many else if statements, you could just say:

cout << "Your weight on " << planet << " is " << CheckWeight(weight, planet) << endl;

Also, you need the string header file and not cmath.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.