为什么 #include 中的 atoi() 函数<cstdlib>不起作用



我创建了一个程序,用字符串(r(将数字转换为二进制格式,现在我想将其转换为整数数据类型,我在谷歌上找到了atoi((函数(从cstdlib导入(,用于从字符串转换为整数,但它不起作用。

这是我的代码-它显示错误点击这里查看它

#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
int num,n;string r;
cout<<"Enter the number : ";
cin>>num;
while(num!=0){r = (num%2==0?"0":"1")+r;num/=2;}
cout<<"nBinary value is "<<r<<endl;
n = atoi(r);
cout<<n;
return 0;
}

atoi()采用char数组(例如char xd[210](。如果要使用字符串,请使用stoi()

atoi()需要一个const char *(char的数组(,其中rstd::string对象。您可以使用c_str()方法将string转换为const char *

atoi(r.c_str())

相关内容

最新更新