【发布时间】: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];
-
在
totalAndAverage、getHighest和getLowest这三个函数中,为什么在@987654328中将arg初始化为0时首先发送int@ 环形?警告:将累积变量更改为double:lowestRain、highestRain等 -
应该是数组索引
-
@Yulia,是的,我知道它是数组索引,但是您没有使用作为函数参数传递的值。
标签: c++