Check writing program (type double variable value converted into English words)

In summary, the code provided involves using a class to convert a double number into English words, but there seems to be an error in the conversion to cents. The issue may be related to using fmod to convert from double to int, which is causing discrepancies in values. The error is likely somewhere in the last 15 lines of code.
  • #1
Of Mike and Men
54
3
1. Homework Statement

First, if you want to skip my explanation, I believe the error is somewhere in the last 15 lines of code or so.

Heres some background on test cases:
Use stating member variables to use a class that helps you convert a double number into english words. For example, if a user inputs 10.99 a print function outputs "ten dollars and ninety nine cents"

My problem is in the conversion to cents. For some reason my conversion of a double into an int (to use the modulo) is cutting down on certain values. For example, if I use int(fmod(13, 10)) it will return 2. But this is only on SOME values. If I do int(fmod(14,10)) it will return 4. I'm not sure why this is happening.

HOWEVER, this is only happening under the CENTS portion of the code. When I'm converting the value before the cents it works fine. I.E. I can input 113.13 and it will out put "one hundred thirteen and twelve cents."

2. Homework Equations
Use only one variable to store the dollars and cents. I.E. You may not have one for whole dollar amounts and another for cents. It must be in one variable called number.

3. The Attempt at a Solution

This is where the error is located in the code (full code located below if needed)
Code:
string Numbers::ones[] = {"","one ","two ","three ","four ","five ","six ","seven ","eight ","nine " };
string Numbers::teens[] = {"", "ten ","eleven ","twelve ","thirteen ","fourteen ","fifteen ","sixteen ","seventeen ","eighteen ","nineteen " };
string Numbers::tens[] = {"", "", "twenty ","thirty ","forty ","fifty ","sixty ","seventy ","eighty ","ninety " };
string Numbers::hundreds[] = {"", "one hundred ", "two hundred ", "three hundred ", "four hundred ", "five hundred ", "six hundred ", "seven hundred ", "eight hundred ", "nine hundred " };
string Numbers::thousands[] = {"", "one thousand ", "two thousand ", "three thousand ", "four thousand ", "five thousand ", "six thousand ", "seven thousand ", "eight thousand ", "nine thousand " };

void Numbers::print() {
    // The first part of print will get the number in dollars up until the decimal place
    if (number < 0){
        number = -number;
    }
    if (number == 0) {
        cout << "zero ";
    }
    if (number >= 1000) {
        cout << thousands[int(number) / 1000];
        number = fmod(number, 1000);
    }
    if (number >= 100) {
        cout << hundreds[int(number) / 100];
        number = fmod(number, 100);
    }
    if (number >= 20) {
        cout << tens[(int(number) / 10)];
        number = fmod(number, 10);
    }
    if (number >= 10 && number <= 19) {
        cout << teens[int(fmod(number, 10)) + 1];
    }
    if (number > 0 && number < 10){
        cout << ones[int(number)];
    }
 
    cout << "dollars"; // the program will end here unless you entered cents// I THINK THE ERROR IS SOMEWHERE BELOW
    number = 100 * fmod(number, int(number)); // if someone entered 112.14 this will make number = 14 (this works fine)
    if (number > 0) {
        cout << " and ";
        if (number >= 20) { x
            cout << tens[(int(number) / 10)];
            number = fmod(number, 10);
        }
        if (number >= 10 && number <= 19) {
            cout << teens[int(fmod(number, 10)) + 1];
        }
        if (number > 0 && number < 10){
            cout << ones[int(number)];
        }

        cout << "cents.";
    }
}

Here's the full program if you need it:
Code:
#include <iostream>
#include <string>
#include <math.h> // required to get cents
using namespace std;

class Numbers{  
private:
    double number;
    static string ones[];
    static string tens[];
    static string teens[];
    static string hundreds[];
    static string thousands[];  
public:
    Numbers();
    Numbers(double num){setNum(num);};
    void setNum(double num){number = num;};
    void print();
};

string Numbers::ones[] = {"","one ","two ","three ","four ","five ","six ","seven ","eight ","nine " };
string Numbers::teens[] = {"", "ten ","eleven ","twelve ","thirteen ","fourteen ","fifteen ","sixteen ","seventeen ","eighteen ","nineteen " };
string Numbers::tens[] = {"", "", "twenty ","thirty ","forty ","fifty ","sixty ","seventy ","eighty ","ninety " };
string Numbers::hundreds[] = {"", "one hundred ", "two hundred ", "three hundred ", "four hundred ", "five hundred ", "six hundred ", "seven hundred ", "eight hundred ", "nine hundred " };
string Numbers::thousands[] = {"", "one thousand ", "two thousand ", "three thousand ", "four thousand ", "five thousand ", "six thousand ", "seven thousand ", "eight thousand ", "nine thousand " };

void Numbers::print() {
    // The first part of print will get the number in dollars up until the decimal place
    if (number < 0){
        number = -number;
    }
    if (number == 0) {
        cout << "zero ";
    }
    if (number >= 1000) {
        cout << thousands[int(number) / 1000];
        number = fmod(number, 1000);
    }
    if (number >= 100) {
        cout << hundreds[int(number) / 100];
        number = fmod(number, 100);
    }
    if (number >= 20) {
        cout << tens[(int(number) / 10)];
        number = fmod(number, 10);
    }
    if (number >= 10 && number <= 19) {
        cout << teens[int(fmod(number, 10)) + 1];
    }
    if (number > 0 && number < 10){
        cout << ones[int(number)];
    }  
  
    cout << "dollars"; // the program will end here unless you entered cents
    number = 100 * fmod(number, int(number)); // get the cents and multiply by 100 so we can use our arrays  
    // Now the program will convert your cents using the arrays we already defined for the dollar amounts
    if (number > 0) {
        cout << " and ";
        if (number >= 20) {
            cout << tens[(int(number) / 10)];
            number = fmod(number, 10);
        }
        if (number >= 10 && number <= 19) {
            cout << teens[int(fmod(number, 10)) + 1];
        }
        if (number > 0 && number < 10){
            cout << ones[int(number)];
        }

        cout << "cents.";
    }
}
  
int main() {
    // Tell user what the program does and get input
    cout << "This program translates dollar amounts into words ";
    cout << "for the purpose of writing checks.";
    cout << "\nEntering a negative terminates the program.";
    cout << "\nEnter an amount for be translated into words: ";
    double  number;
    do {
            cin >> number;
            if (number > 9999) { // Input validation
                cout << endl << "\tERROR: Enter a value <= 9999" << endl;
            }
        } while (number > 9999);

    // Keep translating numbers until the user enters a negative value
    while (number >= 0)
    {
        // Create a Numbers object
        Numbers n(number);
        // Print the English description
         n.print();
        // Get another number
        cout << "\nEnter another number: ";
        do {
            cin >> number;
            if (number > 9999) { //input validation
                cout << endl << "\tERROR: Enter a value <= 9999" << endl;
            }
        } while (number > 9999);
    }
  
    return 0;
}
 
Last edited:
Physics news on Phys.org
  • #2
The int function in c++ simply truncates the number. Due to numerical error, this can cause the problem you are seeing. So if fmod(13,10) returns 2.99999999999999..., int of this will return 2. To fix this, use the function round instead of int, i.e. round(fmod(13,10)). This will return the nearest integer, not simply truncate.
 
  • #3
phyzguy said:
The int function in c++ simply truncates the number. Due to numerical error, this can cause the problem you are seeing. So if fmod(13,10) returns 2.99999999999999..., int of this will return 2. To fix this, use the function round instead of int, i.e. round(fmod(13,10)). This will return the nearest integer, not simply truncate.
This fixed it for certain cases, but causes the program to lock on other numbers, and also didn't work for every number. When I did 80 something it rounded down to 70. The problem is I still need to convert it to an integer number for the index of the array.

It's really irritating because it seems to work fine for the whole numbers, but once it gets to the cents it screws up. I've not had any errors with the ones through thousands place. It's only the cents.
 
  • #4
Of Mike and Men said:
This fixed it for certain cases, but causes the program to lock on other numbers
It's not really locking up -- that occurs when the computer stops accepting any input. Your program is just producing incorrect results due to the way floats and doubles are stored. For example, when you enter 113.13 and take the floating point modulo 100, you actually get something like 13.129999999995. If you strip off the fractional part, multiply by 100.0, and then use round(), you'll have the whole number of cents.
 
  • #5
Figured it out. Thanks. Your method worked. I also took off the fraction a better way (substraction). For some reason on my laptop the code kept modifying to a previous version of the file. I went over to my Mac and it ran fine. So I think I was just having an issue on my laptop for some weird reason. Thanks all.
 
  • #6
Of Mike and Men said:
Figured it out. Thanks. Your method worked. I also took off the fraction a better way (substraction). For some reason on my laptop the code kept modifying to a previous version of the file. I went over to my Mac and it ran fine. So I think I was just having an issue on my laptop for some weird reason. Thanks all.
I was helping a student yesterday who was having a similar problem, using Visual Studio. We would fix problems in his code, recompile, and still get the same error. It turns out his VS project included one file, but the file he was working on was a different one that wasn't part of his VS project. You might have been having the same problem.
 

Related to Check writing program (type double variable value converted into English words)

What is a check writing program and why is it important?

A check writing program is a computer software that converts numerical values, typically in the form of a double variable, into written words. This is important because it ensures accuracy in check writing and helps prevent mistakes and fraud.

How does a check writing program work?

A check writing program uses a conversion algorithm to translate the numerical value into a written format. The program typically breaks down the value into its individual digits and then converts each digit into its corresponding word. It then combines these words to create the written representation of the value.

What are the benefits of using a check writing program?

Using a check writing program can save time and reduce the chances of human error when writing checks. It also provides a clear and easy-to-read written representation of the value, making it easier for both the writer and receiver to understand.

Can a check writing program handle different currencies?

Yes, many check writing programs have the capability to handle different currencies. Some may even have options to switch between different languages for the written words.

Are there any limitations to using a check writing program?

One potential limitation is that the program may not be able to recognize handwritten numbers or symbols on a check. It is also important to double check the written words generated by the program for accuracy before finalizing the check.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
19
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
Replies
10
Views
973
  • Engineering and Comp Sci Homework Help
Replies
12
Views
2K
Back
Top