Reputation: 37
i have to write a program that asks user for weight and a planet name. Program has to output how much user would weight on that planet. For some reason i get errors: " '=': conversion from 'double' to 'int', possible loss of data.
Before i was trying to use (planet == Mercury) but still no luck.
Here is my code:
// ConsoleApplication5.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <string>
#include <sstream>
#include "ConsoleApplication5.h"
using namespace std;
int main()
{
cout << "Assignment 05" << endl;
int weight = 0;
string planet = "";
int weightOnplanet;
string entry = "You entered a weight of ";
string Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune, Pluto;
cout << "Please Enter Weight <as an integer of pounds>: " << endl;
cin >> weight;
cout << "Please Enter Planet name <ie: Earth>: " << endl;
cin >> planet;
cout << entry << weight << " and a planet name of " + planet << endl;
if (planet == Mercury)
{
weightOnplanet = weight * 0.4155;
cout << "On a " + planet + " your weight in pounds would be " << weightOnplanet << endl;
}
else if (planet == Venus)
{
weightOnplanet = weight * 0.8975;
cout << "On a " + planet + " your weight in pounds would be " << weightOnplanet << endl;
}
else if (planet == Earth)
{
weightOnplanet = weight * 1.0;
cout << "On a " + planet + " your weight in pounds would be " << weightOnplanet << endl;
}
else if (planet == Mars)
{
weightOnplanet = weight * 0.3507;
cout << "On a " + planet + " your weight in pounds would be " << weightOnplanet << endl;
}
else if (planet == Jupiter)
{
weightOnplanet = weight * 2.5374;
cout << "On a " + planet + " your weight in pounds would be " << weightOnplanet << endl;
}
else if (planet == Saturn)
{
weightOnplanet = weight * 1.0677;
cout << "On a " + planet + " your weight in pounds would be " << weightOnplanet << endl;
}
else if (planet == Uranus)
{
weightOnplanet = weight * 0.8947;
cout << "On a " + planet + " your weight in pounds would be " << weightOnplanet << endl;
}
else if (planet == Neptune)
{
weightOnplanet = weight * 1.1794;
cout << "On a " + planet + " your weight in pounds would be " << weightOnplanet << endl;
}
else if (planet == Pluto)
{
weightOnplanet = weight * 0.0899;
cout << "On a " + planet + " your weight in pounds would be " << weightOnplanet << endl;
}
else
cout << "Unknown Planet" << endl;
system("pause");
return 0;
}
Upvotes: 0
Views: 62
Reputation: 7542
The warning you get is for
weightOnplanet = weight * 0.4155;
Since weightOnPlanet
is int
and weight * 0.4155
is double you will be loosing precision.You probably want weightOnplanet
to be double
and not int
.
Just to mention if (planet == Mercury)
would be comparing planet
to an empty string which is why you would get undesired result, until you do what @ErrorAtLine0 mentioned.
Upvotes: 2
Reputation: 189
Each planet variable (Mercury, Venus, etc) should have an actual value such as:
Mercury = "Mercury";
A string doesnt initialize with the name of the variable, you have to actually give it a value.
You don't even have to have a variable for every planet. You could just do this:
if(planet == "Mercury")
Upvotes: 3