开发者

I keep getting a 'no match for call to' error

#include <iostream>
#include <str开发者_C百科ing>

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;
}

I Keep getting this Error code:

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&)â


You are using digit_name, teen_name, etc as functions, when they are defined as variables. If you want to use them like that, you need to define them before your int_name function like this:

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 "";
}


Timothy, it looks like you're confused about the requirements of the assignment. Please make sure you understand the requirements, because at this stage it doesn't look like you know what's expected of you. You're trying to move the body of one function into the body of another function and that's simply not possible to do.

Please post the exact words that your teacher used in order for us to give you proper advice on the question.

Here are some tips for you:

  • If your teacher has covered switch statements then use switch statements.
  • Check if your teacher is not asking you to do function declarations.
  • Check if your teacher is not asking you to put the functions in libraries (a header file and source file).

OK scrap the tips... given your teacher's requirements I think it might look a little bit like this:

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;
}

Note that I'm using a switch statement, but if you your teacher hasn't shown you switch statements yet, then you can still use if/else statements:

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;
}

You're going to have to take the first example with digit_name and apply it to tens_name and teen_name functions.

WARNING:
In reality you don't want to repeat the same code and clutter a single function with a bunch of code that could be in its own function. You ALWAYS want to break out repeating code into functions... if she's asking you to repeat code when you can use functions then you should be concerned. Ask your teacher if this is what she REALLY wants you to do!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜