Numbers to Letters

hi , So I have to convert numbers to letter

ex..

Input 1:

110

Output

one hundred ten dollars





This is what I have tried but don't know where to go from here.....Please help what should I do ?? thanks

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string>
using namespace std;
string convertToEnglish (int donation);
int main() {
int donation;
string singles[] = {"zero","one","two", "three","four", "five", "six" , "seven","seven","eight","nine","ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen", "eighteen","nineteen"};
string doubles[]= {"twenty","thirty","fourty","fifty", "sixty", "seventy", "eighty", "ninety"};
cin>>donation;
cout<<convertToEnglish(donation);
return 0;
}
string convertToEnglish (int donation)
{
//Put your code here
return "the result of your conversion";
}
Last edited on
As a starter, consider this for numbers 0 - 999:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <string>

std::string convertToEnglish(int donation);

int main() {
	const int donations[] {0, 6, 17, 20, 32, 45, 70, 81, 99, 111, 887, 700, 608};

	for (const auto& d : donations)
		std::cout << d << "  " << convertToEnglish(d) << '\n';

	//int donation {};
	//std::cin >> donation;
	//std::cout << convertToEnglish(donation);
}

std::string convertToEnglish(int donation)
{
	static const std::string singles[] {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven",
		"twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};
	static const std::string doubles[] {"twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"};

	std::string result;

	const int thos {donation % 1000};

	if (thos > 99)
		result += singles[thos / 100] + " hundred ";

	if (const int sing {thos % 100}; sing < 20)
			result += result.empty() || sing > 0 ? singles[sing] : "";
	else
		result += doubles[sing / 10 - 2] + (sing % 10 ? " " + singles[sing % 10] : "");

	return result;
}



0  zero
6  six
17  seventeen
20  twenty
32  thirty two
45  forty five
70  seventy
81  eighty one
99  ninety nine
111  one hundred eleven
887  eight hundred eighty seven
700  seven hundred
608  six hundred eight

Last edited on
I like my output formatted so it's easy to read, I know it's not necessary, but I just wanted to suggest it.

If you replace line 7 in @seeplus's code with:
1
2
3
std::cout << std::endl;
std::cout << d << std::right << std::setw (30) << convertToEnglish (d) << '\n';
std::cout << std::endl;

your output gets changed to:
prandtl:Desktop agentmax$ c++ -std=c++2a -o donations donations.cc
prandtl:Desktop agentmax$ ./donations

0                            zero
6                             six
17                      seventeen
20                         twenty
32                     thirty two
45                     forty five
70                        seventy
81                     eighty one
99                    ninety nine
111            one hundred eleven
887    eight hundred eighty seven
700                 seven hundred 
608             six hundred eight

prandtl:Desktop agentmax$ 


It's not really a big deal, it's just that some people (like me) like their output formatted so it's a bit cleaner, with the columns and everything.
I like my output formatted so it's easy to read,


In general for these forums, I can't be bothered - unless the question is regarding formatting. IMO if the code is functional then the OP can format any output as required.
Point taken; although my boss REQUIRES us to have a certain type of formatting in our code so it will work with the TUI he uses. So I've kind of gotten used to having output formatted with setw() and stuff like that.
I have to make the user enter the number , can you guys help me out with that part?
Look at @seeplus's code, he built-in a section that is commented out, for inputting the number. Just copy and paste his code into your IDE and test it (and uncomment lines 12-14).
why am I getting errors like this in the program ??

Compilation error
main.cpp: In function ‘std::__cxx11::string convertToEnglish(int)’:
main.cpp:27:33: error: expected ‘)’ before ‘;’ token
if (const int sing {thos % 100}; sing < 20)
^
main.cpp:27:35: error: ‘sing’ was not declared in this scope
if (const int sing {thos % 100}; sing < 20)
^~~~
main.cpp:29:2: error: ‘else’ without a previous ‘if’
else
Replace this:
if (const int sing {thos % 100}; sing < 20)
with this:
1
2
const int sing {thos % 100}; 
if (sing < 20)


p.s. @seeplus was trying to be particularly terse. You could simply replace the semicolon with a comma, and that should take care of it. I just think the comma operator confuses things in this kind of situation.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <iostream>
#include <string>
using namespace std;

const string UNITS[] = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten",
                         "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };
const string TENS[] = { "zero", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" };

//======================================================================

string expandThousand( int n )
{
   string result;
   int hundreds, tens, units;

   hundreds = n / 100;
   n %= 100;
   if ( n < 20 ) 
   {
      tens = 0;
      units = n;
   }
   else
   {
      tens = n / 10;
      units = n % 10;
   }

   if ( hundreds > 0 )
   {
      result += UNITS[hundreds] + " hundred";
      if ( n > 0 ) result += " and ";
   }

   if ( tens > 0 )
   {
      result += TENS[tens];
      if ( units > 0 ) result += "-";
   }

   if ( units > 0 )
   {
      result += UNITS[units];
   }

   return result;
}

//======================================================================

string expand( int n )
{
   string result;
   int millions, thousands, hundreds;

   millions = n / 1000000;
   n %= 1000000;
   thousands = n / 1000;
   n %= 1000;
   hundreds = n / 100;

   if ( millions > 0 )
   {
      result += expandThousand( millions ) + " million";
      if ( thousands > 0 || n > 0 ) result += ( thousands + hundreds > 0 ? ", " : " and " );
   }

   if ( thousands > 0 )
   {
      result += expandThousand( thousands ) + " thousand";
      if ( n > 0 ) result += ( hundreds > 0 ? ", " : " and " );
   }

   if ( n > 0 )
   {
      result += expandThousand( n );
   }

   return result;
}

//======================================================================

int main()
{
   int n;

   while ( true )
   {
      cout << "Enter a number between 1 and 999999999 (or 0 to finish): ";   cin >> n;
      if ( n <= 0 ) break;
      cout << expand( n ) << '\n';
   }
}

//====================================================================== 


Enter a number between 1 and 999999999 (or 0 to finish): 123456789
one hundred and twenty-three million, four hundred and fifty-six thousand, seven hundred and eighty-nine
Enter a number between 1 and 999999999 (or 0 to finish): 11
eleven
Enter a number between 1 and 999999999 (or 0 to finish): 119
one hundred and nineteen
Enter a number between 1 and 999999999 (or 0 to finish): 9876543
nine million, eight hundred and seventy-six thousand, five hundred and forty-three
Enter a number between 1 and 999999999 (or 0 to finish): 0
Last edited on
why am I getting errors like this in the program ??


You need to compile as C++17.
For the full monty, perhaps:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <iostream>
#include <string>
#include <limits>
#include <locale>

using NUMB = unsigned long long;

std::string convertToEnglish(NUMB donation);

struct comma_facet : public std::numpunct<char> {
	explicit comma_facet(unsigned char group) : g(group) {}
	char do_thousands_sep() const override { return ','; }
	std::string do_grouping() const override { return std::string(1, g); }
	unsigned char g {};
};

auto set000s(std::ostream& os, unsigned char group = 3)
{
	return os.imbue(std::locale(os.getloc(), new comma_facet(group)));
}

int main()
{
	const NUMB donations[] {18'446'744'073'709'551'615, 0, 123'456'789, 6, 111};

	set000s(std::cout);

	for (auto d : donations)
		std::cout << d << '\n' << convertToEnglish(d) << "\n\n";

	/*
	NUMB donation {};

	std::cout << "Enter a number between 0 and " << std::numeric_limits<NUMB>::max() << ": ";
	std::cin >> donation;
	std::cout << convertToEnglish(donation) << '\n';
	*/
}

std::string convertToEnglish(NUMB donation)
{
	static const std::string names[] {" quintillion ", " quadrillion ", " trillion ", " billion ", " million ", " thousand ", "", " hundred "};
	static const std::string singles[] {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven",
		"twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};
	static const std::string doubles[] {"twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"};
	static const NUMB divisor {[]() {NUMB i {1}; for (NUMB d = (std::numeric_limits<NUMB>::digits10 / 3) * 3; d--; i *= 10); return i; }()};

	std::string result;

	auto dohund = [&](auto num) {
		const auto thos {num % 1000};

		if (thos > 99)
			result += singles[thos / 100] + names[7];

		if (const auto sing {thos % 100}; sing < 20)
			result += result.empty() || sing > 0 ? singles[sing] : "";
		else
			result += doubles[sing / 10 - 2] + (sing % 10 ? " " + singles[sing % 10] : "");
	};

	if (donation == 0)
		return singles[0];

	for (auto div = divisor, nam = (decltype(divisor)) (0); div; div /= 1000, ++nam)
		if (const auto n {donation / div}; n) {
			dohund(n);
			result += names[nam];
			donation %= div;
		}

	return result;
}



18,446,744,073,709,551,615
eighteen quintillion four hundred forty six quadrillion seven hundred forty four trillion seventy three billion seven hundred nine million five hundred fifty on
e thousand six hundred fifteen

0
zero

123,456,789
one hundred twenty three million four hundred fifty six thousand seven hundred eighty nine

6
six

111
one hundred eleven

Topic archived. No new replies allowed.