Jump to content

Operators in if statement with char array

Lolcrokn

In this task the counter should give me the number of vowels in the word apple.


 

#include <iostream>
#include <string>
using namespace std;

void main()
{
    char word[5]= { 'A','P','P','L','E'};
    int i;
    int counter = 0;
    for (i = 0; i<5; i++)
    {
        if (word == 'A' || 'E' || 'I' ||  'O' ||  'U')
        {
            cout << word << endl;
            counter++;
        }
    }
    cout << "counter:" << counter << endl;
    getchar();

}

When I run it this way it outputs like this:

A
P
P
L
E
counter:5


But when I run it like this:

 


#include <iostream>
#include <string>
using namespace std;

void main()
{
    char word[5]= { 'A','P','P','L','E'};
    int i;
    int counter = 0;
    for (i = 0; i<5; i++)
    {
        if (word == 'A' || word == 'E' || word == 'I' || word == 'O' || word == 'U')
        {
            cout << word << endl;
            counter++;
        }
    }
    cout << "counter:" << counter << endl;
    getchar();

} 

 

 

 

 

 

This is what it outputs:

A
E
counter:2

 

 

Why is the first method wrong?

GPU: GTX 1060 6GB

RAM: 16GB HyperX RAM

CPU: R5 3600 

Motherboard: ASRock B450 Fatal1ty gaming K4

OS: Win 11 Pro 64-bit

Link to comment
Share on other sites

Link to post
Share on other sites

Because you have to specifically specify what your OR comparison is checking (as you are in Example 2). This

if (word == 'A' || 'E' || 'I' ||  'O' ||  'U')

Is not short hand for 

  if (word == 'A' || word == 'E' || word == 'I' || word == 'O' || word == 'U')

 

Link to comment
Share on other sites

Link to post
Share on other sites

If you compare a value directly in an if-statement and the compiler can map a value directly to a boolean, then typically if the value is < 1, it's treated as false, otherwise it's true.

 

Given the first example's if-statement:

if (word == 'A' || 'E' || 'I' ||  'O' ||  'U')

After word == 'A', the rest of the checks resolve to true because they're 1 or greater, so the entire if-statement always passes.

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, Mira Yurizaki said:

If you compare a value directly in an if-statement and the compiler can map a value directly to a boolean, then typically if the value is < 1, it's treated as false, otherwise it's true.

 

Given the first example's if-statement:


if (word == 'A' || 'E' || 'I' ||  'O' ||  'U')

After word == 'A', the rest of the checks resolve to true because they're 1 or greater, so the entire if-statement always passes.

It didn't copy paste properly for some reason It was supposed to look like this:

 if (word == 'A' || 'E' || 'I' ||  'O' ||  'U')

if (word == 'A' || word == 'E' || word == 'I' || word == 'O' || word == 'U')

GPU: GTX 1060 6GB

RAM: 16GB HyperX RAM

CPU: R5 3600 

Motherboard: ASRock B450 Fatal1ty gaming K4

OS: Win 11 Pro 64-bit

Link to comment
Share on other sites

Link to post
Share on other sites

6 minutes ago, C2dan88 said:

Because you have to specifically specify what your OR comparison is checking (as you are in Example 2). This


if (word == 'A' || 'E' || 'I' ||  'O' ||  'U')

Is not short hand for 


  if (word == 'A' || word == 'E' || word == 'I' || word == 'O' || word == 'U')

 

Yea I guess I'm wondering why those 2 lines don't do the same thing

GPU: GTX 1060 6GB

RAM: 16GB HyperX RAM

CPU: R5 3600 

Motherboard: ASRock B450 Fatal1ty gaming K4

OS: Win 11 Pro 64-bit

Link to comment
Share on other sites

Link to post
Share on other sites

5 minutes ago, Lolcrokn said:

It didn't copy paste properly for some reason It was supposed to look like this:

 if (word == 'A' || 'E' || 'I' ||  'O' ||  'U')

if (word == 'A' || word == 'E' || word == 'I' || word == 'O' || word == 'U')

So is the first example supposed to be

 if (word == 'A' || 'E' || 'I' ||  'O' ||  'U')

Or

if (word == 'A' || word == 'E' || word == 'I' || word == 'O' || word == 'U') 

 

Link to comment
Share on other sites

Link to post
Share on other sites

6 minutes ago, Lolcrokn said:

Yea I guess I'm wondering why those 2 lines don't do the same thing

Because the compiler resolves the first example's if-statement to

if (word == 65 || 69 || 73 || 79 || 85)

Which I said before, a conditional check on a straight-up value is basically the same as:

if (value > 0) {
	return true;
}
else {
	return false;
}

 

Edited by Mira Yurizaki
Flipped the if-statement on the value (beacuse blah)
Link to comment
Share on other sites

Link to post
Share on other sites

6 minutes ago, Mira Yurizaki said:

So is the first example supposed to be


 if (word == 'A' || 'E' || 'I' ||  'O' ||  'U')

Or


if (word == 'A' || word == 'E' || word == 'I' || word == 'O' || word == 'U') 

 

First example:

56b56a4827a8e472c4a4757c8295fc71.png

Second example:

27fbcdacd7fba9ae5b98ede3da2499b1.png

For some reason it isn't registering brackets.

GPU: GTX 1060 6GB

RAM: 16GB HyperX RAM

CPU: R5 3600 

Motherboard: ASRock B450 Fatal1ty gaming K4

OS: Win 11 Pro 64-bit

Link to comment
Share on other sites

Link to post
Share on other sites

This seems like a fundamental misunderstanding of how the "if" statement or, more generally, comparisons work in c-like languages.

 

When you use a logical operator like "&&" or "||" the compiler will evaluate the statements on its left and on its right to a boolean (0 or 1) and then apply the operator. Characters are just 8-bit numbers in c and c++ and any number that is larger than 0 is usually evaluated as "true" for logical operators if it's the only part of the statement.

 

The "==" operator returns "true" (1) when the statements on both sides have the same value - so in your case, 'P' == 'A' would evaluate to "false" (0) but 'E' on its own would evaluate to true, so in effect you'd have:

if(word[i] == 'A' || 'E')

which evaluates to

if(0 || 69)

which evaluates to

if(0 || 1)

which evaluates to

if (1)

which is true.

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×