C++ C++;将输入字符串与字符串进行比较会警告我从double转换为int-可能会丢失数据

C++ C++;将输入字符串与字符串进行比较会警告我从double转换为int-可能会丢失数据,c++,visual-c++,C++,Visual C++,我必须编写一个程序,要求用户提供重量和行星名称。程序必须输出用户在那个星球上的体重。由于某些原因,我得到错误:“'=”:从“double”转换为“int”,可能会丢失数据 在我尝试使用(行星=水星)之前,但仍然没有运气 这是我的密码: // ConsoleApplication5.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <iostream&

我必须编写一个程序,要求用户提供重量和行星名称。程序必须输出用户在那个星球上的体重。由于某些原因,我得到错误:“'=”:从“double”转换为“int”,可能会丢失数据

在我尝试使用(行星=水星)之前,但仍然没有运气

这是我的密码:

    // 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;
}
//ConsoleApplication5.cpp:定义控制台应用程序的入口点。
//
#包括“stdafx.h”
#包括
#包括
#包括
#包括“控制台应用程序5.h”
使用名称空间std;
int main()
{
库特星球;
cout每个行星变量(水星、金星等)都应该有一个实际值,例如:

Mercury = "Mercury";
字符串不是用变量名初始化的,你必须给它一个值

你甚至不必为每颗行星都设定一个变量。你可以这样做:

if(planet == "Mercury")
每个行星变量(水星、金星等)应具有实际值,例如:

Mercury = "Mercury";
字符串不是用变量名初始化的,你必须给它一个值

你甚至不必为每颗行星都设定一个变量。你可以这样做:

if(planet == "Mercury")

你得到的警告是为了

weightOnplanet = weight * 0.4155;
由于
weightOnPlanet
int
并且
weight*0.4155
是双倍,您将失去精度。您可能希望
weightOnPlanet
double
而不是
int


只需提及
如果(planet==Mercury)
会将
planet
与空字符串进行比较,这就是为什么您会得到不想要的结果,直到您执行@ErrorAtLine0提到的操作。

您收到的警告是针对

weightOnplanet = weight * 0.4155;
由于
weightOnPlanet
int
并且
weight*0.4155
是双倍,您将失去精度。您可能希望
weightOnPlanet
double
而不是
int


<> >只需提到<代码>(行星=水星)<代码>将比较<代码>行星<代码>空字符串,这就是为什么你会得到不希望的结果,直到你做了@ ErrRotry0所提到的。

谢谢,那个固定的“警告”警报。谢谢解释,我很感激-因为我刚开始学习C++。谢谢,那是固定的。警告“谢谢”,谢谢你的解释,我很感激,因为我刚开始学习C++。