与标题状态一样,当我将它们存储在数组中并尝试输出它时,我会得到随机的科学数字。我不确定他们为什么会改变,因为没有对数字进行任何操作。由于数字的变化,我得到了不正确的华氏温度。
样本输入:天0-6 = 12 我星期天= 0,星期一= 1.32657e。等数虽然输入保持不变,但数字是随机的,甚至不一样
int main()
{
const int DAYS_OF_WEEK = 7;
const double MAXIMUM_TEMPERATURE = 60;
const double MINIMUM_TEMPERATURE = -90 ;
double temperature;
int dayCount;
double fahrenheit;
double temperatures[6];
string daysOfWeek[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};//string array for the days of the week
for(int dayCount = 0; dayCount < DAYS_OF_WEEK; dayCount++)
{
cout << "Please enter the Temperature for " << daysOfWeek[dayCount] << " in Celsius: "; // re-prompt for temperature
cin >> temperature;
while(cin.fail())// checks to see if cin failed
{
cin.clear();
cout << "\nInvalid input. Only numeric values, please try again. "; // re-prompt
cin >> temperature; // try to get the value again
fflush(stdin); //clear input buffer
}//end while
if(temperature > MAXIMUM_TEMPERATURE || temperature < MINIMUM_TEMPERATURE)
{
cout << "\nInvalid input. Temperature must be between " << MINIMUM_TEMPERATURE <<" and " << MAXIMUM_TEMPERATURE <<". Please try again." << endl;// re-prompt user for temp between ranges
cin >> temperature;
}
else
{
temperature = temperatures[dayCount];
}//end else
}//end for
cout << "Temperatures for the Week.\n_________________________________________________";
for (int dayCount = 0; dayCount < DAYS_OF_WEEK; dayCount++)
{
cout << "\nTemperature for " << daysOfWeek[dayCount] << " in Celsius is: " << temperatures[dayCount];
fahrenheit = (5.0/9.0) * (temperatures[dayCount] + 32);
cout << "In Fahrenheit it is:" << fahrenheit;
} //end for
return 0;
}//end main
答案 0 :(得分:0)
我假设在这一行
{
temperature = temperatures[dayCount];
}//end else
你的意思是另一个方向
temperatures[dayCount] = temperature;
因为作业是左侧的右侧。
垃圾值的原因是温度未初始化并且其中包含垃圾值。在整个过程中,由于上述错误,您没有更改该数组的值。