Check Writing

I'm working on a program that translates numbers into words (English). I have majority of the program done, but I am stuck on the main part. Would anyone give me a hint?

I am using classes and static members and the way I set it up seems to be correct.
The following is my code w/ output. Thank you for your time.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Chapter 11 - Assignment 1, Check Writing
// This program creates a Number Translation class 
// That can be used for writing checks.
#include <iostream>
#include <string>
using namespace std;

class Numbers
{	

private:
	int number;
	
	static string one[];
	static string ten[];
	static string hundred[];
	static string thousand[];

public:
	Numbers();
	Numbers(int num){setNum(num);};
	void setNum(int num){number = num;};
	void print();

};

string Numbers::one[] =  {"","one", "two", "three", "four", "five", "six","seven", "eight", "nine"} ;
string Numbers::ten[] = {"ten","twenty","thirty","fourty","fifty", "sixty", "seventy", "eighty", "ninety"};
string Numbers::hundred[] = {"hundred"};
string Numbers::thousand[] = {"thousand"};

void Numbers::print()
{
	
      /*It may not suppose to be a if statement...
        I've thought about a for loop with if statement, but
        I wouldn't know how to set it up so that it would find
        the words for the number(s) by pulling it out from any of
        the static functions repeatedly if needed */
	if (number < 0)
	{number = -number;}
	else if (number >= 1 && number < 10)
	{cout <<one[number]<<" dollar";}//This is correct
	if (number >= 10 && number < 100)//Here is where it tends to mess up.
	{cout << ten[number];}
	
}





int main()
{
    // Tell user what the program does and get input
    cout << "This program translates whole 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: "; 
    int  number;
    cin >> number;

    // 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: ";
        cin >> number;
    }  

    // We are done       
    return 0;
}


Here is the output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/*
This program translates whole dollar amounts into words for the purpose
of writing checks.
Entering a negative terminates the program.
Enter an amount for be translated into words: 1
one dollar
Enter another number: 2
two dollar
Enter another number: 3
three dollar
Enter another number: 10 
one     <--(what!?)
Enter another number: 
*/

Last edited on
You are also missing "eleven", "twelve", "thirteen", etc.

See http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=39627&lngWId=1. It is a very old piece done in Visual Basic by me. Far from perfect but it does have a compact algorithm, I would say.
Because ten[10] is out of bounds!!!
you want ten[number / 10]
you also need to start the ten array with "" too (or something like that) because 10 / 10 == 1 and ten[1] == "twenty", whoops!
Last edited on
Thanks guys, I'll check it out. :)
Topic archived. No new replies allowed.