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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
|
#include <iostream>
#include <iomanip>
#include <cctype>
using namespace std;
const int SIZE = 81; // array size constants
const int NUM_ONES = 10;
const int NUM_TEENS = 20;
const int NUM_TENS = 10;
// onesPlc[] contains words for the numbers in the "ones" place
char onesPlc[NUM_ONES][SIZE] = { "", "One", "Two", "Three", "Four",
"Five", "Six", "Seven", "Eight", "Nine" };
// teensW[] contains words for the teen numbers
char teensW[NUM_TEENS][SIZE] = { "", "", "", "", "", "", "", "", "",
"", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen",
"Sixteen", "Seventeen", "Eighteen", "Nineteen" };
// tensPlc[] contains the words for numbers in the tens place
char tensPlc[NUM_TENS][SIZE] = { "", "Ten", "Twenty", "Thirty",
"Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety" };
// inEnglish() prints an English word version of the value in num
void inEnglish(double num);
int main()
{
double value; // amount of the check
char date[SIZE]; // check date
char payee[SIZE]; // name of payee
char again = 'y'; // loop control variable
do {
cout << "\nEnter the date: ";
cin.getline(date, SIZE); // read date
cout << "\nEnter name of the payee: ";
cin.getline(payee, SIZE); // read payee
cout << "\nEnter check amount: $";
cin >> value; // read check amount
if(value > 10000.00 || value < 0)
{
cout << "*INVALID AMOUNT* Value cannot be more than 10,000 or negative!" << endl;
cout << "\nEnter check amount: $";
cin >> value;
}
cout << fixed << showpoint << setprecision(2); // float-pt format
cout << "--------------------------------------------------------------------\n";
cout << setw(50) << right;
cout << "Date: " << date << "\n\n"; // display date
cout << "\tPay to the order of: " << payee << "\t\t\t";
cout << "$" << value << endl;
inEnglish(value); // show English word version of amount
cout << "--------------------------------------------------------------------\n";
cout << "\n Another check? (Y/N): ";
cin >> again;
cin.get(); // get and ignore
} while (toupper(again) == 'Y');
return 0;
}
void inEnglish(double num)
{
// declare int variables for thousands part, hundreds part, tens part, teens part
// and cents
int thousandspart;
int hundredspart;
int tenspart;
int teenspart;
int onespart;
int cents;
// declare and initialize char array to null
char money[SIZE] = {0};
//
// if num is exactly 10000.00 print words and return
if(num == 10000.00)
cout << "Ten Thousand Dollars and 0 cents" << endl;
return;
// extract thousands part (if it exists) from num and put words in char array
if(num > 999.99){
// extract hundreds part (if it exists) from num
thousandspart = num / 1000;
num = num - (thousandspart * 100);
strcat(money , onesPlc[thousandspart]);
strcat(money , "thousand");
}
if(num > 99.99){
hundredspart = num / 100;
num = num - (hundredspart * 100);
// if tens part is greater than one
if(tenspart > 1){
tenspart = num / 10;
num -= onespart;
}
// put words in char array
strcat(money , onesPlc[thousandspart]);
strcat(money , "thousand");
// get ones part
onespart = num / 1;
// if ones part is greater than zero put words in char array
if(onespart > 0)
{
strcat(money , onesPlc[onespart]);
strcat(money , "dollars");
}
else if(tenspart == 1){
teenspart = num / 10;
strcat(money , teensW[teenspart]);}
cents = (num + 0.005) * 100;
strcat(money , "cents");
cout << money[SIZE];
// else if tens part is equal to one
// get teens part and put in char array
// print the char array and any cents
} // end inEnglish()
}
|