Do-While trouble comparing multiple characters via logical OR.

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 358 Views - Last Post: 05 February 2018 - 02:42 PM Rate Topic: ***-- 2 Votes

#1 CVRIV  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 03-February 18

Do-While trouble comparing multiple characters via logical OR.

Posted 05 February 2018 - 05:20 AM

char P2_UnitOfTime;

do{
   std::cout << "\nType \"H\" to enter an amount in Hours or \"M\" for minutes:" << endl;
   std::cin >> P2_UnitOfTime;
}while(P2_UnitOfTime != 'h' || P2_UnitOfTime != 'h' || P2_UnitOfTime != 'M' || P2_UnitOfTime != 'm');



This do-while fails to exit when I input any of the above mentioned characters. If I comment out all but the first condition, it works just fine, but as soon as I add the logical OR's, nothing works. Any ideas?

I tried encasing my expressions with parentheses and that didn't work. I'm learning from a book and it has an example laid out pretty much like this, except the example is working with integers.

Thanks in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: Do-While trouble comparing multiple characters via logical OR.

#2 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 1437
  • View blog
  • Posts: 4,626
  • Joined: 20-March 10

Re: Do-While trouble comparing multiple characters via logical OR.

Posted 05 February 2018 - 05:57 AM

Why do you think an OR is what you want ?


if the input is 'h' then 'h' != 'm' so it loops same goes for M H

computers only do what you tell them to do..
Was This Post Helpful? 1
  • +
  • -

#3 CVRIV  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 03-February 18

Re: Do-While trouble comparing multiple characters via logical OR.

Posted 05 February 2018 - 05:58 AM

int main(){
	char P2_UnitOfTime;

	std::cout << "\nType \"H\" to enter an amount in Hours or \"M\" for minutes:" << endl;
	std::cin >> P2_UnitOfTime;

	if(P2_UnitOfTime == 'H' ||  P2_UnitOfTime == 'h' || P2_UnitOfTime == 'M' || P2_UnitOfTime == 'm'){
		std::cout << "MATCH!" << endl;
	}else{
		std::cout << "NOPE:(" << endl;
	}
	return 0;
}



I've been doing some testing and found that the above code works, but.......

int main(){
	char P2_UnitOfTime;

	std::cout << "\nType \"H\" to enter an amount in Hours or \"M\" for minutes:" << endl;
	std::cin >> P2_UnitOfTime;

	if(P2_UnitOfTime != 'H' ||  P2_UnitOfTime != 'h' || P2_UnitOfTime != 'M' || P2_UnitOfTime != 'm'){
		std::cout << "NOPE:(" << endl;
	}else{
		std::cout << "MATCH!" << endl;
	}
	return 0;
}



... this code doesn't work. I get NOPE for everything I enter.
Was This Post Helpful? 0
  • +
  • -

#4 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 1437
  • View blog
  • Posts: 4,626
  • Joined: 20-March 10

Re: Do-While trouble comparing multiple characters via logical OR.

Posted 05 February 2018 - 06:02 AM

Err yeah...

That would certainly be the case...

review what OR does...
Was This Post Helpful? 0
  • +
  • -

#5 CVRIV  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 03-February 18

Re: Do-While trouble comparing multiple characters via logical OR.

Posted 05 February 2018 - 06:05 AM

I want the loop to halt when the input equals 'h' or 'H' or 'm' or 'M'.
Was This Post Helpful? 0
  • +
  • -

#6 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 1437
  • View blog
  • Posts: 4,626
  • Joined: 20-March 10

Re: Do-While trouble comparing multiple characters via logical OR.

Posted 05 February 2018 - 06:08 AM

Yes I get that...

Look why dont you try a Logical AND instead && ??

Its called inverse logic.. you should read about it.. it will help you in the future with multiple tasks.
Was This Post Helpful? 0
  • +
  • -

#7 ndc85430  Icon User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 721
  • View blog
  • Posts: 2,918
  • Joined: 13-June 14

Re: Do-While trouble comparing multiple characters via logical OR.

Posted 05 February 2018 - 06:09 AM

That's the same as saying, "keep going while it isn't any of them", which is what snoopy11 is getting at.

This post has been edited by ndc85430: 05 February 2018 - 06:09 AM

Was This Post Helpful? 0
  • +
  • -

#8 CVRIV  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 03-February 18

Re: Do-While trouble comparing multiple characters via logical OR.

Posted 05 February 2018 - 06:09 AM

Wow. Thanks. I hope everyone here isn't as friendly as you are.

000
011
101
111


Was This Post Helpful? 0
  • +
  • -

#9 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 1437
  • View blog
  • Posts: 4,626
  • Joined: 20-March 10

Re: Do-While trouble comparing multiple characters via logical OR.

Posted 05 February 2018 - 06:15 AM

Ok then.... I think...

I'm actually quite friendly and a very handy person to know........ ;)
Was This Post Helpful? 0
  • +
  • -

#10 CVRIV  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 03-February 18

Re: Do-While trouble comparing multiple characters via logical OR.

Posted 05 February 2018 - 06:17 AM

I was under the impression that using || meant that if ANY expression returned true and that using && meant that if ALL returned true.

Why is it the other way around?
Was This Post Helpful? 0
  • +
  • -

#11 andrewsw  Icon User is offline

  • blow up my boots
  • member icon

Reputation: 6505
  • View blog
  • Posts: 26,306
  • Joined: 12-December 12

Re: Do-While trouble comparing multiple characters via logical OR.

Posted 05 February 2018 - 06:19 AM

If I give you something that is either not a banana OR not an apple, I can give you a banana... because it is not an apple.
Was This Post Helpful? 1
  • +
  • -

#12 CVRIV  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 03-February 18

Re: Do-While trouble comparing multiple characters via logical OR.

Posted 05 February 2018 - 06:32 AM

View Postandrewsw, on 05 February 2018 - 06:19 AM, said:

If I give you something that is either not a banana OR not an apple, I can give you a banana... because it is not an apple.


OMG. Why am I having so much trouble understanding this.

Based on what you said, I'd be expecting you to give me an orange, because an orange is neither a banana or an apple.

Could you give me an apple because it's not a banana? I'm seriously trying to wrap my head around this.
Was This Post Helpful? 0
  • +
  • -

#13 ndc85430  Icon User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 721
  • View blog
  • Posts: 2,918
  • Joined: 13-June 14

Re: Do-While trouble comparing multiple characters via logical OR.

Posted 05 February 2018 - 06:36 AM

Yes. Let P be "not a banana" and Q be "not an apple" and consider the expression P || Q. If we have an apple, then P is true and Q is false, so the whole expression is true.
Was This Post Helpful? 1
  • +
  • -

#14 andrewsw  Icon User is offline

  • blow up my boots
  • member icon

Reputation: 6505
  • View blog
  • Posts: 26,306
  • Joined: 12-December 12

Re: Do-While trouble comparing multiple characters via logical OR.

Posted 05 February 2018 - 06:42 AM

Yes, I could give you an apple because it is not a banana.

You want something that is not a banana AND not an apple. Such as an orange.
Was This Post Helpful? 0
  • +
  • -

#15 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 1437
  • View blog
  • Posts: 4,626
  • Joined: 20-March 10

Re: Do-While trouble comparing multiple characters via logical OR.

Posted 05 February 2018 - 06:45 AM

It all comes down to inverse logic as I said before...

https://en.wikipedia...Inverse_(logic)


from the above...

Quote

For example, "If it's not raining, then Sam will not meet Jack at the movies" cannot be inferred from "If it's raining, then Sam will meet Jack at the movies." It could easily be the case that Sam and Jack are attending the movies no matter the weather.

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2