获取随机科学数字而不是输入
Getting Random Scientific numbers instead of inputs
与标题状态一样,当我将它们存储在数组中并尝试输出时,我会得到随机的科学数字。我不知道为什么它们会改变,因为没有对数字进行运算。由于数字的变化,我得到了不正确的华氏温度。
样本输入:0-6=12天我得到sunday=0,monday=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
我假设在这行
{
temperature = temperatures[dayCount];
}//end else
你指的是的另一个方向
temperatures[dayCount] = temperature;
因为分配是从右边到左边。
垃圾值的原因是温度没有初始化,其中有垃圾值。在整个过程中,由于上面的错误,您没有更改该数组的值。
相关文章:
- 如果输入数字为负数,如何循环
- C++程序来查找输入数字的最小/最大值
- 如何接受可变的输入数字?
- cin.fail 在输入数字和字母时不起作用
- 如何在函数中输入数字以输出单词?
- 如果用户输入数字 <0,我如何停止" while loop "?
- C++:如果用户输入数字将其转换为单词,并且如果按回车键程序应退出,则使用开关大小写
- 如何使用正则表达式将输入数字格式化为字符串
- 当我运行我的程序时,即使"user"输入数字||C++ 绘画作业
- C 如何进行计数循环,该循环确定两个输入数字中的哪个是最低的,并且从那里计数
- 如何要求用户在类函数中输入数字
- 输入数字输入到if语句中
- 为什么 cin.fail() 在输入数字后跟字符时不返回 false?
- search用字符串列表输入数字(类似于t9字典)
- 试图创建一个函数来查找用户输入数字的斐波那契数
- 如何允许用户输入数字和运算符
- QTreeWidgetItem 可编辑仅允许输入数字
- 如何提示用户输入数字,直到他们选择奇数?C++
- 输入数字,直到输入 -1
- 编写一个程序,输入数字 1-12 并输出相应的月份