2010-05-16 65 views
0
#include <iostream> 
#include <string> 

using namespace std; 


// Turns a digit between 1 and 9 into its english name 
// Turn a number into its english name 

string int_name(int n) 
{ 

    string digit_name; 
    { 
    if (n == 1) return "one"; 
    else if (n == 2) return "two"; 
    else if (n == 3) return "three"; 
    else if (n == 4) return "four"; 
    else if (n == 5) return "five"; 
    else if (n == 6) return "six"; 
    else if (n == 7) return "seven"; 
    else if (n == 8) return "eight"; 
    else if (n == 9) return "nine"; 

    return ""; 
    } 


    string teen_name; 
    { 
    if (n == 10) return "ten"; 
    else if (n == 11) return "eleven"; 
    else if (n == 12) return "twelve"; 
    else if (n == 13) return "thirteen"; 
    else if (n == 14) return "fourteen"; 
    else if (n == 14) return "fourteen"; 
    else if (n == 15) return "fifteen"; 
    else if (n == 16) return "sixteen"; 
    else if (n == 17) return "seventeen"; 
    else if (n == 18) return "eighteen"; 
    else if (n == 19) return "nineteen"; 

    return ""; 
    } 


    string tens_name; 
    { 
    if (n == 2) return "twenty"; 
    else if (n == 3) return "thirty"; 
    else if (n == 4) return "forty"; 
    else if (n == 5) return "fifty"; 
    else if (n == 6) return "sixty"; 
    else if (n == 7) return "seventy"; 
    else if (n == 8) return "eighty"; 
    else if (n == 9) return "ninety"; 

    return ""; 
    } 

    int c = n; // the part that still needs to be converted 
    string r; // the return value 

    if (c >= 1000) 
    { 
    r = int_name(c/1000) + " thousand"; 
    c = c % 1000; 
    } 

    if (c >= 100) 
    { 
    r = r + " " + digit_name(c/100) + " hundred"; 
    c = c % 100; 
    } 

    if (c >= 20) 
    { 
    r = r + " " + tens_name(c /10); 
    c = c % 10; 
    } 

    if (c >= 10) 
    { 
    r = r + " " + teen_name(c); 
    c = 0; 
    } 

    if (c > 0) 
    r = r + " " + digit_name(c); 

    return r; 
} 

int main() 
{ 
    int n; 
    cout << endl << endl; 
    cout << "Please enter a positive integer: "; 
    cin >> n; 
    cout << endl; 
    cout << int_name(n); 
    cout << endl << endl; 

    return 0; 
} 

我不斷收到此錯誤代碼:我一直得到「不匹配調用」錯誤

intname2.cpp: In function âstd::string int_name(int)â:
intname2.cpp:74: error: no match for call to â(std::string) (int)â
intname2.cpp:80: error: no match for call to â(std::string) (int)â
intname2.cpp:86: error: no match for call to â(std::string) (int&)â
intname2.cpp:91: error: no match for call to â(std::string) (int&)â

+2

問題有一個標題,以*請幫助*和正文包含格式不良的源代碼通常不讚賞SO。 – 2010-05-16 17:45:32

+4

你有沒有想過你正在創建本地功能? C++沒有這樣的東西。 – 2010-05-16 17:48:58

+5

你真的應該閱讀一本C++書籍。 – 2010-05-16 17:50:14

回答

3

您使用digit_nameteen_name等的功能,當它們被定義爲變量。如果你想使用它們那樣,你需要你的int_name函數之前定義它們是這樣的:

string digit_name(int n) 
{ 
if (n == 1) return "one"; 
else if (n == 2) return "two"; 
else if (n == 3) return "three"; 
else if (n == 4) return "four"; 
else if (n == 5) return "five"; 
else if (n == 6) return "six"; 
else if (n == 7) return "seven"; 
else if (n == 8) return "eight"; 
else if (n == 9) return "nine"; 

return ""; 
} 
+0

是啊,那是什麼原型程序是保羅,但現在我們的教授希望我們做到這一點,所以我們唯一擁有的是int_name和int_main她希望我們以某種方式將屍體移動到int_name函數中,這就是我迷失的地方 – 2010-05-16 18:00:14

1

蒂莫西,它看起來像你困惑的任務的要求。請確保您瞭解這些要求,因爲在此階段,您看起來並不像您所期望的那樣。你試圖將一個函數的主體移動到另一個函數的主體中,這根本無法做到。

請發佈確切詞,您的老師使用爲了讓我們給你適當的問題的建議。

下面是一些提示您:

  • 如果您的老師已覆蓋switch語句然後使用switch語句。
  • 檢查你的老師是不是要求你做功能聲明。
  • 檢查你的老師是不是讓你把函數放在函數庫中(頭文件和源文件)。

確定報廢的提示...給你的老師的要求,我想可能是這樣一點點:

string int_name(int n) 
{ 
    int c = n; // the part that still needs to be converted 
    string r; // the return value 

    if (c >= 1000) 
    { 
     r = int_name(c/1000) + " thousand"; 
     c = c % 1000; 
    } 

    if (c >= 100) 
    { 
     // If you have covered switch statements then it will look like this 
     string digitName; 
     switch(c/100) // <- instead of calling digit_name(c/100), we call switch(c/100) 
     { 
      case 1: 
       // assign the digit name 
       digitName = "one"; 
       break; 
      case 2: 
       //... fill here with your own code 
       break; 
      case 3: 
       //... fill here with your own code 
       break; 
      // write all the cases through 9 
      default: 
       digitName = ""; 
       break; 
     } 

     // in the result string use the digitName variable 
     // instead of calling the digit_name function 
     r = r + " " + digitName + " hundred"; 
     c = c % 100; 
    } 

    if (c >= 20) 
    { 
     r = r + " " + tens_name(c /10); 
     c = c % 10; 
    } 

    if (c >= 10) 
    { 
     r = r + " " + teen_name(c); 
     c = 0; 
    } 

    if (c > 0) 
     r = r + " " + digit_name(c); 

    return r; 
} 

請注意,我使用的是switch語句,但是如果你的老師並沒有表現出你switch語句呢,那麼你仍然可以使用if/else語句:

string int_name(int n) 
{ 
    int c = n; // the part that still needs to be converted 
    string r; // the return value 

    if (c >= 1000) 
    { 
     r = int_name(c/1000) + " thousand"; 
     c = c % 1000; 
    } 

    if (c >= 100) 
    { 
     // declare a digitName 
     string digitName; 

     // declare a temporary value 
     int temp = c/100; 
     if(1 == temp) 
     { 
      // assign the digit name 
      digitName = "one"; 
     } 
     else if(2 == temp) 
     { 
      digitName = "two"; 
     } 
     else if(3 == temp) 
     { 
      // fill in the rest 
     } 
     else if(4 == temp) 
     { 
      // fill in the rest 
     } 
     // write all the other else if statements 
     else 
     { 
      digitName = "": 
     } 

     // in the result string use the digitName variable 
     // instead of calling the digit_name function 
     r = r + " " + digitName + " hundred"; 
     c = c % 100; 
    } 

    if (c >= 20) 
    { 
     r = r + " " + tens_name(c /10); 
     c = c % 10; 
    } 

    if (c >= 10) 
    { 
     r = r + " " + teen_name(c); 
     c = 0; 
    } 

    if (c > 0) 
     r = r + " " + digit_name(c); 

    return r; 
} 

你將不得不採取的第一個例子與digit_name並將其應用到tens_nameteen_name功能。

警告:
在現實中,你不想重複相同的代碼和雜波有一堆代碼,可能是在它自己的功能的單一功能。你總是想要重複代碼到功能中......如果她要求你重複代碼,當你可以使用功能,那麼你應該擔心。問問你的老師,如果這是她真的希望你做!

+0

爲什麼不讓它成爲評論而不是答案? – 2010-05-16 18:19:25

+0

(3)編寫另一個版本的程序intname.cpp,其中 你只有函數int_name(int n),但不是 其他三個函數digit_name,teen_name, 和tens_name。你的程序會是什麼樣子? 得到它的編譯和運行... 提示:funtions digit_name的屍體,teen_name 和tens_name將需要與變量正確 namings – 2010-05-16 18:20:44

+0

這就是分配和適當的位置 體int_name內出現我發佈的代碼是我嘗試轉換它,我知道它的可怕我們的C++書沒有任何幫助 – 2010-05-16 18:21:55

相關問題