1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

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

  1. Apr 13, 2017 #1
    1. The problem statement, all variables and given/known data

    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. Relevant 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 (C):

    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 (C):
    #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: Apr 13, 2017
  2. jcsd
  3. Apr 13, 2017 #2

    phyzguy

    User Avatar
    Science Advisor

    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.
     
  4. Apr 13, 2017 #3
    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.
     
  5. Apr 13, 2017 #4

    Mark44

    Staff: Mentor

    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.
     
  6. Apr 14, 2017 #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.
     
  7. Apr 14, 2017 #6

    Mark44

    Staff: Mentor

    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.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: Check writing program (type double variable value converted into English words)
  1. Write a SIC program (Replies: 3)

Loading...