I'm stuck on Stroustrup's P:PP Chapter 4 Drill, part 5. The problem asks me to write a program that consists of a while-loop that reads 2 doubles and terminates the program when the character | is entered. The program should output both the doubles, tell you which is smaller and larger, and whether or not they're equal or almost equal. I took the definition for almost equal to be when the numbers are within 0.1 of each other. I also wrote the program so that it terminates when any non-double input is entered, instead of just |.
When I input 15.0 and 19.0, I'm still told that "The values are almost equal," instead of that 15.0 is smaller and 19.0 is larger. Curiously, if I input 19.0 and 15.0, it will tell me that the smaller value is 15, and the larger one is 19. If I input 15.0 and 15.0, it does tell me the values are now equal.
Basically, if x < y, and I input in the order "x y" I get "almost equal," but if I input "y x" I'm told that x is smaller and y is larger. Otherwise, error-free.
Any suggestions?
#include "../../../std_lib_facilities.h"
int main()
{
double val1 = 0;
double val2 = 0;
cout << "Enter 2 values: \n";
while (cin >> val1 >> val2) {
if (val1 < val2) {
cout << "The smaller value is " << val1 << " and the bigger value is " << val2 << '\n';
}
else if (val1 == val2) {
cout << "Both values are equal.\n";
}
else if (val1 > val2) {
cout << "The smaller value is " << val2 << " and the bigger value is " << val1 << '\n';
}
else (val1 - val2 <= 0.01 && val1 != val2 || val2 - val1 <= 0.01 && val1 != val2) {
cout << "The values are almost equal.\n";
}
}
}
else (val1 - val2 <= 0.01 && val1 != val2 || val2 - val1 <= 0.01 && val1 != val2)is not valid syntax. - NathanOliver