views:

73

answers:

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

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

+3  A: 

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 "";
}
W_P
yeah thats what the oringinal program was paul but now our professor wants us to make it so the only thing we have is int_name and int_main she wants us to move the bodies into the int_name function somehow and thats where im getting lost
Timothy Poseley
+1  A: 
Lirik
Why not make that a comment instead of an answer?
Georg Fritzsche
(3) Write another version of program intname.cpp where you only have function int_name(int n), but NOT the other three functions digit_name, teen_name, and tens_name. What will your program look like? Get it to compile and run ... Hint: the bodies of funtions digit_name, teen_name and tens_name will need to appear within the body of int_name in the proper locations with proper namings of variables
Timothy Poseley
thats the assignment and the code i posted is my attempt at converting it i know its horrible our c++ book has nothing to help
Timothy Poseley
so the _bodies_ of the functions will need appear within the body of int_name, not the function definitions + body.
W_P
how do i do that
Timothy Poseley
@Timothy, I've updated my response with some sample code, but you should check with your teacher, because moving the bodies of the functions inside the body of int_name will produce some duplicate code which is a **bad practice** in programming.
Lirik