Drewdinie Drewdinie - 18 days ago 6
C++ Question

not letting me return boolean value to function

#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;


}


Trying to make a program that when run the user inputs integers and will be able to continue inputting integers until the difference of the numbers input is greater than or equal to 10. For example if i input

1

2

3

4

14

"the difference between is greater than 10"

However boo leans are tricky for me still and the do while. shouldn't checkspread be returning a true or false to the while statement?

Answer

You don't need all of these

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

}

Instead,

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;
}
Comments