简体   繁体   English

C ++将输入字符串与字符串进行比较会给我一个从双精度到整数转换的警告-可能会丢失数据

[英]C++ comparing input string with a string gives me a warning of conversion from double to int - possible data loss

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. 由于某种原因,我得到了错误:“'=':从'double'到'int'的转换,可能会丢失数据。

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;
}

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")

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 . 由于weightOnPlanetintweight * 0.4155是double,您将失去精确度。您可能希望weightOnplanetdouble而不是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. 只需提及if (planet == Mercury)是否会将planet与一个空字符串进行比较,这就是为什么您会得到不想要的结果的原因,直到您执行@ ErrorAtLine0提到的操作为止。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C ++警告C4244&#39;返回&#39;:从&#39;double&#39;转换为&#39;int&#39;,可能丢失数据Visual Studio 2015 - C++ Warning C4244 'return': conversion from 'double' to 'int', possible loss of data Visual Studio 2015 警告:“从&#39;double&#39;转换为&#39;int&#39;,可能会丢失数据” - Warning: “conversion from 'double' to 'int', possible loss of data” 警告C4244:“参数”:从“双精度”转换为“整数”,可能丢失数据 - Warning C4244: 'argument' : conversion from 'double' to 'int', possible loss of data 警告C4244:&#39;argument&#39;:从&#39;double&#39;转换为&#39;const int&#39;,可能会丢失数据 - warning C4244: 'argument' : conversion from 'double' to 'const int', possible loss of data 警告C4244:&#39;argument&#39;:从&#39;double&#39;转换为&#39;float&#39;,可能会丢失数据 - c ++ - warning C4244: 'argument' : conversion from 'double' to 'float', possible loss of data - c++ 警告C4244:&#39;argument&#39;:从&#39;time_t&#39;转换为&#39;unsigned int&#39;,可能丢失数据 - C ++ - warning C4244: 'argument' : conversion from 'time_t' to 'unsigned int', possible loss of data — C++ 在C ++中从double / int / string自动转换为bool - Automatic conversion from double/int/string to bool in C++ 警告C4244 \\&#39;=&#39;:从&#39;int&#39;转换为&#39;char&#39;,可能会丢失数据? - Warning C4244 \ '=': conversion from 'int' to 'char', possible loss of data? <where do I convert?>警告C4244:“正在初始化”:从“ __int64”转换为“ int”,可能丢失数据 - <where do I convert?> warning C4244: 'initializing' : conversion from '__int64' to 'int', possible loss of data c ++从“ float”到“ int32”的转换,可能会丢失数据 - c++ conversion from 'float' to 'int32', possible loss of data
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM