警告:“从'double'转换为'int',可能会丢失数据”

时间:2014-10-08 06:59:16

标签: c++

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

"编写一个程序,让用户将12个月中每个月的总降雨量输入一系列双打。该计划应计算并显示当年的总降雨量,月平均降雨量以及最高和最低量的月份。

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

我得到的错误是:

  • 警告2警告C4244:' =' :转换为' double'到' int',可能丢失数据
  • 警告4警告C4244:' =' :转换为' double'到' int',可能丢失数据
  • 警告1警告C4244:'初始化' :转换为' double'到' int',可能的损失 数据
  • 警告3警告C4244:'初始化' :转换自 '双'到' int',可能丢失数据

阅读这些内容,我想我可以安全地假设我的代码中有几个错误,即将双打整数转换成整数。我梳理了我的代码,寻找这些错误。我找到的那些int变量(根据Visual Studio 2012提供的行方向),我改为double;但它只会弄乱我的代码并产生更多错误。我甚至走出去,把每个int变量变成了一个双变量,只是被关于'计数器的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;
        }

    }
}

3 个答案:

答案 0 :(得分:1)

您写道:

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

由于您希望找到最高和最低值而不会舍入为整数,因此您的&#34;最好到目前为止&#34;变量需要加倍。您可以明确地声明它们:

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

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

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

答案 1 :(得分:1)

更改

int highestRain = rainData2[0];

double highestRain = rainData2[0];

同样的最低雨。

答案 2 :(得分:1)

首先,当你这样做时:

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”而不是“1月”?

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

要取消警告,请替换:

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

使用:

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

同样的事情:

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

将其更改为double。这摆脱了警告但没有给你更多像你提到的错误。