Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please write in C++ Write a program that displays a simulated paycheck. The prog

ID: 3824116 • Letter: P

Question

Please write in C++

Write a program that displays a simulated paycheck. The program should ask the user to enter the date, the payee's name, and the amount of the check (up to $10,000). It should then display a simulated check with the dollar amount, both in digits and spelled out. For example: Pay to the Order of: John Doe One thousand nine hundred twenty and 88/100 Be sure to format the numeric value of the check in fixed point notion with two decimal places of precision. Be sure the decimal places are always displayed, even when the number is a whole number Input Validation: Do not accept negative dollar amounts, or amounts over $10,000.

Explanation / Answer

#include <stdio.h>
#include <string.h>
#include <stdlib.h>


/* A function that prints given number in words */
void convert_to_words(char *num)
{
int len = strlen(num); // Get number of digits in given number

/* Base cases */
if (len == 0) {
fprintf(stderr, "empty string ");
return;
}
if (len > 4) {
fprintf(stderr, "Length more than 4 is not supported ");
return;
}

/* The first string is not used, it is to make array indexing simple */
char *single_digits[] = { "zero", "one", "two", "three", "four",
"five", "six", "seven", "eight", "nine"};

/* The first string is not used, it is to make array indexing simple */
char *two_digits[] = {"", "ten", "eleven", "twelve", "thirteen", "fourteen",
"fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};

/* The first two string are not used, they are to make array indexing simple*/
char *tens_multiple[] = {"", "", "twenty", "thirty", "forty", "fifty",
"sixty", "seventy", "eighty", "ninety"};

char *tens_power[] = {"hundred", "thousand"};

/* Used for debugging purpose only */
printf(" %s: ", num);

/* For single digit number */
if (len == 1) {
printf("%s ", single_digits[*num - '0']);
return;
}

/* Iterate while num is not '' */
while (*num != '') {

/* Code path for first 2 digits */
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;
}

/* Code path for last 2 digits */
else {
/* Need to explicitly handle 10-19. Sum of the two digits is
used as index of "two_digits" array of strings */
if (*num == '1') {
int sum = *num - '0' + *(num + 1)- '0';
printf("%s ", two_digits[sum]);
return;
}

/* Need to explicitely handle 20 */
else if (*num == '2' && *(num + 1) == '0') {
printf("twenty ");
return;
}

/* Rest of the two digit numbers i.e., 21 to 99 */
else {
int i = *num - '0';
printf("%s ", i? tens_multiple[i]: "");
++num;
if (*num != '0')
printf("%s ", single_digits[*num - '0']);
}
}
++num;
}
}

/* Driver program to test above function */
int main(void)
{
   int i;
   char Name[50];
   char Date[9];
   float Amount;

   while(1)
   {
       printf("Enter Name: ")
       scanf("%s",&Name);
       printf("Enter Date in dd/mm/yyyy ")
       scanf("%s",&Date)
       printf("Enter Amount (less than 10,000 $) ")
       scanf("%f",&Amount);
       if (Amount <= 10,000)
       {
           int temp = static_cast<int>(Amount);
           float decimal;
           decimal=Amount-temp;
           if (decimal > 0)
           {
               decimal=decimal*100;
               printf("%s
               Pay to the Order of : %s %f,/n
               convert_to_words(temp) %d/100 dollars",&Date,&Name,&Amount,&decimal)
           }
           if (decimal == 0)
           {
              
               printf("%s
               Pay to the Order of : %s %f,/n
               convert_to_words(temp) dollars",&Date,&Name,&Amount)
           }
              
       }
       else
       {
           cout << "Invalid Amount. Please Try Again"
       }
      
      

   }
}