Write a program which asks for the following information: bullet the date bullet
ID: 3802530 • Letter: W
Question
Write a program which asks for the following information: bullet the date bullet the name of the person who the check is for bullet the amount of the check Using this information you should display a check formatted exactly like the example. The cash amount should be formatted to two decimal places. Input Validation: Do not accept values greater than 5,000. Do not accept negative amounts. A check for only a cents amount should be valid. Sample Output: What is the amount of the check?: 1920.85 Enter the date of the check: 1/21/2015 Who is the check for?: John Smith Date: 1/21/2015 Pay to the Order of: John Smith $1920.85 One thousand nine hundred twenty and 85 centsExplanation / Answer
#include <stdio.h>
#include <string.h>
void convert_to_words(char *num, char *fraction)
{
int len = strlen(num);
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 result[200] = {''};
if (len == 4) {
// get thousand place
if (num[0] != '0') {
strcat (result, single_digits[num[0] - 48]);
strcat (result, " thousand ");
}
num++;
len--;
}
if (len == 3) {
// get hundred place
if (num[0] != '0') {
strcat (result, single_digits[num[0] - 48]);
strcat (result, " hundred ");
}
num++;
len--;
}
if (len == 2) {
// if num >= 20 then this case
if (num[0] > '1') {
strcat (result, tens_multiple[num[0] - 48]);
strcat (result, " ");
} else if (num[0] == '1') { // if 2nd place is < 20 then only one word required
strcat (result, two_digits[num[1] - 48]);
num++;
len --;
}
num++;
len--;
}
if (len == 1) {
// get ones place
if (num[0] != '0')
strcat (result, single_digits[num[0] - 48]);
}
if (strlen(result))strcat (result, " and ");
printf("%s%s cents ", result, fraction);
}
int strtoint(char *input) {
int res = 0;
int i;
for (i = 0; i < strlen(input); i++) {
res = res * 10 + (input[i] - 48);
}
return res;
}
int is_valid(char *input, char *fraction) {
if (strlen(input) > 0 && input[0] == '-') return 0; // case of negative
int num = strtoint(input); // convert string to int
//if num > 5000 or number == 5000 anf fraction part is not 0 invalid
if (num > 5000 || num == 5000 && strtoint(fraction) > 0) return 0;
return 1;
}
void remove_trailing(char *input) {
int len = strlen(input) - 1;
while (len >= 0 && input[len] == ' ') input[len--] = '';
}
int main() {
int i, len;
char *fraction = NULL;
char input[20];
char name[30];
char date[12];
printf("What is the amount of check?:");
fgets(input,20, stdin);
remove_trailing(input);
printf("Enter the date of the check:");
fgets(date,12, stdin);
remove_trailing(date);
printf("Who is the check for?:");
fgets(name,30, stdin);
remove_trailing(name);
len = strlen(input);
for (i = 0; i < len; i++) {
if (input[i] == '.') {
fraction = input + i + 1;
input[i] = '';
break;
}
}
if (!fraction) fraction = "0";
if (!is_valid(input, fraction)) {
printf("Enter amount in between 0.00 to 5000.00 cents");
return 0;
}
printf(" Date: %s ",date);
printf("Pay the order of: %s %s.%s ", name, input, fraction);
convert_to_words(input, fraction);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.