2012-10-30 51 views
0

基本程序創建一個10000以上的隨機數然後打印出一個字格式的數字。字符串的C++轉換爲整數一個字符的時間

的問題是,對於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"); 
    } 
} 
+5

對於簡單character->整數轉換,您可以使用'c - '0',只要'c'在'['0','9']'範圍內。 – chris

+2

請閱讀'atoi()'函數的文檔 - 做完之後,一切都會清晰。 – 2012-10-30 06:00:02

+0

這段代碼甚至沒有編譯,你希望我們如何提供幫助? –

回答

4

atoi()函數需要一個C字符串。無論是擺脫你的整個代碼,並使用

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

轉換,或者在你的代碼,改變

eNum = atoi(result[i]); 

eNum = result[i] - '0'; 
相關問題