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;
}
|