0
votes

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

}
2
Welcome to Stack Overflow! Please edit your question with an minimal reproducible example or SSCCE (Short, Self Contained, Correct Example). As your code is right now it won't even compile. - NathanOliver
else (val1 - val2 <= 0.01 && val1 != val2 || val2 - val1 <= 0.01 && val1 != val2) is not valid syntax. - NathanOliver
I'm not sure how to correctly write that, though. I'm a complete beginner to programming and this is my 2nd week. Would you have any tips on how to write it properly? - JXX
Write what? You said the code works but it gives you a bad answer. I am saying the code you posted doesn't even compile so we can't tell you why it doesn't work. Do you actually have a working version of this code and is it the same as what you posted here? - NathanOliver
Sorry for the vagueness - it compiles for me, and it is the exact same as I have it up there. I'm on VS2015 Community, if that matters at all. As for writing what, the incorrect syntax on the statement you quoted. - JXX

2 Answers

0
votes

The problem is in the order of your "if-else" conditions: it is important because as soon as one such condition is fulfilled, the other ones will no longer be even checked.

0
votes

This...

else (val1 - val2 <= 0.01 && val1 != val2 || val2 - val1 <= 0.01 && val1 != val2) {
  • should have the else replaced with if, so that - regardless of the earlier output, it tests this specific condition and may print the "almost equals" message

  • needs the condition fixed: your logic would work if you compared the absolute value of val1 - val2 to the 0.01 threshold (but as it is now, it's always true); hint

To have the program only terminate when '|' is entered is a little tricky - you could wrap your currrent while loop in something like this:

do
{
    while (cin >> val1 >> val2) etc. as you have now...

} while (!cin.eof() && cin.clear() && cin.peek() != '|' && cin.ignore(1);

What that does is exit if you're at the end of the input, otherwise clear the stream error state, check the next character isn't a '|', and if it's not ignore whatever garbage character broke the numeric parsing and loop back to try again at the following character.

Note though that if there was one good number then a garbage character, the good number will be thrown away and it will start looking for a sequence of two good numbers after any garbage characters are skipped.