[英]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
. 由于
weightOnPlanet
是int
而weight * 0.4155
是double,您将失去精确度。您可能希望weightOnplanet
是double
而不是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.