Click here to Skip to main content
15,885,921 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How can I output the text when the difference between input1 and input2 is less than 0.1.
Can it includes both negative double and positive double?

C++
#include <iostream>
using namespace std;

int main()
{
double input1=0.00;
    double input2=0.00;
    bool input=true;

    cout<<"Please enter first number"<<std::endl;
    cin>>input1;
    cout<<"Please enter second number"<<std::endl;
    cin>>input2;
    double input_diff=input1-input2;
    cout<<"input difference is "<<input_diff<<endl;


I tried to put this after the code above, but it produces error.

C++
    if (input_diff > -2.0 & < 2.0){      // What is wrong here?
        x=true;
        cout<<"The value between the two is almost equal"<<endl;

    }
}
Posted

1 solution

Your "& <" is gibberish. Just think about it: is there such operator?
The condition could be (input_diff > -2.0) & (input_diff < 2.0); and if requires any expression on round brackets.

Your questions about 0.1 and negative is just a mess; you write 0.1 and then 2.0, and so on. Please sort it out yourself. Mathematics for elementary school is included in prerequisites for computing, a really smallest part of what you need to know.

—SA
 
Share this answer
 
Comments
Are Riff 26-Aug-15 5:52am    
Your condition works.

if ((input_diff > -2.0) & (input_diff < 2.0)) {
cout << "The value between the two is almost equal" << endl;
}

I never thought of that. Clearly I need more practice.
The reason for 0.1 and 2 is I changed it to see whether double is the reason it fail (and forgot to change it back).

I was reading the if statement like this: if input_diff is larger than -2 and smaller than 2, then cout the string (ignore the x=true;, it was from previous experimenting to make it work).

As for the '&', I copied it from another place, it should be a '&&'. But that didn't work too.

Thank You, I learned something here.
Sergey Alexandrovich Kryukov 26-Aug-15 9:20am    
You are welcome.
Good luck, call again.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900