operator || problem need help!

Sep 6, 2019 at 6:31pm
I having trouble to figure || operator work but when I expected it to print 0 but it print 2. I thought I tell it to "if name is 0 and B is 1 it should not print or name is 0 and b = 5 it should not print" can anyone tell me where I go wrong?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <stdio.h>
using namespace std;

int
main ()
{
  char name[15] = "Budi Sutiono";
  int count = 0;
  for (int i = 0; i < 12; i++)
    {
      if (name[i] == 'B' || name[i] == 'S')
	{
	  count++;
	}
    }

  printf ("jumlah B : %d", count);

  return 0;
}
Sep 6, 2019 at 6:59pm
Why would you expect count to equal 0? Your string has both a 'B' and a 'S' so it should print 2.

Remember you're saying if name[i] is equal to 'B' OR name[i] is equal to 'S' increment count.

Sep 6, 2019 at 7:10pm
<cstdio> //stdio.h is a C header and it works but is not ideal to use in c++
'B' is not 1.
'B' - 'A' is 1 in ascii.

B is 1 it should not print or name is 0 and b = 5 makes no sense to me.
are you confusing 5 and S visually? Also case matters, b and B are different whether in text data or variable names (unclear).

char name[15] can be said as
char name[] = "whatever"
you don't need to specify the length when you initialize it this way.

12 ... strlen(name) for C-string length.
Last edited on Sep 6, 2019 at 7:12pm
Sep 6, 2019 at 8:09pm
Based on @jonnin's comment it seems the original post was edited, as I see nothing that would evaluate B as 1 or lower case 'b' being involved, or the number 5 relating to 'S'.


To that end, I see this question

but when I expected it to print 0 but it print 2


With a general inquiry as the meaning or purpose of the "||" operator, as in this code as posted at this time

1
2
3
4
     if (name[i] == 'B' || name[i] == 'S')
	{
	  count++;
	}


Here, the code is saying that if the character in name at position "i" is either a "B" or an "S", increment the count.

Since the name includes a B and an S, it should count to 2, as @jib points out.



Last edited on Sep 6, 2019 at 8:09pm
Sep 6, 2019 at 8:26pm
he has not changed it.
I was trying to make sense of his B=1 or b = 5 and name = 0 words.
Last edited on Sep 6, 2019 at 8:26pm
Sep 6, 2019 at 9:51pm
Ah, and that is something I can't make sense of either...
Sep 7, 2019 at 1:05am
1. I not mistaking for any 5 =S

2.the reason I say about B =1 is because the position about "Budi" is first and it also apply with S after it finish counting Budi(not include space) and add up with S it will be 5.

But now I understand where I go wrong now because the char[15] is like factorial now it make sense to me why it will be print both of the B and S
Sep 7, 2019 at 1:12am
Just to be clear, "B" is not at 1.

It is at 0.

The "S" is at position 5.

Your code isn't counting by position, per se (it's counting the occurrences of B's and S's)...it's sequencing through the positions in the array, in which the space is considered at position 4.

Last edited on Sep 7, 2019 at 1:13am
Sep 7, 2019 at 1:14am
@niccolo does space count too ?
That mean at the start of counting is it always start with 0 ?
Last edited on Sep 7, 2019 at 1:15am
Sep 7, 2019 at 1:47am
Yes, every position is a character, and space is character.

Where "A" is 65 (decimal), and "B" is 66 (decimal), the space is 32 (decimal)...and it occupies a position like any other character.

As you'll discover in your near future, the "tab" character (which is 9 decimal) is another "non-visible" character which occupies one position.

As does the line feed (decimal 10) and carriage return (decimal 13) and many other "control" characters which don't display any visible character.

Some of these (the space and tab, and others) are often called "whitespace" characters.
Last edited on Sep 7, 2019 at 1:51am
Topic archived. No new replies allowed.