0

So essentially, I am asking the user for a departure time, as well as if it is in the AM or PM. In the code provided, I only test for AM/am (caps or lowercase), but in my actual program I will be testing for both AM/am and PM/pm. Now, when I set my do/while loop up like this:

do {
    cout << "Please Enter a Valid Period: ";
    cin >> departure_amOrPM;
} while (departure_amOrPM != "AM");

it works just fine, and it lets the program continue when "AM" is entered in all caps. But when I add another test to my loop like this:

do {
    cout << "Please Enter a Valid Period: ";
    cin >> departure_amOrPM;
} while (departure_amOrPM != "AM" || departure_amOrPM != "am");

it does not work. I tried entering both "AM" and "am", yet it did not let me continue. I don't know how to overcome this obstacle, so any help or tips would be much appreciated. Thanks!

2
  • Covert the entire string entered to uppercase. Then the first example will work. And what if aM is entered? Is that valid? Commented Jul 28, 2022 at 20:58
  • Your while loop says: keep looping as long as the string isn't AM or the string isn't am. Only one of those condition needs to be true, that's what || means. Therefore, for the loop to fail both conditions must fail, and the string must be AM, and the string must also be am, at the same time. Until such time as quantum computing becomes reality, this is just not possible, sorry... Commented Jul 28, 2022 at 21:02

4 Answers 4

2

while means to continue looping while the condition is true. You'd like to loop as long as it's neither "AM" _nor "am".

You can write it as:

while (departure_amOrPM != "AM" && departure_amOrPM != "am");
Sign up to request clarification or add additional context in comments.

Comments

1

Think about this condition

} while (departure_amOrPM != "AM" || departure_amOrPM != "am");

This condition is true of every string. Every string is either not equal to "AM" or not equal to "am" (most strings are not equal to both). That's why you couldn't proceed.

What you meant to write is this

} while (departure_amOrPM != "AM" && departure_amOrPM != "am");

It's very common to get && and || confused, especially when also dealing with negation.

1 Comment

ah, I see where my logic went wrong. I can't believe that went over my head like that. It makes sense now that you've explained it though, thanks!
0

Instead of using the logical OR operator in the condition or the do-while loop

do {
    cout << "Please Enter a Valid Period: ";
    cin >> departure_amOrPM;
} while (departure_amOrPM != "AM" || departure_amOrPM != "am");

Use the logical AND operator like

do {
    cout << "Please Enter a Valid Period: ";
    cin >> departure_amOrPM;
} while (departure_amOrPM != "AM" && departure_amOrPM != "am");

To test yourself at first write a condition to which the valid input satisfies. For your code it will look like

(departure_amOrPM == "AM" || departure_amOrPM == "am")

Now make its negation like

!(departure_amOrPM == "AM" || departure_amOrPM == "am")

or

not(departure_amOrPM == "AM" || departure_amOrPM == "am")

As for example

do {
    cout << "Please Enter a Valid Period: ";
    cin >> departure_amOrPM;
} while ( not (departure_amOrPM == "AM" || departure_amOrPM == "am") );

Now you can rewrite it using the mathematical logic like

do {
    cout << "Please Enter a Valid Period: ";
    cin >> departure_amOrPM;
} while (departure_amOrPM != "AM" && departure_amOrPM != "am");

Comments

0

SET COLOR TO W/N.N/W

mcursoff = ",N/N"

mreverse = " I "

mstablink = "w*/N "

mbrigt = "W+ /N "

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

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.