Classes

Design a class called Numbers that can be used to translate whole dollar amounts in the range 0 through 9999 into an English description of the number.

Here's what I have:
main.cpp:
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <conio.h>
#include <iostream>
#include <string>
#include "Numbers.h"

using namespace std;

int main() 
{
	int n;

	cout << "please enter a number between  and 9999";
	cin >> n;

	while (n!=0)
	{
		cout << "the number is:" << n << " ";
		Numbers num(n);

		cout << "enter a number between 0 and  9999";
		cin >> n;

	}// end of while loop
	_getch();
	return 0;

}

Numbers.h

#include <iostream>
#include <string>
#include <math.h>
#ifndef NUMBERS_H
#define NUMBERS_H


using namespace std;

class Numbers
{
private:
	int num;
public:
	Numbers (int x)
	{
		num = x;
	}
	void print();

};
void Numbers::print()
{
	int n;
	int num;


	string lessThanTwenty[20] = { " ", "One", "Two", "Three", "Four", "Five" "Six", "Seven", "Eight",
		"Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen" };

	string tens[] = { "zero", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninty" };

	string hundreds = { " ", "Hundred" };

	string thousands = { " ", "Thousand" };

	if (num < 0)
		cout << "Number is a negative number. Please enter a number between 0-9999.";
	num = abs(num);
	n = num / 1000;

	if (n>0)
		cout << lessThanTwenty[n] << thousands;
	num %= 1000;
	n = num / 100;

	if (n > 0)
		cout << lessThanTwenty[n] << hundreds;
	num %= 100;
	if (num >= 20)
	{
		n = num / 10;
		if (n > 0)
			cout << tens[n] << " ";
	}
	else if (num >= 10)
	{
		cout << lessThanTwenty[num] << " ";

		return;
	}
	num %= 10;
	if (num > 0)
		cout << lessThanTwenty[num];
	cout << " ";
}//End of class

#endif 
Last edited on
So, do you have any questions?

It looks like that void Numbers::print() is defined in the header file (Numbers.h). Don't do that. Instead put it in it's own cpp (like Numbers.cpp)
Topic archived. No new replies allowed.