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 <cstdlib>
#include <string>
#include <iomanip>
using namespace std;
//****************************************************************************
// Function Prototypes follow
//****************************************************************************
void printArrayContents(string, int SIZE);
//****************************************************************************
// Main Line
//****************************************************************************
int main()
{
char *ones[]={ " ",
"one",
"two",
"three",
"four",
"five",
"six",
"seven",
"eight",
"nine"};
char *teens[]={ "ten",
"eleven",
"twelve",
"thirteen",
"fourteen",
"fifteen",
"sixteen",
"seventeen",
"eighteen",
"nineteen"};
char *tens[]={ " ",
" ",
"twenty",
"thirty",
"forty",
"fifty",
"sixty",
"seventy",
"eighty",
"ninety"};
char hundred[]={ " hundred "};
string date;
string name;
double amount;
char and[] = " and ";
char value_str[50] = "";
int value = 0;
int digits[] = {0,0,0};
int i = 0;
// Get The Date
cout << "Enter the date: ";
cin >> date;
cin.ignore();
// Get name
cout << "Enter your name: ";
getline(cin,name);
// Get amount
cout << "Enter the amount: ";
cin >> amount;
// Validate amount
if (amount < 0)
{
cout << "Invalid amount: amount cannot be negative or over 1000. Please re-enter: ";
cin >> amount;
}
else if (amount >= 1000)
{
cout << "Invalid amount: amount cannot be negative or over 1000. Please re-enter: ";
cin >> amount;
}
printf("Enter the amount: ");
scanf("%d",&value);
if(value>=1000)
{
cout << "Invalid amount: amount cannot be negative or over 1000. Please re-enter: ";
cin >> value;
}
else if(value<1)
{
cout << "Invalid amount: amount cannot be negative or over 1000. Please re-enter: ";
cin >> value;
}
while(value>0)
{
digits[i++] = value%10;
value /= 10;
}
if(digits[2] > 0)
{
strcat(strcat(value_str,ones[digits[2]]), hundred);
if(digits[1] >0 || digits[0] > 0)
strcat(value_str, and);
}
if(digits[1] > 0)
{
if(digits[1] == 1)
strcat(value_str,teens[digits[0]]);
else
{
strcat(value_str,tens[digits[1]]);
if(digits[0] > 0)
strcat(strcat(value_str, " "), ones[digits[0]]);
}
}
else
if(digits[0] > 0)
strcat(value_str, ones[digits[0]]);
// Show Final Result
{
cout << setprecision(2) << fixed << showpoint;
cout << "========================================" <<endl <<endl;
cout << "\n Date: " << date;
cout << "\nPay to the order of: " << name; cout << " $" << amount;
cout << "\n " << cout << printf("\n%s\n", value_str) << "cents";
}
return 0;
}
|