Your Weight On Planets

This is a discussion on Your Weight On Planets within the C++ Programming forums, part of the General Programming Boards category; Hi, I'm doing another homework problem and I've been stuck for a couple of hours. The assignment is to find ...

  1. #1
    Registered User Trekkie's Avatar
    Join Date
    Oct 2005
    Posts
    20

    Your Weight On Planets

    Hi,

    I'm doing another homework problem and I've been stuck for a couple of hours. The assignment is to find your weight on the different planets. Right now, I've been able to tell the user their weight on ONE planet. How do I use cout statement to tell the user their weight on the different planets. HEre is what I have so far:
    Code:
    #include <iostream>
    #include <string>
    #include <cmath>
    
    using namespace std;
    int main()
    {
    	float weight;
    	char planet;
    
                    const float Mercury = 0.4155;
    	const float Venus = 0.8975;
    	const float Earth = 1.0;
    	const float Moon = 0.166;
    	const float Mars = 0.3507;
    	const float Jupiter = 2.5374;
    	const float Saturn = 1.0677;
    	const float Uranus = 0.8947;
    	const float Neptune = 1.1794;
    	const float Pluto = 0.0899;
    
    	cout<< "Enter your weight in pounds" <<endl;
    	cin>> weight;
    	cout<< "Enter the name of a planet" <<endl;
    	cin>> planet;
    
    	cout<< "Your weight on Mars is " <<weight*Mars <<"."<<endl;
    	return 0;
    }
    Thanks!

  2. #2
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    darn dbl post

  3. #3
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    I would use if statements (and string instead of char for the input).

    If the planet variable == "mars" then output the mars one, ect.
    Last edited by Enahs; 10-23-2005 at 07:13 PM.

  4. #4
    Registered User Trekkie's Avatar
    Join Date
    Oct 2005
    Posts
    20
    Thanks for your help.

    I think I am finally moving forward, but now my output is not coming out. I didn't use string instead of char just because I had a ton of errors when I tried to switch the two. Here are the changes that I made. Can anyone tell me why there is no output?

    Code:
    #include <iostream>
    #include <string>
    #include <cmath>
    
    using namespace std;
    int main()
    {
    	float weight;
    	char planet;
    
        const double Mercury = 0.4155;
    	const double Venus = 0.8975;
    	const double Earth = 1.0;
    	const double Moon = 0.166;
    	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;
    
    	cout<< "Enter your weight in pounds" <<endl;
    	cin>> weight;
    	cout<< "Enter the name of a planet" <<endl;
    	cin>> planet;
    	{
    	if (planet==Mercury)
    		cout<< "Your weight on Mercury is " <<weight*Mercury <<"."<<endl;
    	else if (planet==Venus)
    		cout<< "Your weight on Venus is "<<weight*Venus <<"."<<endl;
    	else if (planet==Earth)
    		cout<< "Your weight on Earth is " <<weight*Earth <<"."<<endl;
    	else if (planet==Moon)
    		cout<< "Your weight on the Moon is "<<weight*Moon <<"."<<endl;
    	else if (planet==Mars)
    		cout<< "Your weight on Mars is "<<weight*Mars<<"."<<endl;
    	else if (planet==Jupiter)
    		cout<< "Your weight on Jupiter is "<<weight*Jupiter<<"."<<endl;
    	else if (planet==Saturn)
    		cout<< "Your weight on Saturn is "<<weight*Saturn<<"."<<endl;
    	else if (planet==Uranus)
    		cout<< "Your weight on Uranus is "<<weight*Uranus<<"."<<endl;
    	else if (planet==Neptune)
    		cout<< "Your weight on Neptune is "<<weight*Neptune<<"."<<endl;
    	else if (planet==Pluto)
    		cout<< "Your weight on Pluto is "<<weight*Pluto<<"."<<endl;
    }
    
    	return 0;
    }

  5. #5
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    Char only holds one letter.
    So put that letter in your if statement in 'X'

    so
    Code:
    if (planets == 'X')
    But that will not work here, because you have three things that start with the letter M.

    Here it is with strings:

    Code:
    #include <iostream>
    #include <string>
    #include <cmath>
    
    using namespace std;
    int main()
    {
    	float weight;
    	string planet;
    
        const double Mercury = 0.4155;
    	const double Venus = 0.8975;
    	const double Earth = 1.0;
    	const double Moon = 0.166;
    	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;
    
    	cout<< "Enter your weight in pounds" <<endl;
    	cin>> weight;
    	cout<< "Enter the name of a planet" <<endl;
    	cin>> planet;
    	{
    	if (planet=="Mercury")
    		cout<< "Your weight on Mercury is " <<weight*Mercury <<"."<<endl;
    	if (planet=="Venus")
    		cout<< "Your weight on Venus is "<<weight*Venus <<"."<<endl;
    	if (planet=="Earth")
    		cout<< "Your weight on Earth is " <<weight*Earth <<"."<<endl;
    	if (planet=="Moon")
    		cout<< "Your weight on the Moon is "<<weight*Moon <<"."<<endl;
    	if (planet=="Mars")
    		cout<< "Your weight on Mars is "<<weight*Mars<<"."<<endl;
    	if (planet=="Jupiter")
    		cout<< "Your weight on Jupiter is "<<weight*Jupiter<<"."<<endl;
    	if (planet=="Saturn")
    		cout<< "Your weight on Saturn is "<<weight*Saturn<<"."<<endl;
    	if (planet=="Uranus")
    		cout<< "Your weight on Uranus is "<<weight*Uranus<<"."<<endl;
    	if (planet=="Neptune")
    		cout<< "Your weight on Neptune is "<<weight*Neptune<<"."<<endl;
    	if (planet=="Pluto")
    		cout<< "Your weight on Pluto is "<<weight*Pluto<<"."<<endl;
    }
    
    	return 0;
    }

    Now, if the user does not enter the first letter capitalized it will not work.
    Because “Mars” is different than “mars”.

    You can either put another set of if statements and do the exact same but check for the first letter lower case. Tell the user they have to input the first letter upper case, or they have to input lower case (and just check for lower case). Ect

    I would also highly recommend you put a else statement saying something like “Planet name not recognized”, or something similar.

    P.S. Your multiplication factors are wrong and are not giving you the correct answers!

  6. #6
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Code:
    #include <iostream>
    #include <string>
    #include <cmath>
    
    using namespace std;
    int main()
    {
    	float weight;
    	string planet;
    
        const double Mercury = 0.4155;
    	const double Venus = 0.8975;
    	const double Earth = 1.0;
    	const double Moon = 0.166;
    	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;
    
    	cout<< "Enter your weight in pounds" <<endl;
    	cin>> weight;
    	cout<< "Enter the name of a planet" <<endl;
    	cin>> planet;
        cin.ignore();
    	if (planet=="Mercury")
    		cout<< "Your weight on Mercury is " <<weight*Mercury <<"."<<endl;
    	else if (planet=="Venus")
    		cout<< "Your weight on Venus is "<<weight*Venus <<"."<<endl;
    	else if (planet=="Earth")
    		cout<< "Your weight on Earth is " <<weight*Earth <<"."<<endl;
    	else if (planet=="Moon")
    		cout<< "Your weight on the Moon is "<<weight*Moon <<"."<<endl;
    	else if (planet=="Mars")
    		cout<< "Your weight on Mars is "<<weight*Mars<<"."<<endl;
    	else if (planet=="Jupiter")
    		cout<< "Your weight on Jupiter is "<<weight*Jupiter<<"."<<endl;
    	else if (planet=="Saturn")
    		cout<< "Your weight on Saturn is "<<weight*Saturn<<"."<<endl;
    	else if (planet=="Uranus")
    		cout<< "Your weight on Uranus is "<<weight*Uranus<<"."<<endl;
    	else if (planet=="Neptune")
    		cout<< "Your weight on Neptune is "<<weight*Neptune<<"."<<endl;
    	else if (planet=="Pluto")
    		cout<< "Your weight on Pluto is "<<weight*Pluto<<"."<<endl;
    
        cin.get();
    	return 0;
    }
    I couldn't figure out why you had {} around all your if/else so i removed all of those. I don't know how to use char like that, so I changed to strings and added " " around the planet names. Also I added cin.ignore() after the cin to get the planet input, that way the cin.get() i put at the end (cause I am too lazy to goto dos to be able to see the results without having a pause at the end)wouldn't get ignored.

  7. #7
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Another tip i would suggest is a switch/case, I love these as then I don't have to type out else if 30 times and it looks cleaner imho.

  8. #8
    Registered User Trekkie's Avatar
    Join Date
    Oct 2005
    Posts
    20
    Thanks again for all your help.

    I made some more changes, and everything looks pretty good now. I have one more question. You mentioned in your last post that I should "You can either put another set of if statements and do the exact same but check for the first letter lower case." How would I do that? I figured out how to make sure the user enters a correct weight range, but I have no idea how to make sure the user enters a capital letter.

    Also, wouldn't the weight on Mars be your weight multiplied by 0.3507?

    Code:
    #include <iostream>
    #include <string>
    #include <cmath>
    
    using namespace std;
    int main()
    {
    	float weight;
    	string planet;
    	bool dataOK;
    
        const double Mercury = 0.4155;
    	const double Venus = 0.8975;
    	const double Earth = 1.0;
    	const double Moon = 0.166;
    	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;
    
    	cout<< "Enter your weight in pounds" <<endl;
    	cin>> weight;
    	cout<< "Enter the name of a planet using first letter as uppercase" <<endl;
    	cin>> planet;
    	if (weight <=20)
    		dataOK = false;
    	else
    		dataOK = true;
    	if ( dataOK)
    
    	{
    	if (planet=="Mercury")
    		cout<< "Your weight on Mercury is " <<weight*Mercury <<"."<<endl;
    	if (planet=="Venus")
    		cout<< "Your weight on Venus is "<<weight*Venus <<"."<<endl;
    	if (planet=="Earth")
    		cout<< "Your weight on Earth is " <<weight*Earth <<"."<<endl;
    	if (planet=="Moon")
    		cout<< "Your weight on the Moon is "<<weight*Moon <<"."<<endl;
    	if (planet=="Mars")
    		cout<< "Your weight on Mars is "<<weight*Mars<<"."<<endl;
    	if (planet=="Jupiter")
    		cout<< "Your weight on Jupiter is "<<weight*Jupiter<<"."<<endl;
    	if (planet=="Saturn")
    		cout<< "Your weight on Saturn is "<<weight*Saturn<<"."<<endl;
    	if (planet=="Uranus")
    		cout<< "Your weight on Uranus is "<<weight*Uranus<<"."<<endl;
    	if (planet=="Neptune")
    		cout<< "Your weight on Neptune is "<<weight*Neptune<<"."<<endl;
    	if (planet=="Pluto")
    		cout<< "Your weight on Pluto is "<<weight*Pluto<<"."<<endl;
    }
    	else 
    		cout << "Invalid weight, weight must be more than 20 pounds" 
    		     << " and planet name must begin "
    			 << "with a capital letter."
    			 << endl;
    	return 0;
    }

  9. #9
    Banned
    Join Date
    Oct 2005
    Posts
    4
    Quote Originally Posted by Wraithan
    Another tip i would suggest is a switch/case, I love these as then I don't have to type out else if 30 times and it looks cleaner imho.
    switch/case blows. It only works with literals and macros. It won't work with strings or constants for that matter. Stick to if-else.

  10. #10
    Registered User Trekkie's Avatar
    Join Date
    Oct 2005
    Posts
    20
    This is only my second program... I don't even know what switch/case is!

    I will try and remember that for in the future.

  11. #11
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    How would I do that?
    What you have:
    Code:
     if (planet=="Mercury")
    		cout<< "Your weight on Mercury is " <<weight*Mercury <<"."<<endl;
    Can be like this:

    Code:
     if (planet=="Mercury")
    		cout<< "Your weight on Mercury is " <<weight*Mercury <<"."<<endl;
    if (planet=="mercury")
    		cout<< "Your weight on Mercury is " <<weight*Mercury <<"."<<endl;
    Now, there is ways with strings and such to change case; but since you said this is your 2nd project we will just stay with that.


    Also, wouldn't the weight on Mars be your weight multiplied by 0.3507?
    No. Sorta, close. Here are more accurate multiples:

    Code:
    Mercury 0.378
    Venus 0.907
    Earth 1
    Moon .166
    Mars .379
    Jupiter 2.529
    Saturn 1.066
    Uranus 0.903
    Neptune 1.096
    Pluto 0.069
    But if your teacher gave you the original numbers use them. Just saying, that are not totally correct

  12. #12
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Man this could be a real fun project So many ways to do it.

    I'd probably take the fun overly complicated route
    Code:
    #include <map>
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
            map<string,double> planets;
            planets["Mercury"] = 0.4155;
            planets["Venus"] = 0.8975;
            planets["Earth"] = 1.0;
            planets["Moon"] = 0.166;
            planets["Mars"] = 0.3507;
            planets["Jupiter"] = 2.5374;
            planets["Saturn"] = 1.0677;
            planets["Uranus"] = 0.8947;
            planets["Neptune"] = 1.1794;
            planets["Pluto"] = 0.0899;
    
            double weight;
            cout<< "Enter your weight in pounds" <<endl;
            cin>> weight;
            string planet;
            cout<< "Enter the name of a planet using first letter as uppercase" <<endl;
            cin>> planet;
            map<string,double>::const_iterator it= planets.find(planet);
            if ( it != planets.end() )
            {
                    double fac = it->second;
                    cout<<"Your weight on "<<planet<<" is "<<(weight*fac)<<'.'<<endl;
            }
            else
            {
                    cout<<"Invalid planet specified"<<endl;
            }
    }
    Of course if you turned that in your instructor will know you copied it

  13. #13
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Generally when you compare strings with user input, you convert to all lowercase or all caps first. Look into the tolower or toupper functions (and I think converting entire strings to lower/uppercase is covered in the faq on this site as well)
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  14. #14
    Registered User Trekkie's Avatar
    Join Date
    Oct 2005
    Posts
    20
    Thank you for all your help. It's taken me a little over 9 hours to do this program. I hope they get easier! I guess it would help if I knew in advance that "char" held only one letter. It's the little things like that that has me pulling my hair out.

  15. #15
    Registered User
    Join Date
    Oct 2005
    Posts
    13
    you wouldnt happen to be a Mr. Baker's class would you? because i remember that exact problem from many years back.

    or could just be that its a common problem

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Weight Balanced Tree - Implementation
    By paulovitorbal in forum C Programming
    Replies: 1
    Last Post: 10-31-2007, 03:28 PM
  2. some design advice
    By viaxd in forum Game Programming
    Replies: 0
    Last Post: 08-03-2006, 07:08 AM
  3. Double to Int conversion warning
    By wiznant in forum C Programming
    Replies: 15
    Last Post: 09-19-2005, 10:25 PM
  4. Guaranteed weight loss
    By Glirk Dient in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 05-12-2004, 07:11 AM
  5. Replies: 1
    Last Post: 10-18-2003, 10:15 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21