【问题标题】:Warning: "conversion from 'double' to 'int', possible loss of data"警告:“从 'double' 转换为 'int',可能丢失数据”
【发布时间】:2014-10-08 06:59:16
【问题描述】:

我正在处理以下 C++ 作业问题:

“编写一个程序,让用户将 12 个月中每个月的总降雨量输入到一个双精度数组中。该程序应计算并显示一年的总降雨量、月平均降雨量和最高的月份和最低金额。

输入验证:月降雨量数据不接受负数。"

我得到的错误是:

  • 警告 2 警告 C4244:“=”:从“double”转换为“int”,可能会丢失数据
  • 警告 4 警告 C4244:“=”:从“double”转换为“int”,可能会丢失数据
  • Warning 1 警告 C4244:'initializing' : 从 'double' 转换为 'int',可能丢失 数据
  • 警告 3 警告 C4244:“正在初始化”:从 'double' 到 'int',可能会丢失数据

阅读这些,我想可以肯定地假设我的代码中有几个关于将双精度数转换为整数的错误。我梳理了我的代码,寻找这些错误。我找到的那些int变量(根据Visual Studio 2012提供的线方向),我改成double;但它只会使我的代码更加混乱并产生更多错误。我什至把每个 int 变量都变成了一个 double 变量,结果却被 20 多个关于“计数器”变量的错误所击中;关于“枚举类型”的错误。

我们将非常感谢您提供任何帮助,此作业应在本周四下午(10 月 9 日)之前完成。

这个程序确实可以编译,但它给了我各种垃圾答案和多余的垃圾答案。

到目前为止,这是我的代码:

#include<iostream>
#include<string>
using namespace std;

//Function prototypes
void totalAndAverage(int, double []);
void getHighest(int, double [], string []);
void getLowest(int, double [], string []);


//Global integer
const int SIZE = 12;
int main()
{
    //Create arrays
    double rainfallAmount[SIZE];
    int index;
    string months[SIZE] = { "January", "February", "March",
                         "April", "May", "June", 
                         "July", "August", "September", 
                         "October", "November", "December" };

    //Get the amount of rain for each month
    for ( index = 0; index < SIZE; index++)
    {
        cout << "Please enter the amount of rain (in inches) for month " << (index + 1) << ": " << endl; //+1 to not skip an array element
        cin >> rainfallAmount[index];

            //input validation, do not accept negative numbers
            while (rainfallAmount[index] < 0)
            {
                cout << "You cannot have a negative amount of rainfall. Please enter the amount of rain." << endl;
                cin >> rainfallAmount[index];
            }

    }

    //Call function to find total and average
    totalAndAverage(index, rainfallAmount);

    //Call function to find highest
    getHighest(index, rainfallAmount, months);

    //Call function o find lowest
    getLowest(index, rainfallAmount, months);

system("pause");
return 0;

}



void totalAndAverage(int i, double rainData[])
{
    double total = 0, average;

    //Find the total and average
    for (i = 0; i < SIZE; i++)
    {
        total += rainData[i];
    }
    //Display total
    cout << "The total rainfall is: " << total << endl;

    //Find average and display
    average = total / SIZE;
    cout << "The average monthly rainfall is: " << average << endl;
}



void getHighest(int iCount, double rainData2[], string monthData[])
{
    string highestMonth;

    //Initialize highest
    int highestRain = rainData2[0];

    //Find the highest
    for (iCount = 0; iCount < SIZE; iCount++)
    {
        if (rainData2[iCount] > highestRain)
        {
            highestRain = rainData2[iCount];
            highestMonth = monthData[iCount];

            //Display highest
            cout << highestMonth << " had the most rainfall this year with " << highestRain << " inches." << endl;
        }

    }
}



void getLowest(int counter, double rainData3[], string monthDataCopy[])
{
    string lowestMonth;

    //Initialize highest
    int lowestRain = rainData3[0];

    //Find the highest
    for (counter = 0; counter < SIZE; counter++)
    {
        if (rainData3[counter] > lowestRain)
        {
            lowestRain = rainData3[counter];
            lowestMonth = monthDataCopy[counter];

            //Display highest
            cout << lowestMonth << " had the least rainfall this year with " << lowestRain << " inches." << endl;
        }

    }
}

【问题讨论】:

  • 错误在哪几行?
  • 错误行对应顺序为:highestRain = rainData2[iCount];最低雨=rainData3[计数器]; int最高雨=rainData2[0]; int minimumRain = rainData3[0];
  • totalAndAveragegetHighestgetLowest这三个函数中,为什么在@987654328中将arg初始化为0时首先发送int @ 环形?警告:将累积变量更改为doublelowestRainhighestRain
  • 应该是数组索引
  • @Yulia,是的,我知道它是数组索引,但是您没有使用作为函数参数传递的值。

标签: c++


【解决方案1】:

你写道:

 int highestRain = rainData2[0];
 int lowestRain = rainData3[0];

由于您想在不四舍五入的情况下找到最高值和最低值,因此您的“迄今为止最佳”变量需要为双精度值。您可以明确声明它们:

 double highestRain = rainData2[0];
 double lowestRain = rainData3[0];

您也可以使用auto 关键字。 (见docs for Visual Studio 2012。)这种风格的优点和缺点超出了这个答案的范围,但你可能应该注意语言特性。

 auto highestRain = rainData2[0];
 auto lowestRain = rainData3[0];

【讨论】:

  • 我明白了...但我的输出仍然不正确...据说 2 月的降雨量最多,为 3 英寸,它显示 4 月的降雨量最多,为 3.4 英寸(这是真的)。 ..但它对最低的说同样的话;月份和降雨量。
  • 啊,那可能是因为rainData3[counter] &gt; lowestRain 应该是rainData3[counter] &lt; lowestRain
  • 您将能够通过单步调试调试器中的代码来捕获类似的错误。您注意到的下一个错误可能是从未打印过一月,有时会打印多个月。要解决这个问题,请考虑相对于for 循环,字符串显示代码的位置。
  • 感谢您的所有帮助,但我已经尝试过了。我仍然不正确(并且以不同的数字重复相同的月份)。我现在又在浏览代码了
  • 从你说的...听起来好像我有一个错误
【解决方案2】:

改变

int highestRain = rainData2[0];

double highestRain = rainData2[0];

最低降雨量也是如此。

【讨论】:

    【解决方案3】:

    首先当你这样做时:

    string months[SIZE] = { "January", "February", "March",
                             "April", "May", "June",
                             "July", "August", "September", 
                             "October", "November", "December" };
    

    你不需要“SIZE”,所以你可以把它改成:

    string months[] = { "January", "February", "March",
                        "April", "May", "June",
                        "July", "August", "September", 
                        "October", "November", "December" };
    

    C++ 可以计算数组中有多少元素。

    而不是说:

    cout << "Please enter the amount of rain (in inches) for month " << (index + 1) << ": " << endl; 
    

    (index+1) 替换为months[index]。谁愿意读“1”而不是“一月”?

    除此之外,您没有任何错误!这些是警告,因此它们不会使您的程序停止工作。

    要消除警告,请替换:

    //Initialize highest
    int highestRain = rainData2[0];
    

    与:

    //Initialize highest
    double highestRain = rainData2[0];
    

    同样的事情:

    //Initialize highest
    int lowestRain = rainData3[0];
    

    将其更改为 double。这消除了警告,但没有像你提到的那样给我更多的错误。

    【讨论】:

    • Re “这些是警告,它们不会使您的程序无法运行。” ...在这种情况下我不得不不同意。如果 1 月为 5.9 英寸,2 月为 5.1 英寸,带有 int highestRain 的代码将错误地比较 5.1 > 5 到 2 月,它会报告 2 月的降雨量最多,为 5 英寸。程序不会崩溃,但行为会出现微妙的错误。
    • 我的意思是它允许您构建和调试,但感谢您指出这一点。
    猜你喜欢
    • 2021-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-30
    • 1970-01-01
    相关资源
    最近更新 更多