为什么这个while循环显示第一个元素?

时间:2017-04-21 09:13:45

标签: c++ while-loop converter temperature

我是C ++的初学者,想做this练习。以下是我的代码,但我无法弄清楚为什么在cout while循环时它没有显示下限。

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
    double low=-0.000001, high=-0.000001, step=0.0, stop, f;
    while (low <= -0.000001 || low > 50000) {
        cout << "Lower limit (0~50,000): "; cin >> low;
        if (low <= -0.000001) {cout << "Must be positive." << endl;}
        else if (low > 50000) {cout << "Out of range." << endl;}
    }
    while (high <= -0.000001 || high > 50000 || high <= low) {
        cout << "Higher limit (0~50,000): "; cin >> high;
        if (high <= -0.000001) {cout << "Must be positive." << endl;}
        else if (high > 50000) {cout << "Out of range." << endl;}
        else if (high <= low) {cout << "Must higher than lower limit." << endl;}
    }
    while (step <= 0 || step > high) {
        cout << "Step (0.000001~" << high << "): "; cin >> step;
        if (step <= 0) {cout << "Must be positive." << endl;}
        else if (step > high) {cout << "Out of range." << endl;}
    }
    cout << endl << "Celsius\t\tFahrenheit" << endl;
    cout << "-------\t\t----------" << endl;
    stop = low;
    while (stop < high) {
        f = 1.8 * stop + 32.0;
        stop += step;
        cout << fixed << setprecision(6) << stop << "\t" << fixed << setprecision(6) << f << endl;
    }
    return 0;
}

例如,如果我输入1.5作为下限,我想它的输出应该从1.5开始,而是从2.0开始...我该如何解决这个问题?感谢。

Img of Not the outcome I want

1 个答案:

答案 0 :(得分:1)

第一次增加

stop

将打印移到上面,它应该可以正常工作。

旁注:使用调试器很容易找到这样的问题,并且重复过来&#34;命令(可能有一些手表显示你的有趣值如何随时间变化)。程序执行后,一步一步可以启动。