0

Ok, so I was writing a simple interface for a programming I'm creating and i come across this issue, where it gives me the same response regardless.

  #include <iostream>
  using namespace std;

  int main()
 {
  char v;

  cout << "Binary or ASCII? "<<endl;

  cin >> v;

  if (v == 'B' || 'b')
  {
    cout << "Binary " << endl;

  }
  else if (v == 'A' || 'a')
  {
    cout << "ASCII " << endl;

  }
  else
  {
    cout << "ERROR: Invalid Option" << endl;

  }


   return 0;

  }

The interface is supposed to output

 Binary

if I type B or b

  ASCII

if i type A or a

and

  ERROR: Invalid Option

for everything else

Instead, I get

  Binary 

regardless of what I type

Where is my mistake? what am I doing wrong?

  • 1
    Terminology quibble: `if`s and `else`s don't loop. – user4581301 Oct 12 '18 at 20:34
  • 2
    Change `if (v == 'B' || 'b')` to `if (v == 'B' || v == 'b')` – GBlodgett Oct 12 '18 at 20:34
  • 3
    Well assembled question, by the way. It contains all the stuff I want to see in one. – user4581301 Oct 12 '18 at 20:34
  • 1
    Alternative to two tests: convert `v` to [lower](https://en.cppreference.com/w/cpp/string/byte/tolower) (or [upper](https://en.cppreference.com/w/cpp/string/byte/toupper)) case so that you only have to test one case. Eg: `cin >> v; v = std::tolower(v); if (v == 'b')`. This is particularly helpful if you have many options you need to test. – user4581301 Oct 12 '18 at 20:38
  • _@Lisandro_ `if() else` control structures aren't _loops_ BTW. I am struggling over 20 years where you guys got that wrong from. Loops are control structures which apply for recurring code blocks, `if() else if() else` will select code blocks **once** straightforward. – πάντα ῥεῖ Oct 12 '18 at 21:26

1 Answers1

1

Let's take a look at what happens in your if:

if (v == 'B' || 'b')

First it checks if v == 'B'. Let's assume it doesn't for the sake of this walkthrough. Then it'll check (false || 'b'). Since 'b' always evaluates to true, this will be true!

You probably wanted:

if (v == 'B' || v == 'b')
scohe001
  • 15,110
  • 2
  • 31
  • 51
  • 4
    Answering the umpteenth duplicate of this question recurring about twice a week doesn't really look useful for me. – πάντα ῥεῖ Oct 12 '18 at 20:38
  • 1
    @πάνταῥεῖ but does it cause harm? The way I see it, worst case I only help *this* OP *this* one time. But at least I've helped someone. – scohe001 Oct 12 '18 at 20:44
  • That would clutter the site with more or less noise. You could have helped better proposing the duplicate and leave a comment for OP's particular case. – πάντα ῥεῖ Oct 12 '18 at 20:45
  • @πάνταῥεῖ yes, but then you can't rep-farm effectively. I don't know how many times I've missed out on all that phat loot by taking the time to look for a duplicate and either finding one or losing out to some fast-typing son of a gun because I stopped to look. scohe001, the harm is now that the question has an accepted, upvoted answer, it is now much harder to delete and probably will remain clutter for quite some time to come. Plus this put me further from my goal of being the next Jon Skeet because I had to burn rep downvoting it. – user4581301 Oct 12 '18 at 21:11
  • 1
    @user4581301 _"but then you can't rep-farm effectively"_ since when was rep-farming ever a concern to usefully contribute to this site? And of course that could be considered harmful if it's getting into the way of roomba, and needs manual interaction to delete the question and answer altogether. – πάντα ῥεῖ Oct 12 '18 at 21:17
  • @πάνταῥεῖ I think they may be trying for sarcasm to insinuate that I'm a "rep-farmer" looking for "phat loot," but it's not readily apparent to me. Regardless, they do make a good point that this question may have been able to be deleted if not for my answer--that's not something I'd considered. I'll try to avoid answers like this in the future. Thanks guys. – scohe001 Oct 12 '18 at 21:20
  • @scohe001 I well understand the _sarcasm_. – πάντα ῥεῖ Oct 12 '18 at 21:21
  • Scohe, I apologize for the overly mocking tone. I should have stuck to the main point, the blocking of automated clean-up. – user4581301 Oct 12 '18 at 21:23
  • @user4581301 you're good, it was a fun read. This place wouldn't be what it is without the classic SE snark :) – scohe001 Oct 12 '18 at 21:27