rectangletangle
rectangletangle

Reputation: 52911

Why isn't my or statement working, C++?

I'm trying to create a function that will replace 0's, 1's, or 2's with spaces in a string. I'm going about it by iterating through the string and comparing each individual character.

My function will work if I compare str_in[i] == '0', but if I add the or statement it returns nothing.

Snippet:

string omit_num( string ) {
    int i ;

    str_len = str_in.length();
    str_out = "" ; 

    for( i = 0 ; i < str_len ; i ++ ){
         cout << str_in[i] << endl; 
         if ( str_in[i] == '0' || '1' || '2') 
            app = " " ;
         else
             app = str_in[i];
         str_out.append(app) ; 
    }
    return str_out; 

}

Upvotes: 3

Views: 367

Answers (11)

Inverse
Inverse

Reputation: 4476

If you're using c++, use the standard library already:

std::replace_if( s.begin(), s.end(), [](char a){return a=='0'||a=='1'||a=='2';}, ' ');

Upvotes: 0

Harmon Wood
Harmon Wood

Reputation: 2987

'1' is '1' therefore '1' evaluates to true. You must make new, complete, statements with every operator.

try this:

if ( str_in[i] == '0' || str_in[i] == '1' || str_in[i] == '2' )

Upvotes: 3

Nick Dandoulakis
Nick Dandoulakis

Reputation: 43110

if (str_in[i] == '0' || str_in[i] == '1' || str_in[i] == '2')

or alternatively

switch (str_in[i]) {
  case '0':
  case '1':
  case '2': app = " " ;
            break;
  default:  app = str_in[i];
}

Upvotes: 4

Kizaru
Kizaru

Reputation: 2493

The expression str_in[i] == '0' || '1' || '2' contains three separate expressions:

str_in[i] == '0'
'1'
'2'

According to any ASCII chart, '0' has a value of 48, '1' is 49, '2' is 50. So the last two expressions are always non-zero (and therefore always true).

You probably wanted str_in[i] == '0' || str_in[i] == '1' || str_in[i] == '2'

Upvotes: 4

Paul
Paul

Reputation: 2238

Your if statement reads as follows: If the character at i is equal to '0', or '1' is true, or '2' is true. As '1' and '2' both evaluate to a non zero integer, it will always be true.

What you want is: str_in[i] == '0' || str_in[i] == '1' || str_in[i] == '2'

Upvotes: 3

Andrew
Andrew

Reputation: 1650

You need to apply the equality operator in each || expression:

if (str_in[i] == '0' || str_in[i] == '1' || str_in[i] == '2')

Upvotes: 1

g19fanatic
g19fanatic

Reputation: 10921

string omit_num( string ) { 
int i ; 

str_len = str_in.length(); 
str_out = "" ;  

for( i = 0 ; i < str_len ; i ++ ){ 
     cout << str_in[i] << endl;  
     if ( str_in[i] == '0' || '1' || '2')  
        app = " " ; 
     else 
         app = str_in[i]; 
     str_out.append(app) ;  
} 
return str_out;  

}

this would work as the following:

string omit_num( string ) { 
int i ; 

str_len = str_in.length(); 
str_out = "" ;  

for( i = 0 ; i < str_len ; i ++ ){ 
     cout << str_in[i] << endl;  
     if ( (str_in[i] == '0') || (str_in[i] == '1') || (str_in[i] == '2'))  
        app = " " ; 
     else 
         app = str_in[i]; 
     str_out.append(app) ;  
} 
return str_out;  

}

Upvotes: 1

Ondrej Slint&#225;k
Ondrej Slint&#225;k

Reputation: 31910

Change your if statement to:

if ( str_in[i] == '0' || str_in[i] == '1' || str_in[i] == '2') {
...
}

Upvotes: 1

Matt Davis
Matt Davis

Reputation: 46034

Not sure about the logic overall, but the logic here specifically is wrong. Replace

if ( str_in[i] == '0' || '1' || '2') 

with

if ( str_in[i] == '0' || str_in[i] == '1' || str_in[i] == '2') 

Upvotes: 1

ravenspoint
ravenspoint

Reputation: 20457

You need

if ( str_in[i] == '0' ||  str_in[i] =='1' ||  str_in[i] =='2') 

Upvotes: 11

Bruce Armstrong
Bruce Armstrong

Reputation: 1602

You will have to repeat the test each time, '1', '2' on their own are basically small ints and evaluate to true. It should look like the following:

if (str_in[i] == '0' || str_in[i] == '1' || str_in[i] == '2')

Upvotes: 6

Related Questions