Currency to words C++

Im trying to make a code that displays currency such as (312,12) in words (three hundred twelve dollars and twelve cents.

#include <iostream>
#include <string>
#include <cmath>

using namespace std;

const int HUNDRED= 0;
const int THOUSAND= 3;



string double_name(double number);
string hundred_name(int number, int group);
string tens_name(int number);
string cents_name(int number);
string digit_name(int number);
string teen_name(int number);



int main()
{

double number;


do
{
cout << "Please enter a currency no greater then (999,999.99): ";
cin >> number;

if (number < 0);
{
cout << "Invalid number." << endl;

}
} while (number < 0);

cout << "\n" << double_name(number) << endl;

system("pause");
return 0;
}

string double_name(double number)
{
string name = "";
int dollars = number;
int cents = (number - dollars) * 100;

if (dollars == 0)
{
name += "zero ";
}
if (dollars >= 1000)
{
name += hundred_name(dollars, THOUSAND) + "thousand ";
dollars %= 1000;

}

if (dollars > 0)
{
name += hundred_name(dollars, HUNDRED) + " ";
}


name += "dollars and ";

name += cents_name(cents);

return 0;
}

string hundred_name(int number, int group)
{

string name = "";
int number_r = 100 * pow(10, group);
int hundreds_p = number / number_r;
number %= number_r;
int tens_p = number / (number_r / 100);

if (hundreds_p > 0)
name = digit_name(hundreds_p) + " hundred ";


if (tens_p > 0)
{
name += tens_name(tens_p);
}

return name;
}
string tens_name(int number)
{
string name = "";


if (number >= 20)
{
name += tens_name(number) + " ";
number %= 10;
}
else if (number >= 10)
{
name += teen_name(number);
number = 0;
}

if (number > 0)
{
name += digit_name(number);
}


return name;
}


string digit_name(int number)
{
switch (number)
{
case 1:
return "one";
case 2:
return "two";
case 3:
return "three";
case 4:
return "four";
case 5:
return "five";
case 6:
return "six";
case 7:
return "seven";
case 8:
return "eight";
case 9:
return "nine";
case 10:
return "ten";
case 11:
return "eleven";
case 12:
return "twelve";
case 13:
return "thirteen";
case 14:
return "fourteen";
case 15:
return "fifteen";
case 16:
return "sisteen";
case 17:
return "seventeen";
case 18:
return "eighteen";
case 19:
return "nineteen";
}
return "";
}

string teen_name(int number)
{
switch (number)
{
case 1:
return "twenty";
case 2:
return "thirty";
case 3:
return "three";
case 4:
return "forty";
case 5:
return "fifty";
case 6:
return "sixty";
case 7:
return "seventy";
case 8:
return "eighty";
case 9:
return "ninety";
return "";

}
}
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
#include <iostream>
#include <sstream>
#include <string>
using namespace std;

void print_thousands(ostream& out, unsigned n) {
    static const char
    *ones[] {
        "", "one", "two", "three", "four", "five", "six", "seven", "eight",
        "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen",
        "sixteen", "seventeen", "eighteen", "nineteen"
    },
    *tens[] {
        "", "ten", "twenty", "thirty", "forty",
        "fifty", "sixty", "seventy", "eighty", "ninety"
    };
    if (n > 99) out << ones[n / 100] << " hundred ";
    n %= 100;
    if (n > 19) {
        out << tens[n / 10] << ' ';
        n %= 10;
    }
    if (n > 0) out << ones[n] << ' ';
}

void number_to_words(ostream& out, unsigned long long n, int mag = 0) {
    static const char* thous[] {
        "", "thousand", "million", "billion", "trillion",
        "quadrillion", "quintillion"
    };
    if (n == 0) {
        if (mag == 0) out << "zero";
    }
    else {
        number_to_words(out, n / 1000, mag + 1);
        print_thousands(out, n % 1000);
        if (n % 1000 && mag) out << thous[mag] << ' ';
    }
}

void number_to_words(ostream& out, long long n) {
    if (n < 0) {
        out << "negative ";
        n = -n;
    }
    number_to_words(out, (unsigned long long)n);
}

int main()
{
    long long n;
    while (cout << ">> ", cin >> n) {
        ostringstream out;
        number_to_words(out, n);  // or pass cout if you don't need a string
        cout << out.str() << '\n';
    }
    cout << '\n';
}

Last edited on
dutch this will not work as it does not allow for you to use decimals such as .34. i want the code to read cents
You're not thinking!
I'm just showing you the idea.
You basically just use a pared-down version of the above, first for what's before the decimal then for what's after it. It would be best to read the dollar amount and cent amount into different ints, then just print the words for each of those.
Thanks dutch for your help i was able to get the code working. after your message i took another go at it and i got it to work.

thank you.
Topic archived. No new replies allowed.