题目:输入一个表示整数的字符串,把该字符串转换成整数并输出
例如:输入字符串”345“,则输出整数345
代码如下:
#include <iostream>
#include <string>
#include <math.h>
using namespace std;
void main()
{
string str;
cout<<"输入一个表示整数的字符串" <<endl;
cin >> str;
int len = str.length(),num=0;
//cout << len << endl;
for (int i = 0 ; i<len ; ++i)
{
num+=(int)pow(10.0,len-1-i)*(str[i]-'0');
//cout<<num<<endl;
}
cout << num <<endl;
system("pause");
}