My question is are there various ways that I can approach this program. I.e. do I have to use switch statements?
/*
NumToTxt
Creates the appropriate word form of any positive integer up to 999999
*/
#include <iostream>
using namespace std;
//represents the largest array size for the user entered number
const int MAXNUMARRAY = 6;
/*represents the largest number that can be entered + 1.
Used to calculate the first number used to truncate the user
entered number and to display an error message to the user
that tells the user the largest number that the program will
accept.
*/
const int BIGGESTNUMBER = 1000000;
/*function used to convert user entered number
to an array that stores each digit into a separate index.
@long int: the number that needs to be put in the array
@int[]: the integer to store the digits in
*/
void integerToArray(long int, int[]);
/*function used to convert numbers that are even multiples of ten
and greater than 19 into words.
@int: the number to be converted into words.
*/
void tensToWords(int);
/*function used to convert numbers 1-19 to words.
@int: the number to be converted into words.
@int&: used to calculate the place of the number:
i.e. ones, tens, hundreds, etc.
*/
void teensToWords(int, int&);
int main()
{
//the number that the user enters to convert into words
long int number = 0;
//integer array that stores the user's number with each
//digit in its own index.
int numberArray[MAXNUMARRAY];
//number sent to functions to display the word equivalent of the number
int teens;
//counter used to determine how many times the program has run
int i = 0;
//variable used to determine the "place" of the number to convert.
//(ones, tens, hundreds, etc.)
int count;
cout<<"Enter numbers in their decimal form.\n"<<
"To stop, enter a negative value.\n";
cout<<"Number: ";
cin>>number;
//the program will not run if a negative number is entered
while (number >= 0)
{
//check to see if the number is a zero
if (number == 0){
//display number and get a new number
cout<<"zero\n";
cout<<"Number: ";
cin>>number;
//set the counter so that the program will not ask for a number again.
i = 0;
}
//if the program has already run once, ask for another number
if (i != 0){
cout<<"\nNumber: ";
cin>>number;
}
//check to see if the number is bigger than the program can handle
while(number >=BIGGESTNUMBER){
cout<<"NUMBER ENTERED IS TOO LARGE!\n"<<
"Please enter a number less than "<<BIGGESTNUMBER<<
"\a\n";
cout<<"Number: ";
cin>>number;
}
//if the number is negative, terminate the program
if (number < 0)
return 0;
integerToArray(number, numberArray);
//this series of if/else statements allows the
//program to ignore leading zeros.
if (number < 10)
count = 5;
else if (number < 100)
count = 4;
else if (number < 1000)
count = 3;
else if (number < 10000)
count = 2;
else if (number < 100000)
count = 1;
else if (number < 1000000)
count = 0;
do
{
//switch statement sends the correct digit to the correct function
//to determine its "name" in words.
switch(count)
{
case 0:teens = (numberArray[count]);
teensToWords(teens, count);
printf("hundred ");
break;
//since numbers in this place can be represented as a "teen"
//the program needs to combine two numbers to send to the function.
case 1:teens = ((numberArray[count] * 10) + numberArray[count + 1]);
teensToWords(teens, count);
break;
case 2:teens = numberArray[count];
teensToWords(teens, count);
printf("thousand ");
break;
case 3:teens = (numberArray[count]);
teensToWords(teens, count);
//allows the program to ignore a 0 in the hundreds place.
//prevents 12000 from being displayed as "two thousand hundred one."
if (numberArray[count - 1] > 0)
printf("hundred ");
break;
case 4:teens = ((numberArray[count] * 10) + numberArray[count + 1]);
teensToWords(teens, count);
break;
case 5:teens = numberArray[count];
//prints a '-' character to numbers like "forty-five"
if (numberArray[count - 1] != 0)
printf("\b-");
teensToWords(teens, count);
break;
}
}
//end the loop when the count reaches the maximum number of indeces in
//the number array
while (count < MAXNUMARRAY);
//increase the number of times the program has run by one
i++;
}
return 0;
}
void integerToArray(long int numberToConvert, int integerArray[])
{
//get the first number to divide by to properly truncate the number.
//This will always be BIGGESTNUMBER divided by 0.
int divideBy = BIGGESTNUMBER / 10;
//this calculates where to store the current digit in the array.
//can never be greater than the max allowed digits.
int count = MAXNUMARRAY;
//prevents a division by 0
while (divideBy > 0){
//take the first digit off of the number and store it into the first
//open index in the array.
integerArray[MAXNUMARRAY - count]= numberToConvert / divideBy;
//truncate the number by one digit
numberToConvert = numberToConvert % divideBy;
//reduce the number used to truncate
divideBy = divideBy / 10;
//recalculate the index to store the next digit
count--;
}
return;
}
void teensToWords(int teens, int& count)
{
//if the number is twenty or over, it can be represented by a "-ty" number
if (teens >= 20)
{
//recalculate the digit to send
tensToWords(teens/10);
//increase the number of "places" to check next.
//i.e. ensures that the program reads the 1 in 21.
count = count + 1;
return;
}
//if the number is a "teen" or less
else if (teens <= 19)
{
switch (teens){
//ignore zeros
case 0: break;
//print the correct number
case 1: printf("one "); break;
case 2: printf("two "); break;
case 3: printf("three "); break;
case 4: printf("four "); break;
case 5: printf("five "); break;
case 6: printf("six "); break;
case 7: printf("seven "); break;
case 8: printf("eight "); break;
case 9: printf("nine "); break;
case 10: printf("ten "); break;
case 11: printf("eleven "); break;
case 12: printf("twelve "); break;
case 13: printf("thirteen "); break;
case 14: printf("fourteen "); break;
case 15: printf("fifteen "); break;
case 16: printf("sixteen "); break;
case 17: printf("seventeen "); break;
case 18: printf("eighteen "); break;
case 19: printf("nineteen "); break;
default: printf("Illegal call to TeensToWords\a\n");
}
//this allows the word "thousand" to print in word like "thirteen thousand"
if (count == 1){
printf("thousand ");
//also, numbers in index one of the array can be represented as two digits
//taking on a single word. i.e. 1 3 is taken as thirteen.
//this updates the counter to move the position and skip the next digit
//since it has been accounted for
count = count + 2;
}
//the same is true for numbers in index for as in index 1
else if (count == 4)
count = count + 2;
//if not in these two positions, the number is a single digit number (one, two, etc.)
else
//update the counter by one "place"
count = count + 1;
}
return;
}
void tensToWords(int tens)
{
switch(tens)
{
case 0: break;
case 2: printf("twenty "); break;
case 3: printf("thirty "); break;
case 4: printf("forty "); break;
case 5: printf("fifty "); break;
case 6: printf("sixty "); break;
case 7: printf("seventy "); break;
case 8: printf("eighty "); break;
case 9: printf("ninety "); break;
default: printf("Illegal call to TensToWords!\a\n");
}