2013-04-05 101 views
0

試圖使用適當的貨幣面額爲我的遊戲。貨幣存儲爲一個字符串(即不能由於我的教授而改變),並且按照白金,金,銀和銅的順序存儲。例如,如果我將我的貨幣初始化爲「0.1.23.15」,這意味着我有0白金,1黃金,23白銀和15銅。貨幣面額

但是,我需要能夠轉換到更高的面額。那是什麼意思?一個例子是如果我有105個銀片(即0.0.105.0),它應該顯示爲1個金和5個銀(即0.1.5.0)。

我在我的setCost方法中加粗了我的問題。我正在檢查一個大於100的數字,如果是 - 我使該列爲0,返回到前一個元素,並向ASCII值加1,以提供適當的進位。不幸的是,調試器顯示「/ x4」正被轉儲到元素中,而不是「4」。有誰知道這是爲什麼,我怎麼能改變它?

編輯:編輯的代碼,只要你不輸入一個數字它的工作原理上面100有一個關於如何使它超過100

較大的數字工作的大腦經過這一番我曾經寫過的最不經意的代碼。請溫柔。 :(

void Potion::setCost(std::string cost) 
{ 
    char buffer[256]; 
    std::string currencyBuffer [4]; 
    int integerBuffer[4]; 
    int * integerPointer = nullptr; 
    int temp = 0; 
    int i = 0; 
    char * tokenPtr; 
    //Convert string to cString 
    strcpy(buffer, cost.c_str()); 

    //Tokenize cString 
    tokenPtr = strtok(buffer, "."); 

    while(tokenPtr != nullptr) 
    { 
     //Convert ASCII to integer 
     temp = atoi(tokenPtr); 

     //Store temp into currency buffer 
     integerBuffer[i] = temp; 

     //Make pointer point to integer buffer 
     integerPointer = &integerBuffer[i]; 

     if(*integerPointer < 100) 
      currencyBuffer[i] = tokenPtr; 
     else 
     { 
      //Store zero in column if number is 
      //greater than 100 
      temp2 = temp % 100; 
      itoa(temp2, temp3, 10); 
      currencyBuffer[i] = temp3; 

      //Go back and add one to currency buffer 
      temp = atoi(currencyBuffer[i-1].c_str()); 
      temp += 1; 
      itoa(temp, temp3, 10); 
      currencyBuffer[i - 1] = temp3; 
     } 

     i++; 

     //Get next token 
     tokenPtr = strtok(nullptr, "."); 
    } 
    NewLine(); 

    std::string tempBuffer; 

    //Store entire worth of potions 
    tempBuffer = "Platinum: "; 
    tempBuffer += currencyBuffer[0]; 
    tempBuffer += "\nGold: "; 
    tempBuffer += currencyBuffer[1]; 
    tempBuffer += "\nSilver: "; 
    tempBuffer += currencyBuffer[2]; 
    tempBuffer += "\nCopper: "; 
    tempBuffer += currencyBuffer[3]; 

    mCost = tempBuffer; 
} 
+0

uhm ...不知道這裏,但是,爲什麼0?如果是150S,那應該是1G50S,不是嗎?你需要看看分裂和餘下......這應該有所幫助:http://www.daniweb.com/software-development/cpp/threads/9349/c-division-remainder-help注意最後一個帖子,這是非常重要的... – MaxOvrdrv 2013-04-05 03:13:35

+0

這絕對是我的錯。它應該是模數算子得到餘數(即如果它是105,其餘的將是5,其中一個作爲進位)。 – MrPickle5 2013-04-05 03:17:18

回答

1

我認爲這個問題是在這一行:

currencyBuffer[i - 1] = temp; 

你爲一個字符串(currencyBuffer[i-1]),這將導致垃圾字符寫入此指派一個int(temp)。是允許的,顯然是: (Why does C++ allow an integer to be assigned to a string?) 因爲整數可以隱式轉換爲字符,並字符可以被分配到字符串,.

你想臨時轉換爲使用012一個char或類似的函數(當你從字符串中獲得int時,你已經做了相反的處理,atoi)。

既然你在C++的時候,一個簡單的方法來做到這一點是:

std::stringstream itos; 
itos << temp; 
currencyBuffer[i-1] = itos.c_str(); 
+0

非常感謝。我用itoa,現在它完美地工作。不幸的是,我無法弄清楚如何解決「temp2 = temp%100」的問題線。如果用戶輸入200,那麼應該有兩個進位,但是現在我只需要設置一個進位而不管(即如果數字大於100)。 – MrPickle5 2013-04-05 03:35:26

+0

是的,因爲你有命令「返回並添加一個貨幣緩衝區」。 :)你想把'temp + = 1;'換成'temp + = atoi(tokenPtr)/ 100;' – maditya 2013-04-05 03:39:05

+0

'%'得到餘數。你應該有另一個變量,我們稱之爲'quotient',得到商:'quotient = temp/100;'。用一個整數除以另一個將得到商,因此將例如205乘100會給你2.然後你說'temp + =商數''。在前面的評論中,我沒有把事物放在一個名爲'quotient'的單獨變量中,而是因爲我很懶,我只是用'atoi'重新獲得了這個數字。 – maditya 2013-04-05 03:42:36

1

不知道這只是我在這裏(我的C++天回去約13歲),和你的老師會最適合回答你這個問題,但感覺就像你在做什麼/你是如何做的,這是非常強大的處理器。從技術上講,你很可能會關閉整個字符串分割成字符串數組,然後使用這些來確定你的最終計數更好:

std::string str = "I.have.a.dog"; 
//replace all DOTS with SPACES for next use 
for (int i = 0; i < str.length(); ++i) { 
    if (str[i] == '.') 
     str[i] = ' '; 
} 
std::istringstream stm(str) ; 
string word ; 
while(stm >> word) // read white-space delimited tokens one by one 
{ 
    // put word into array 
} 

從那裏,你有一個數組,用正確的文本/字,進入一個數組,你可以用它來做你的計算...雖然...不要引用我;

1

這是我創建的函數來解析你的號碼。它沒有問題的數字大於...使用它,如果你願意=)

unsigned long int str2cur(std::string cost) 
{ 
    unsigned long int money = 0; 
    //given that there are always 4 segments 
    int end; 
    do 
    { 
     end = cost.find('.', 0); 
     money = money * 100 + atoi(cost.substr(0, end).c_str()); 
     cost.erase(0, end + 1);   
    } 
    while (end != std::string::npos); 
    return money; 
} 
+0

此功能的輸出是最低面額的總金額 – Kupto 2013-04-05 03:51:45