2014-10-10 85 views
-1
int main(){ 

    int x; 
    cout<<"enter a number: "; 
    cin>>x; 
    cout<<endl; 
    odd(x); 


    return 0; 
} 

void odd(int a){ 

if(a%2 != 0){ 

    cout<<"the number is odd : "<< '(' +a+ ')'; 

    }else{ 

    even(a); 

    } 
} 

我执行上述计划,我得到不同的输出:为什么我在C++中获得不同的输出?

enter a number: 15 

the number is odd : 96 

这究竟是为什么?

感谢

+1

C++没有字符串连接,它*绝对*不像其他语言那样具有数字到字符串的转换。 @ MikolajMularczyk的回答是正确的。 – Qix 2014-10-10 19:55:44

回答

12

试试这个:cout<<"the number is odd : "<< '('<< a << ')';

“(”和“)”的ASCII值有40和41它们被晋升为int和你添加它们,这就是为什么你的输出是96( 40 + 15 + 41 == 96)。

+0

这清除了我的怀疑。谢谢@MikolajMularczyk。 – hamid 2014-10-10 20:16:39

相关问题