- #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)
Here's the full program if you need it:
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: