如何在不进行屠宰的情况下将十进制数添加到当前工作代码中?

时间:2015-04-08 03:35:05

标签: c++ floating-point

我一直在破解这段代码,当我认为自己正在制定时,我似乎向前迈了一步,后退了两步。

我目前正在使用Visual Studios 2013更新4,我想知道是否有人可以帮助我在目前为止完成的工作代码中使用小数进行输出计算。

我尝试过使用double变量,但所有这一切都会引发1120错误或者什么都不做。

非常感谢任何帮助。

// Chapter 7 Problem #3
// Rainfall Statistics
// 4-7-15

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

// Functions prototypes
void getRain(string[], int[], int);
int getTotal(int[], int);
int getAve(int[], int);
int largestElement(int[], int);
int smallestElement(int[], int);

int main()
{
    // Costant for the number of Month entries
    const int Rain_Per_Month = 12;

    //Array of Months
    string names[Rain_Per_Month] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "Decemeber" };

    // Array of inches of each month
    int inches[Rain_Per_Month];

    //Total inches of rainfall
    int totalRainFall;

    // Ave Rain Fall
    int aveRainFall;

    // Subscript of the month that had the most rain
    int highRainFall;

    // Subscript of the month that had the least rain
    int lowRainFall;

    // Get the amount of rainfall in inches of each Month.
    getRain(names, inches, Rain_Per_Month);

    // Get total rainfall and Ave rainfall and high rainfall and low rainfall in inches.
    totalRainFall = getTotal(inches, Rain_Per_Month);
    aveRainFall = getAve(inches, Rain_Per_Month);
    highRainFall = largestElement(inches, Rain_Per_Month);
    lowRainFall = smallestElement(inches, Rain_Per_Month);

    // Display the Rainfall report header.
    cout << endl << endl;
    cout << "                         12 Month Rain Report \n\n";
    cout << "______________________________________________________________\n";

    // Display the and rain in inches of each month.

    for (int Rain_Per_Month = 0.00; Rain_Per_Month < Rain_Per_Month; Rain_Per_Month++)
    {
        cout << setw(6) << names[Rain_Per_Month]
            << setw(21) << inches[Rain_Per_Month]
            << endl;
    }

    // Display the total rainfall, highest rainfall, and lowest rainfall.
    cout << "\nThe total rainfall for the year was:" << setw(15) << totalRainFall << " inches" << endl;
    cout << "The average rainfall for the year is " << aveRainFall << endl;
    cout << "The largest amount of rainfall was " << inches[highRainFall] << " inches in " << names[highRainFall] << endl;
    cout << "The smallest amount of rainfall was " << inches[lowRainFall] << " inches in " << names[lowRainFall] << endl;
    cout << "\n" << endl;

    return 0;
}

/*********************************************************
*                         getRain                        *
* Gets the amount of rain in inches of each month from   *
* the user. The names array parameter holds the names of *
* the months, and the month array parameter will hold    *
* the amount of rainfall in iches of each corresponding  *
* month.                                                 *
**********************************************************/
void getRain(string names[], int inches[], int size)
{
    for (int month = 0.00; month < size; month++)
    {
        //Get the amount of Rainfall for each month.
        cout << "How many inches of rain fall for the month of " << names[month] << ": ";
        cin >> inches[month];

        // Validate the input.
        while (inches[month] < 0.00)
        {
            cout << "Inches of rain must be 0 or more. Please re-enter: ";
            cin >> inches[month];
        }
    }
}
/**********************************************************
*                           getTotal                      *
* Calculates and returns the total of the values stored in*
* the array passed to the function.                       *
***********************************************************/
int getTotal(int array[], int size)
{
    int total = 0.00;

    for (int pos = 0.00; pos < size; pos++)
        total += array[pos];

    return total;
}
/***********************************************************
*                           getAve                         *
* Calculates the total of the values stored in             *
* the array passed to the function and then averages them. *                       *
************************************************************/
int getAve(int array[], int size)
{
    int Total = 0.00;
    double Ave;

    for (int pos = 0.00; pos < size; pos++)
        Total += array[pos];
    Ave = Total / size;

    return Ave;
}
/**********************************************************
*                             largestElement              *
* Finds and returns the subscript of the array position   *
* holding the largest value in the array passed to the    *
* function.                                               *
***********************************************************/
int largestElement(int array[], int size)
{
    int indexOfLargest = 0.00;

    for (int pos = 1; pos < size; pos++)
    {
        if (array[pos] > array[indexOfLargest])
            indexOfLargest = pos;
    }
    return indexOfLargest;
}

/**********************************************************
*                      smallestElement                    *
* Finds and returns the subscript of the array position   *
* holding the smallest value in the array passed to the   *
* function.                                               *
***********************************************************/
int smallestElement(int array[], int size)
{
    int indexOfSmallest = 0.00;

    for (int pos = 1; pos < size; pos++)
    {
        if (array[pos] < array[indexOfSmallest])
            indexOfSmallest = pos;
    }
    return indexOfSmallest;
}

1 个答案:

答案 0 :(得分:0)

你似乎把'type'的概念与'variable'的概念混为一谈。每个变量都包含一个特定类型的数据,当你弄清楚你将用它做什么时,你必须自己声明。

例如:正好有12个月,所以你应该声明任何处理月份数的整数。

另一个例子:降雨量可以是几分之一英寸,因此任何处理降雨量,总量,平均值,最大值或最小值的事物都应声明为浮点类型,例如浮点数或双倍数。

最后,作为一般提示,请重新读取并修复变量名称。你有这个:

// Costant for the number of Month entries
const int Rain_Per_Month = 12;

但你显然是指Months_Per_Year。您会发现如果您修改名称以准确描述它们的含义,您可能会更好地理解您的代码。