开发者

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.

开发者_JAVA百科

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; 

}


You need

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


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')


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'


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];
}


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'


'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' )


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') 


Change your if statement to:

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


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;  

}


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

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


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';}, ' ');
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜