Question
I have a problem I have to solve with c++ and I need help please. The question is : Computers are frequently employed in check-writing
I have a problem I have to solve with c++ and I need help please. The question is : Computers are frequently employed in check-writing systems such as payroll and accounts payable applications. Write a program that inputs a numeric check amount (an integer between 0 and 4 billion) and writes the word equivalent of the amount. For example, the amount 221 should be written as: TWO HUNDRED TWENTY ONE DOLLARS. 1005021 should be written as: ONE MILLION FIVE THOUSAND and TWENTY ONE DOLLARS. My code doesn't work 100%. The second number section doesn't produce anything. For example, 25 comes out as five. Also anything past 1000 doesn't work either. Can some one help? Here is my code:
#include "stdafx.h"
#include
#include
using namespace std;
int reverse(int v, int lim)
{
if (lim == 1)
return v;
else
return (((v % 10)*pow(10, lim - 1)) + reverse(v / 10, lim - 1));
}
void print_c(int digit, int l, int r = 12)
{
if (l != 2)
{
switch (digit)
{
case 1: cout << "One";
break;
case 2: cout << "Two";
break;
case 3: cout << "Three";
break;
case 4: cout << "Four";
break;
case 5: cout << "Five";
break;
case 6: cout << "Six";
break;
case 7: cout << "Seven";
break;
case 8: cout << "Eight";
break;
case 9: cout << "Nine";
break;
}
}
else if (l == 2)
{
switch (digit)
{
case 1: switch (r)
{
case 10: cout << "Ten";
break;
case 11: cout << "Eleven";
break;
case 12: cout << "Twelve";
break;
case 13: cout << "Thirteen";
break;
case 14: cout << "Forteen";
break;
case 15: cout << "Fifteen";
break;
case 16: cout << "Sixteen";
break;
case 17: cout << "Seventeen";
break;
case 18: cout << "Eighteen";
break;
case 19: cout << "Nineteen";
break;
}
break;
case 20: cout << "Twenty";
break;
case 30: cout << "Thirty";
break;
case 40: cout << "Forty";
break;
case 50: cout << "Fifty";
break;
case 60: cout << "Sixty";
break;
case 70: cout << "Seventy";
break;
case 80: cout << "Eighty";
break;
case 90: cout << "Ninty";
break;
case 0: cout << " ";
break;
}
}
}
int main()
{
int num, temp, length = 0, result, n, m = 0;
cout << " Enter the number" << endl;
cin >> num;
temp = num;
for (;num > 0; num /= 10)
{
length++;
}
result = reverse(temp, length);
while (result)
{
n = result % 10;
m = m * 10 + n;
result /= 10;
if (length == 1)
{
print_c(n, length);
}
else if (length == 2)
{
if (n == 1)
{
print_c(n, length, result);
break;
}
else
{
print_c(n, length);
length--;
}
}
else if (length == 3)
{
print_c(n, length);
length--;
if (n != 0)
{
cout << "hundred";
}
}
else if (length == 4)
{
print_c(n, length);
length--;
cout << "Thousand";
}
else if (length == 7)
{
print_c(n, length);
length--;
cout << "Million";
}
else if (length == 13)
{
print_c(n, length);
length--;
cout << "Billion";
}
}
system("pause");
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started