0

基本程序创建一个超过 10000 的随机数,然后以 word 格式打印出该数字。

问题是,对于该eNum=atoi(Result[i]);行,我得到一个关于变量的编译器错误std::string

Error:argument of type "char" is incompatible with parameter of type "const char*" 

这意味着什么?我以为我正在拿一个单曲char并将其转换为int.

#include <iostream>
#include <stdlib.h>
#include <sstream>
#include <string>
using namespace std;


enum Numbers {Zero, One, Two, Three, Four, Five, Six, Seven, Eight, Nine, Point } eNum;

void main(void)
{
  int iRnd, iTemp;
  string Result;
  iRnd = rand() %  (sizeof(int)-10000) + 10000;
  ostringstream convert;
  convert << iRnd;
  Result = convert.str();

  cout << "\nRandmon number is: " << iRnd << endl << "Converted Number is : " << Result << endl;

  for (int i=0;i<Result.length();i++)
  {
    eNum = atoi(Result[i]);
    cout << eNum; 
    system("pause");
  }
}
4

1 回答 1

4

atoi()函数需要一个 C 字符串。要么摆脱你的整个代码并使用

int num = atoi(someString.c_str());

用于转换,或在您的代码中,更改

eNum = atoi(result[i]);

eNum = result[i] - '0';
于 2012-10-30T06:05:19.587 回答