Thank you for the help again! you must be getting bored of looking over this problem. I have sorted this problem out and I am printing out the correct results. However I am having difficulty appending all of the three chunks together including there magnitude; for example my result if I put an input of 123456789 is giving me;
Seven Hundred and Eighty Nine
Four Hundred and Fifty Six
One Hundred and Twenty Three
below is what I have done with my code;
Code:
#include <iostream> //including header files
#include <string>
using namespace std;
//string array declared
string units [10] = {"One","Two","Three", "Four", "Five", "Six","Seven","Eight", "Nine"};
string teens [10] = {"Ten", "Eleven", "Twelve", "Thirteen","Fourteen", "Fifteen", "Sixteen", "Seventeen","Eighteen", "Nineteen"};
string tens [10] = {"Twenty", "Thirty", "Fourty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};
//fcn defs
void printMillions(int unsigned i);
void printHundreds(int unsigned i);
void printThousands(int unsigned i);
void printTens(int unsigned i);
void printTeens(int unsigned i);
void printUnits(int unsigned i);
int main() //main body of fcn
{
int num; //declaring integers
int hundreds;
int thousands;
int millions;
cout << "Enter an a number between -2 million and 2 million " << endl; //prompts user for i/p and stores it
cin >> num;
if ((num < -200000000) || (num > 200000000)) //exits program if not within these limits
{
return 0;
}
else
{
hundreds = (num % 1000); //work out how many hundreds
printHundreds(hundreds);
thousands = (num/1000);
thousands = (thousands%1000); //work out how many thousdands
printThousands(thousands);
millions = (num/1000000); //work out how many millions
printMillions(millions);
}
cout << "Output 1: " << millions << "," << thousands << "," << hundreds << endl; //prints out result with comma
}
void printMillions (int unsigned i) //start of millions fcn
{
int unsigned t; //declares integer
t=i%100; //works out remainder
i=i/100; //works out value of i
cout << units[i-1] << " Hundred and "; //diplays hundred value
if (t>19&&t<100) // works out remainder value
printTens(t);
else if (i>9&&i<=19)
printTeens(t);
else if (t<10)
printUnits(t);
}
void printThousands(int unsigned i) //start of print thousands fcn
{
int unsigned t; //declares integer
t=i%100; //works out remainder
i=i/100; //works out i value
cout << units[i-1] << " Hundred and "; //prints out hundred value
if (t>19&&t<100) // if remainder within these limits calls function
printTens(t);
else if (i>9&&i<=19)
printTeens(t);
else if (t<10)
printUnits(t);
}
void printHundreds(int unsigned i) //start of print hundreds fcn
{
int unsigned t; //declares integer
t=i%100; //works out remainder
i=i/100; //works out i value
cout << units[i-1] << " Hundred and "; //prints out hundred value
if (t>19&&t<100) //if remainder within these limits calls function
printTens(t);
else if (i>9&&i<=19)
printTeens(t);
else if (t<10)
printUnits(t);
}
void printTens (int unsigned i) //start of fcn to print tens
{
int unsigned t; //declares integer
if (i>19&&i<100) //if i within limits statement executes
{
t=i%10; //works out remainder
i=i/10; //works out new i value
cout << tens[i-2] ; //prints result out whilst looking up table
if (t<10) //if remainder within these limits calls function
printUnits(t);
}
}
void printTeens (int unsigned i) //start of func to print teens
{
int unsigned t; //declares integer
if (i>9&&i<=19) //if i within these limits statement executes
{
t=i%10; //works out remainder
cout << teens[t-1] << endl; //prints out value whilst looking up table
}
}
void printUnits (int unsigned t) //start of fcn to print units
{
if (t<10) //if statement less than 11 executes
{
cout << " " << units[t-1] << endl; //prints out result whilst looking up table.
}
}