获取随机科学数字而不是输入

Getting Random Scientific numbers instead of inputs

本文关键字:输入 数字 随机 获取      更新时间:2023-10-16

与标题状态一样,当我将它们存储在数组中并尝试输出时,我会得到随机的科学数字。我不知道为什么它们会改变,因为没有对数字进行运算。由于数字的变化,我得到了不正确的华氏温度。

样本输入: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;

因为分配是从右边到左边。

垃圾值的原因是温度没有初始化,其中有垃圾值。在整个过程中,由于上面的错误,您没有更改该数组的值。