C++ Challenge

Jul 26, 2016 at 8:56pm
I cant figure out how to print the correct dollar amount in words. Any help would be greatly appreciated.
Last edited on Jul 27, 2016 at 2:17am
Jul 26, 2016 at 8:57pm
This is what the output should look like:

Date: 03/05/2016
Pay to the order of: Maya Tolappa $0.00
zero dollars and 0 cent
---------------------------------------------------------------------
Date: 03/05/2016
Pay to the order of: Maya Tolappa $0.01
zero dollars and 1 cent
---------------------------------------------------------------------
Date: 03/05/2016
Pay to the order of: Maya Tolappa $0.25
zero dollars and 25 cents
---------------------------------------------------------------------
Date: 03/05/2016
Pay to the order of: Maya Tolappa $12.00
twelve dollars and 0 cent
---------------------------------------------------------------------
Date: 03/05/2016
Pay to the order of: Maya Tolappa $12.45
twelve dollars and 45 cents
---------------------------------------------------------------------
Date: 03/05/2016
Pay to the order of: Maya Tolappa $19.00
nineteen dollars and 0 cent
---------------------------------------------------------------------
Date: 03/05/2016
Pay to the order of: Maya Tolappa $19.02
nineteen dollars and 2 cents
---------------------------------------------------------------------
Date: 03/05/2016
Pay to the order of: Maya Tolappa $19.45
nineteen dollars and 45 cents
---------------------------------------------------------------------
Date: 03/05/2016
Pay to the order of: Maya Tolappa $20.00
twenty dollars and 0 cent
---------------------------------------------------------------------
Date: 03/05/2016
Pay to the order of: Maya Tolappa $20.45
twenty dollars and 45 cents
---------------------------------------------------------------------
Date: 03/05/2016
Pay to the order of: Maya Tolappa $34.00
thirty four dollars and 0 cent
---------------------------------------------------------------------
Date: 03/05/2016
Pay to the order of: Maya Tolappa $56.78
fifty six dollars and 78 cents
---------------------------------------------------------------------
Date: 03/05/2016
Pay to the order of: Maya Tolappa $100.00
one hundred dollars and 0 cent
---------------------------------------------------------------------
Date: 03/05/2016
Pay to the order of: Maya Tolappa $109.00
one hundred nine dollars and 0 cent
---------------------------------------------------------------------
Date: 03/05/2016
Pay to the order of: Maya Tolappa $119.78
one hundred nineteen dollars and 78 cents
---------------------------------------------------------------------
Date: 03/05/2016
Pay to the order of: Maya Tolappa $450.00
four hundred fifty dollars and 0 cent
---------------------------------------------------------------------
Date: 03/05/2016
Pay to the order of: Maya Tolappa $678.90
six hundred seventy eight dollars and 90 cents
---------------------------------------------------------------------
Date: 03/05/2016
Pay to the order of: Maya Tolappa $1000.00
one thousand dollars and 0 cent
---------------------------------------------------------------------
Date: 03/05/2016
Pay to the order of: Maya Tolappa $1009.45
one thousand nine dollars and 45 cents
---------------------------------------------------------------------
Date: 03/05/2016
Pay to the order of: Maya Tolappa $1056.00
one thousand fifty six dollars and 0 cent
---------------------------------------------------------------------
Date: 03/05/2016
Pay to the order of: Maya Tolappa $1234.00
one thousand two hundred thirty four dollars and 0 cent
---------------------------------------------------------------------
Date: 03/05/2016
Pay to the order of: Maya Tolappa $1567.98
one thousand five hundred sixty seven dollars and 98 cents
---------------------------------------------------------------------
Date: 03/05/2016
Pay to the order of: Maya Tolappa $9999.00
nine thousand nine hundred ninety nine dollars and 0 cent
---------------------------------------------------------------------
Date: 03/05/2016
Pay to the order of: Maya Tolappa $9999.99
nine thousand nine hundred ninety nine dollars and 99 cents
---------------------------------------------------------------------
Press any key to continue . . .
Jul 26, 2016 at 10:45pm
closed account (48T7M4Gy)
Is pasting your assignment here all you plan on doing? We'll need more than that to help you. Best idea is to show us an attempt at creating the class.
Jul 27, 2016 at 12:49am
I have this so far:

.h file:

#ifndef TEXTVERSIONOFNUMBER_H
#define TEXTVERSIONOFNUMBER_H

#include <iostream>
#include <iomanip>
#include <string>
#include <string.h>
#include <sstream>

class TextVersionOfNumber
{
private:
double amount;

public:
std::string getTextVersionOfNumber();
void setAmount(double _amount);
};

#endif

________________________________________________________
.cpp file:

#include "TextVersionOfNumber.h"

std::string TextVersionOfNumber::getTextVersionOfNumber()
{
std::string one_19[20] = { "", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };

std::string twenty_90[10] = { "", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" };

double _amount = amount;

double _amount2 = (amount - (_amount)) * 100;//0

std::string str_one = "";

if (_amount <= 19)
{
str_one = one_19[(int)_amount];
}
else
{
int tens = _amount / 10;
int ones = _amount - (tens * 10);
str_one = one_19[ones] + twenty_90[ones];
}

std::string str_two = "";

if (_amount2 <= 19)
{
str_two = one_19[(int)_amount2];
}
else
{
int tens = _amount2 / 10;
int ones = _amount2 - (tens * 10);
str_two = twenty_90[tens] + one_19[ones];
}

std::string ret_val = str_one + " dollars and " + str_two + " cents";
return ret_val;
}

void TextVersionOfNumber::setAmount(double _amount)
{
amount = _amount;
};
Jul 27, 2016 at 12:51am
I cant get the program to properly display the $ amount in words
Jul 27, 2016 at 2:07am
closed account (48T7M4Gy)
What does your main() program look like?
Jul 27, 2016 at 2:12am
#include <iostream>
#include <iomanip>
#include <string>
#include <string.h>
using namespace std;

#include "TextVersionOfNumber.h"

int main()
{
string date = "03/05/2016", payee = "Maya Tolappa";
TextVersionOfNumber checkAmount;
double testAmounts [] = {0, .01, .25, 12, 12.45, 19, 19.02,
19.45, 20, 20.45,
34, 56.78, 100, 109, 119.78,
450, 678.90, 1000, 1009.45, 1056,
1234, 1567.98,9999, 9999.99};

for(int i=0; i < sizeof(testAmounts)/sizeof(double); i++)
{
double an_amount = testAmounts[i];
checkAmount.setAmount(an_amount);
cout << setprecision(2) << fixed;
cout << setw(60) << right;
cout << "Date: " << date << endl;
cout << "Pay to the order of: " << payee << "\t\t\t";
cout << "$" << an_amount << endl;
cout << checkAmount.getTextVersionOfNumber() << endl;
cout << "---------------------------------------------------------------------\n";
}
return 0;
}
Jul 27, 2016 at 3:28am
closed account (48T7M4Gy)
Next question. What output do you get and what's wrong with it?
Jul 27, 2016 at 3:44am
@Snick

I do notice that in your TextVersionOfNumber function, you are only trying to find the tens and ones, but not hundreds or even the thousands. To get the full check value, you're going to need them.
Jul 27, 2016 at 4:40am
closed account (48T7M4Gy)
First step: Concentrate on how many cents in the amount and the number of tens and the number of units. Convert those two to a string.

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
string TextVersionOfNumber::getTextVersionOfNumber()
{
    string one_19[20] =
    {
        "", "one", "two", "three", "four", "five", "six", "seven",
        "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen",
        "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"
    };
    
    string twenty_90[10] =
    {
        "", "", "twenty", "thirty", "forty", "fifty",
        "sixty", "seventy", "eighty", "ninety"
    };
    
    //STEP 1 - CENTS
    string cents_str = " and ";
    
    int cents = 100 * amount - (int)amount * 100;
    if( cents > 0)
    {
        int ten_cents = cents/10;
        int single_cents = cents % 10;
        cents_str += twenty_90[ten_cents] + '-' + one_19[single_cents];
    }
    else cents_str += "zero";
    
    cents_str += " cents";
    
    return cents_str;
}
Last edited on Jul 27, 2016 at 4:57am
Topic archived. No new replies allowed.