C++ Question: Given Tester file: // Chapter 12-- Assignment 14: Check Writer //
ID: 3697871 • Letter: C
Question
C++ Question:
Given Tester file:
// Chapter 12-- Assignment 14: Check Writer
// This program can convert a dollar and cents amount given in
// numerical form to a word description of the amount.
#include <iostream>
#include <iomanip>
#include <string>
#include <string.h>
using namespace std;
#include "TextVersionOfNumber.h"
// Assume a maximum amount of $10,000
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 << " ";
cout << "$" << an_amount << endl;
cout << checkAmount.getTextVersionOfNumber() << endl;
cout << "--------------------------------------------------------------------- ";
}
cin.get();
return 0;
}
Given h file:
#include<iostream>
#include<string>
#include <iomanip>
using namespace std;
class TextVersionOfNumber
{
private:
double amount;
public:
string getTextVersionOfNumber();
void setAmount(double);
};
I need to return the numbers as a string using array processing with decision structures.
This is what I have so far...
#include "TextVersionOfNumber.h"
string TextVersionOfNumber::getTextVersionOfNumber()
{
string one_19[] = { "", "one", "two", "three", "four",
"five", "six", "seven", "eight", "nine", "ten",
"eleven", "twelve",
"thirteen", "fourteen", "fifteen", "sixteen", "seventeen",
"eighteen", "nineteen" };
string twenty_90[] = { "", "", "twenty", "thirty", "forty",
"fifty", "sixty", "seventy", "eighty", "ninety" };
}
void TextVersionOfNumber::setAmount(double _amount)
{
amount = _amount;
}
Is anyone able to finish this so that it works? I know that the tester and h file are correct, but i need help with the last file that I started. Thanks!
Explanation / Answer
void convert_to_words(char *num)
{
int len = strlen(num);
if (len == 0) {
fprintf(stderr, "empty string ");
return;
}
if (len > 4) {
fprintf(stderr, "Length more than 4 is not supported ");
return;
}
char *single_digits[] = { "zero", "one", "two", "three", "four",
"five", "six", "seven", "eight", "nine"};
char *two_digits[] = {"", "ten", "eleven", "twelve", "thirteen", "fourteen",
"fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};
char *tens_multiple[] = {"", "", "twenty", "thirty", "forty", "fifty",
"sixty", "seventy", "eighty", "ninety"};
char *tens_power[] = {"hundred", "thousand"};
printf(" %s: ", num);
if (len == 1) {
printf("%s ", single_digits[*num - '0']);
return;
}
while (*num != '') {
if (len >= 3) {
if (*num -'0' != 0) {
printf("%s ", single_digits[*num - '0']);
printf("%s ", tens_power[len-3]); // here len can be 3 or 4
}
--len;
}
else {
if (*num == '1') {
int sum = *num - '0' + *(num + 1)- '0';
printf("%s ", two_digits[sum]);
return;
}
else if (*num == '2' && *(num + 1) == '0') {
printf("twenty ");
return;
}
else {
int i = *num - '0';
printf("%s ", i? tens_multiple[i]: "");
++num;
if (*num != '0')
printf("%s ", single_digits[*num - '0']);
}
}
++num;
}
}
call the function as convert_to_words("9923"); in the main function.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.