我一直收到“不匹配的电话”错误

时间:2010-05-16 17:43:49

标签: c++

#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:在函数âstd:: string中   int_name(INT)A:
  intname2.cpp:74:错误:不匹配   调用â(std :: string)(int)â   intname2.cpp:80:错误:不匹配   调用â(std :: string)(int)â   intname2.cpp:86:错误:不匹配   调用â(std :: string)(int&amp;)â   intname2.cpp:91:错误:不匹配   调用â(std :: string)(int&amp;)â

2 个答案:

答案 0 :(得分: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 "";
}

答案 1 :(得分: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语句,但如果您的教师尚未向您显示切换语句,那么您仍然可以使用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个函数。

警告:
实际上,您不希望重复相同的代码,并使用一堆可能在其自身功能中的代码来混淆单个函数。你总是想把重复的代码分解成函数......如果她在你可以使用函数时要求你重复代码那么你应该关注。问你的老师,这是她真的希望你做的事情!