0

So I've looked around but I'm apparently either not looking in the right place or I'm misunderstanding something.

The directions given are that the program can only accept values between 2.0 and 5.0 for vault heights. I get the feeling it has to do with my if condition but I'm not sure how to correct it.

When I input the height during debugging, it jumps to my else statement, and doesn't give me a chance to re-enter the new input.

//Retrieving name and first vault and date
cout << "Please enter the name of the pole vaulter.\n";
getline(cin, name);
cout << "Please enter the height of the first vault attempt.\n";
cin >> vault1;
if(5.0 >= vault1 >= 2.0) {
    cout << "What date was this vault accomplished?\n";
    getline(cin, date1);
} else {
    cout << "Only jumps between 2.0 meters and 5.0 meters are valid inputs.       Please enter the height of the first vault attempt.\n";
    cin >> vault1;
    cout << "What date was this vault accomplished?\n";
    getline(cin, date1);
}
0

2 Answers 2

4

The expression

5.0>=vault1>=2.0

should work fine in Python, but in C++ it's parsed as

(5.0>=vault1) >= 2.0

where the result of the first comparison is compared to 2.0, yielding a nonsense result.

In C++ write that instead as

2.0 <= vault1 && vault1 <= 5.0

If you perform this check in more than one place, then it can help on clarity and maintainability define a suitably named boolean function.

Sign up to request clarification or add additional context in comments.

5 Comments

Yeah, the check is being performed on all three vault height inputs.\
I wish I could +2 for that last sentence.
@HeatherT you have a stray '\n' in the buffer.
as rapptz says. one way to get rid of it is to use the ignore method.
Not sure where you mean I have a stray new line. Prof. also checks for readability so I try to start a new line of text after every question.
1

C++ doesn't support mathematical expression.

Change:

if (5.0>=vault1>=2.0)

To:

if (vault1 > 5.0 || valut1 < 2.0)
{
   // invalid vault1
}
else
{
   // valid vault1
}

OR if you want to use &&

if (vault1 <= 5.0 && valut1 > 2.0)
{
   // valid vault1
}

7 Comments

Thanks Billz! I thought it was something like that but I wasn't sure how to fix it.
Shouldn't it be if(vault1 <= 5.0 && vault1 >= 2.0)? Since it's a mathematical expression?
@Rapptz: no, he's checking for invalid. but, he got the inclusive thing wrong...
If he's checking for invalid then you can negate the expression, i.e. if(!(vault1 <= 5.0 && vault1 >= 2.0))?
yepps, that's de morgan's theorem
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.