I'm working on the following C++ homework question:
"Write a program that lets the user enter the total rainfall for each of 12 months into an array of doubles. The program should calculate and display the total rainfall for the year, the average monthly rainfall, and the months with the highest and lowest amounts.
Input Validation: Do no accept negative numbers for monthly rainfall figures."
The errors I am getting are:
Reading these, I guess it's safe to assume I have several mistakes in my code regarding making doubles into integers. I have combed through my code, looking for these mistakes. Those int variables I found (according to the line direction provided by Visual Studio 2012), I changed to double; but it only messes up 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 well over 20 errors regarding the 'counter' variables; errors saying something about "enum type."
Any help here would be greatly appreciated, this assignment is due by this Thursday afternoon (Oct 9).
This program does compile, but it gives me all kinds of garbage answers and excess garbage ones as well.
Here is my code thus far:
#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;
        }
    }
}
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 need to be doubles. You can declare them as such explicitly:
 double highestRain = rainData2[0];
 double lowestRain = rainData3[0];
You can also use the auto keyword. (See docs for Visual Studio 2012.) The advantages and disadvantages of this style are out of scope for this answer, but you probably should be aware of the language feature.
 auto highestRain = rainData2[0];
 auto lowestRain = rainData3[0];
Change
int highestRain = rainData2[0];
To
double highestRain = rainData2[0];
Same for lowest rain.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With