2014-03-28 14 views
1
#include <iostream> 
#include <string> 

using namespace std; 

int main() 
{ 
    string onesPlace[] = {"one", "two", "three", "four", "five", "six", "seven", "eight", "nine",}; 
    string thoseCrazyTeens[] = {"ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen",}; 
    string tensPlace[] = {"twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety",}; 

    float userInput = 0; 
    bool flag = true; 
    cout << "Please input your check amount. " << endl; 
    while (flag == true) 
    { 
     cin >> userInput; 
     if (userInput > 100000) 
     { 
      cout << "That number is too big! Try again. " << endl; 
      flag = true; 
     } 
     else 
     { 
      flag = false; 
     } 
    } 

    int partOne = userInput; 

    return 0; 
} 

目前我正在開發這個程序,它接受用戶輸入(支票金額)並將其從數值轉換爲單詞達到100000.我想要做的一個例子是344.47美元:檢查字數?

三百四十四和67/100。

我已經得到了我需要的單詞的字符串值(數百和數千可以稍後超過一定的數值​​),現在我想弄清楚如何得到小數點後的小數。如果我使用mod,它並不總是準確的。

之後,是否有一種簡單的方法可以在沒有太多if語句的情況下通過1到20來吹?

+1

合併你的第一個2個單詞的數組,並在開頭添加「零」。數字索引將是數組中的單詞,然後 – cppguy

回答

0

mod用於整數而非十進制數。如果你想使用模式,你將不得不乘以100,然後修改100.

+0

對,我這樣做了,但它仍然導致舍入錯誤。 例如:100.45在乘以100後改爲100後得出44. – Santa

+0

絕對沒有,因爲。我沒有任何線索。 100.45是10045,10045%100是45.不應該有任何舍入。 –

+0

親自試一試,這很奇怪。 – Santa

1

要將字符串拆分爲美元和美分,不要依賴像int partOne = userInput這樣的代碼。

一般來說,sscanf是解析字符串的首選武器。這是一種C方法,但比C++替代方法更有效。

所以,這樣的事情:

int dollars; 
int cents; 

int items = sscanf (userInput.c_str(), "$%d.%d", &dollars, &cents) 

項目該值用於錯誤檢測。我強烈建議檢查它。

至於處理案例1至20 ... 一些如果聲明是必要的,但它不應該是繁重的。只要開始編碼,並隨時思考你的算法。