Warning: "conversion from" double "to" int ", possible data loss"

I am working on the following C ++ homework question:

"Write a program that allows the user to enter the total rainfall for each of the 12 months in a paired array. The program should calculate and display the total rainfall for the year, the average monthly rainfall, and the highest and lowest months."

Introductory validation: Do not accept negative numbers for monthly precipitation numbers.

The errors I am getting are as follows:

  • Warning 2 Warning C4244: '=': conversion from 'double' to 'int', possible data loss
  • Warning 4 warning C4244: '=': conversion from 'double' to 'int', possible data loss
  • Warning 1 warning C4244: 'initializing': conversion from 'double' to 'int', possible data loss
  • Warning 3 Warning C4244: "initialization": conversion from 'double' to 'int', possible data loss

As I read this data, I am sure that I am sure that I have several mistakes in my code regarding making double numbers into integers. I've been combing through my code looking for these errors. These int variables I found (according to the line direction provided by Visual Studio 2012) I changed to double; but this only clogs my code more and creates more errors. I even went out and made every int variable into a double variable, only to be smacked by over 20 errors regarding counter variables; errors about "enumerated type".

Any help here would be greatly appreciated, this assignment is related to this Thursday (Oct 9th) day.

This program compiles, but it gives me all sorts of answers to garbage and extra garbage products.

Here is my code:

#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


source to share


3 answers


You wrote:

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

      

Since you want to find the highest and lowest values ​​without rounding to an integer, your "best so far" variables must be doubled. You can declare them as such explicitly:



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

      

You can also use a keyword auto

. (See the docs for Visual Studio 2012. ) The advantages and disadvantages of this style don't fit this answer, but you should probably know the language feature.

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

      

+1


source


Edit

int highestRain = rainData2[0];

      

For



double highestRain = rainData2[0];

      

The same goes for the lowest rainfall.

+1


source


First of all, when you do this:

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

      

you don't need "SIZE", so you can change it to:

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

      

C ++ can count the number of elements in an array.

Instead of saying:

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

      

Replace (index+1)

with months[index]

. Who wants to read "1" instead of "January"?

As an aside, you don't have any mistakes! These are warnings, so they don't make your program work.

To get warnings, replace:

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

      

from:

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

      

And the same for:

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

      

Change this to double

. This got rid of the warnings, but didn't give me more errors as you mentioned.

+1


source







All Articles