Converting netpay to string

GOOD MORNING, ITS 2AM AND IM TIRED LOL.

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
void convertNetPay(float netPay, char *netPayString)
{
    int numHuns, numTens, numOnes;
    char OnesTable[9][8]={"One","Two","Three","Four","Five","Six","Seven","Eight","Nine"};
    char ResultString[10+1];
    char TensTable[9][8] = {"Ten","Twenty","Thirty","Forty","Fifty","Sixty","Seventy","Eighty","Ninety"};
    char TeensTable[9][10] {"Eleven","Twelve","Thirteen","fourteen","fifteen","Sixteen","Seventeen","Eighteen","Nineteen"};
    float cents;
 
    cents = netPay - int(netPay);
 
    strcpy(netPayString,"The sum of ");
 
    numHuns = int(netPay) / 100;
 
    if (numHuns > 0)
    {
    strcat(netPayString,OnesTable[numHuns-1]);
    strcat(netPayString," Hundred ");
    }
    int remainder =  int(netPay) % 100;
    /*if  (remainder ==0)
    {
        //do something
    }*/
         if ((remainder>=11) || (remainder<=19))
        {
            strcat(netPayString, TeensTable[remainder -11]);
        }
            else{
                numTens = int(netPay) % 100 / 10;
                numOnes = int(netPay) % 100 % 10;
                if (numTens > 0)
                {
                    strcat(netPayString,TensTable[numTens -1]);
                    strcat(netPayString," - ");
                }
                if (numOnes > 0)
                {
                    strcat(netPayString,OnesTable[numOnes-1]);
                    strcat(netPayString," Dollars and ");
                }
            cents = cents + 0.005;
            cents = cents * 100;
 
            sprintf(ResultString,"%d",int(cents));
            strcat(netPayString,ResultString);
            }
}


BASICALLY, I have to convert netpay (float to string).
Theoretically if I have 666.66, my program here should output the sum of "six hundred sixty-six and 66/100 dollars".
Any help is appreciated, thank you!
Impossible. At least, too hard if you only want it to appear cute.
Oh, it's quite possible.
Just takes a little thought and some effort to get it right.
@long double man
I know, but I don't have this time, haha.
You could use a map of strings and their values, and simply write them in:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
std::string floatToString(float f) {
    const std::map<int, std::string> nums {
        {1, "one"},
        {2, "two"},
        {3, "three"},
        //...
    };

    std::string ret;

    if (f > 99.99) {
        ret += nums[f / 100] + " hundred ";
        f -= static_cast<int>(f / 100) * 100;
    }

    // so on

    return ret;
}


EDIT:
Oops, messed up how you downscale 'f'.
Last edited on
Topic archived. No new replies allowed.