不要让我返回布尔值来起作用

时间:2016-11-15 06:11:25

标签: c++

#include <iostream>
#include <cstdlib>
using namespace std;
bool CheckSpread();

int main()
{
    int x;
    int y;
    bool ok;
    do 
    {

        cout << "Please enter some integers" << endl;
        cin >> x >> y;
        CheckSpread(x, y);
        ok == CheckSpread;
    } 
    while (CheckSpread() == true); 
    {
    cout << "The difference between the two numbers is equal to or greater than 10" << endl;

    }


    return 0;
}
int CheckSpread(int a, int b)
{

    int diff = std::abs(a - b);
    if (diff >= 10)
    {
        return true;
    }
    else
        return false;


}

尝试制作一个程序,当运行用户输入整数时,将能够继续输入整数,直到数字输入的差异大于或等于10.例如,如果我输入

1

2

3

4

14

&#34;之间的差异大于10&#34;

然而,对于我来说,boo leans仍然很棘手。不应该检查是否会在while语句中返回true或false?

1 个答案:

答案 0 :(得分:-2)

你不需要所有这些

    CheckSpread(x, y);
    ok == CheckSpread;
} 
while (CheckSpread() == true); 
{
cout << "The difference between the two numbers is equal to or greater than 10" << endl;

}

相反,

do 
{
    cout << "Please enter some integers" << endl;
    cin >> x >> y;

    if (!CheckSpread(x, y))
        break;
} 
while (1); 
{
    cout << "The difference between the two numbers is equal to or greater than 10" << endl;
}