2010-05-16 66 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

相关问题