C++帮我看看为什么abs加了就出错

img

img


为什么加abs就出错 不加abs就可以运行程序
else if( a[i] != b[i] && abs(a[i]-b[i])==32 )
{
flag = 3;
}

你可以在abs前边加std::
abs是C++的cmath库中提供的用于求绝对值的函数,调用时需要确定命名空间,通常在多次用到同一个命名空间时可以在包含头文件后加using namespace std;当用的次数不多时也可以直接在所调函数前加std::

int main上面加一句 using namespace std;
试试


#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<string>
#include<ctime>
#include<clocale>
using namespace std;
string a,b;
bool l=true;
int ai,bi;
int main(){//32 
    cin>>a>>b;
    if(a.size()!=b.size())cout<<1;
    else if(a==b)cout<<2;
    else if(a.size()==b.size()){
        for(int i=0;i<a.size();i++){
            ai=a[i];
            bi=b[i];
            if(abs(ai-bi)==32)l==true;
            else{
                l=false;
                break;
            }
        }
        if(l==false)cout<<4;
        else cout<<3;
    }
    return 0;
}